import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)
    for i in range(K):
        r, c, v = NMI()
        grid[r-1][c-1] = v

    dp0 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp1 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp2 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp3 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]

    for r in range(R+1):
        for c in range(C+1):
            now0 = dp0[r][c]
            now1 = dp1[r][c]
            now2 = dp2[r][c]
            now3 = dp3[r][c]
            try:
                val = grid[r][c]
            except:
                val = 0

            dp0[r+1][c] = max(now3, dp0[r+1][c])

            # 取って下
            dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c])
            # 取らずに下
            dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c])
            # 取って横
            dp1[r][c+1] = max(now0 + val, dp1[r][c+1])
            dp2[r][c+1] = max(now1 + val, dp2[r][c+1])
            dp3[r][c+1] = max(now2 + val, dp3[r][c+1])

            # 取らずに横
            dp1[r][c+1] = max(now1, dp1[r][c+1])
            dp2[r][c+1] = max(now2, dp2[r][c+1])
            dp0[r][c+1] = max(now0, dp0[r][c+1])

    print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]))


if __name__ == "__main__":
    main() import sys sys import math math from collections import defaultdict from collections import deque sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 input = lambda: sys.stdin.readline().strip() input lambda: sys.stdin.readline().strip() sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip NI = lambda: int(input()) NI lambda: int(input()) int(input()) int input() input NMI = lambda: map(int, input().split()) NMI lambda: map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split NLI = lambda: list(NMI()) NLI lambda: list(NMI()) list(NMI()) list NMI() NMI SI = lambda: input() SI lambda: input() input() input [int(num)] * w for _ in range(h) for _ in range(h) _ range(h) range h for _ in range(h) [int(num)] * w [int(num)] int(num) int num w def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)] make_grid_int h h w w num num def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)
    for i in range(K):
        r, c, v = NMI()
        grid[r-1][c-1] = v

    dp0 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp1 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp2 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp3 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]

    for r in range(R+1):
        for c in range(C+1):
            now0 = dp0[r][c]
            now1 = dp1[r][c]
            now2 = dp2[r][c]
            now3 = dp3[r][c]
            try:
                val = grid[r][c]
            except:
                val = 0

            dp0[r+1][c] = max(now3, dp0[r+1][c])

            # 取って下
            dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c])
            # 取らずに下
            dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c])
            # 取って横
            dp1[r][c+1] = max(now0 + val, dp1[r][c+1])
            dp2[r][c+1] = max(now1 + val, dp2[r][c+1])
            dp3[r][c+1] = max(now2 + val, dp3[r][c+1])

            # 取らずに横
            dp1[r][c+1] = max(now1, dp1[r][c+1])
            dp2[r][c+1] = max(now2, dp2[r][c+1])
            dp0[r][c+1] = max(now0, dp0[r][c+1])

    print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])) main R, C, K = NMI() R NMI() NMI C K grid = make_grid_int(R, C, 0) grid make_grid_int(R, C, 0) make_grid_int R C 0 for i in range(K):
        r, c, v = NMI()
        grid[r-1][c-1] = v

     i range(K) range K r, c, v = NMI() r NMI() NMI c v grid[r-1][c-1] = v grid[r-1][c-1] [r-1] grid r-1 r 1 c-1 c 1 v [0 for _ in range(C + 2)] for _ in range(R + 2) for _ in range(R + 2) _ range(R + 2) range R + 2 R 2 for _ in range(R + 2) [0 for _ in range(C + 2)] dp0 = [[0 for _ in range(C + 2)] for _ in range(R + 2)] dp0 [[0 for _ in range(C + 2)] for _ in range(R + 2)] [0 for _ in range(C + 2)] for _ in range(R + 2) for _ in range(R + 2) _ range(R + 2) range R + 2 R 2 for _ in range(R + 2) [0 for _ in range(C + 2)] dp1 = [[0 for _ in range(C + 2)] for _ in range(R + 2)] dp1 [[0 for _ in range(C + 2)] for _ in range(R + 2)] [0 for _ in range(C + 2)] for _ in range(R + 2) for _ in range(R + 2) _ range(R + 2) range R + 2 R 2 for _ in range(R + 2) [0 for _ in range(C + 2)] dp2 = [[0 for _ in range(C + 2)] for _ in range(R + 2)] dp2 [[0 for _ in range(C + 2)] for _ in range(R + 2)] [0 for _ in range(C + 2)] for _ in range(R + 2) for _ in range(R + 2) _ range(R + 2) range R + 2 R 2 for _ in range(R + 2) [0 for _ in range(C + 2)] dp3 = [[0 for _ in range(C + 2)] for _ in range(R + 2)] dp3 [[0 for _ in range(C + 2)] for _ in range(R + 2)] for r in range(R+1):
        for c in range(C+1):
            now0 = dp0[r][c]
            now1 = dp1[r][c]
            now2 = dp2[r][c]
            now3 = dp3[r][c]
            try:
                val = grid[r][c]
            except:
                val = 0

            dp0[r+1][c] = max(now3, dp0[r+1][c])

            # 取って下
            dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c])
            # 取らずに下
            dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c])
            # 取って横
            dp1[r][c+1] = max(now0 + val, dp1[r][c+1])
            dp2[r][c+1] = max(now1 + val, dp2[r][c+1])
            dp3[r][c+1] = max(now2 + val, dp3[r][c+1])

            # 取らずに横
            dp1[r][c+1] = max(now1, dp1[r][c+1])
            dp2[r][c+1] = max(now2, dp2[r][c+1])
            dp0[r][c+1] = max(now0, dp0[r][c+1])

     r range(R+1) range R+1 R 1 for c in range(C+1):
            now0 = dp0[r][c]
            now1 = dp1[r][c]
            now2 = dp2[r][c]
            now3 = dp3[r][c]
            try:
                val = grid[r][c]
            except:
                val = 0

            dp0[r+1][c] = max(now3, dp0[r+1][c])

            # 取って下
            dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c])
            # 取らずに下
            dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c])
            # 取って横
            dp1[r][c+1] = max(now0 + val, dp1[r][c+1])
            dp2[r][c+1] = max(now1 + val, dp2[r][c+1])
            dp3[r][c+1] = max(now2 + val, dp3[r][c+1])

            # 取らずに横
            dp1[r][c+1] = max(now1, dp1[r][c+1])
            dp2[r][c+1] = max(now2, dp2[r][c+1])
            dp0[r][c+1] = max(now0, dp0[r][c+1])

     c range(C+1) range C+1 C 1 now0 = dp0[r][c] now0 dp0[r][c] [r] dp0 r c now1 = dp1[r][c] now1 dp1[r][c] [r] dp1 r c now2 = dp2[r][c] now2 dp2[r][c] [r] dp2 r c now3 = dp3[r][c] now3 dp3[r][c] [r] dp3 r c try:
                val = grid[r][c]
            except:
                val = 0

             except:
                val = 0

             val = 0 val 0 val = grid[r][c] val grid[r][c] [r] grid r c dp0[r+1][c] = max(now3, dp0[r+1][c]) dp0[r+1][c] [r+1] dp0 r+1 r 1 c max(now3, dp0[r+1][c]) max now3 dp0[r+1][c] [r+1] dp0 r+1 r 1 c dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c]) dp0[r+1][c] [r+1] dp0 r+1 r 1 c max(now0 + val, now1 + val, now2 + val, dp0[r+1][c]) max now0 + val now0 val now1 + val now1 val now2 + val now2 val dp0[r+1][c] [r+1] dp0 r+1 r 1 c dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c]) dp0[r+1][c] [r+1] dp0 r+1 r 1 c max(now0, now1, now2, now3, dp0[r+1][c]) max now0 now1 now2 now3 dp0[r+1][c] [r+1] dp0 r+1 r 1 c dp1[r][c+1] = max(now0 + val, dp1[r][c+1]) dp1[r][c+1] [r] dp1 r c+1 c 1 max(now0 + val, dp1[r][c+1]) max now0 + val now0 val dp1[r][c+1] [r] dp1 r c+1 c 1 dp2[r][c+1] = max(now1 + val, dp2[r][c+1]) dp2[r][c+1] [r] dp2 r c+1 c 1 max(now1 + val, dp2[r][c+1]) max now1 + val now1 val dp2[r][c+1] [r] dp2 r c+1 c 1 dp3[r][c+1] = max(now2 + val, dp3[r][c+1]) dp3[r][c+1] [r] dp3 r c+1 c 1 max(now2 + val, dp3[r][c+1]) max now2 + val now2 val dp3[r][c+1] [r] dp3 r c+1 c 1 dp1[r][c+1] = max(now1, dp1[r][c+1]) dp1[r][c+1] [r] dp1 r c+1 c 1 max(now1, dp1[r][c+1]) max now1 dp1[r][c+1] [r] dp1 r c+1 c 1 dp2[r][c+1] = max(now2, dp2[r][c+1]) dp2[r][c+1] [r] dp2 r c+1 c 1 max(now2, dp2[r][c+1]) max now2 dp2[r][c+1] [r] dp2 r c+1 c 1 dp0[r][c+1] = max(now0, dp0[r][c+1]) dp0[r][c+1] [r] dp0 r c+1 c 1 max(now0, dp0[r][c+1]) max now0 dp0[r][c+1] [r] dp0 r c+1 c 1 print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])) print max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]) max dp0[R][C] [R] dp0 R C dp1[R][C] [R] dp1 R C dp2[R][C] [R] dp2 R C dp3[R][C] [R] dp3 R C if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD NMI = lambda: map(int, input().split()) lambda: map(int, input().split()) NMI NI = lambda: int(input()) lambda: int(input()) NI def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)] def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)] make_grid_int def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)
    for i in range(K):
        r, c, v = NMI()
        grid[r-1][c-1] = v

    dp0 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp1 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp2 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp3 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]

    for r in range(R+1):
        for c in range(C+1):
            now0 = dp0[r][c]
            now1 = dp1[r][c]
            now2 = dp2[r][c]
            now3 = dp3[r][c]
            try:
                val = grid[r][c]
            except:
                val = 0

            dp0[r+1][c] = max(now3, dp0[r+1][c])

            # 取って下
            dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c])
            # 取らずに下
            dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c])
            # 取って横
            dp1[r][c+1] = max(now0 + val, dp1[r][c+1])
            dp2[r][c+1] = max(now1 + val, dp2[r][c+1])
            dp3[r][c+1] = max(now2 + val, dp3[r][c+1])

            # 取らずに横
            dp1[r][c+1] = max(now1, dp1[r][c+1])
            dp2[r][c+1] = max(now2, dp2[r][c+1])
            dp0[r][c+1] = max(now0, dp0[r][c+1])

    print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])) def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)
    for i in range(K):
        r, c, v = NMI()
        grid[r-1][c-1] = v

    dp0 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp1 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp2 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp3 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]

    for r in range(R+1):
        for c in range(C+1):
            now0 = dp0[r][c]
            now1 = dp1[r][c]
            now2 = dp2[r][c]
            now3 = dp3[r][c]
            try:
                val = grid[r][c]
            except:
                val = 0

            dp0[r+1][c] = max(now3, dp0[r+1][c])

            # 取って下
            dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c])
            # 取らずに下
            dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c])
            # 取って横
            dp1[r][c+1] = max(now0 + val, dp1[r][c+1])
            dp2[r][c+1] = max(now1 + val, dp2[r][c+1])
            dp3[r][c+1] = max(now2 + val, dp3[r][c+1])

            # 取らずに横
            dp1[r][c+1] = max(now1, dp1[r][c+1])
            dp2[r][c+1] = max(now2, dp2[r][c+1])
            dp0[r][c+1] = max(now0, dp0[r][c+1])

    print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])) main input = lambda: sys.stdin.readline().strip() lambda: sys.stdin.readline().strip() input SI = lambda: input() lambda: input() SI NLI = lambda: list(NMI()) lambda: list(NMI()) NLI
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)
    for i in range(K):
        r, c, v = NMI()
        grid[r-1][c-1] = v

    dp0 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp1 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp2 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]
    dp3 = [[0 for _ in range(C + 2)] for _ in range(R + 2)]

    for r in range(R+1):
        for c in range(C+1):
            now0 = dp0[r][c]
            now1 = dp1[r][c]
            now2 = dp2[r][c]
            now3 = dp3[r][c]
            try:
                val = grid[r][c]
            except:
                val = 0

            dp0[r+1][c] = max(now3, dp0[r+1][c])

            # 取って下
            dp0[r+1][c] = max(now0 + val, now1 + val, now2 + val, dp0[r+1][c])
            # 取らずに下
            dp0[r+1][c] = max(now0, now1, now2, now3, dp0[r+1][c])
            # 取って横
            dp1[r][c+1] = max(now0 + val, dp1[r][c+1])
            dp2[r][c+1] = max(now1 + val, dp2[r][c+1])
            dp3[r][c+1] = max(now2 + val, dp3[r][c+1])

            # 取らずに横
            dp1[r][c+1] = max(now1, dp1[r][c+1])
            dp2[r][c+1] = max(now2, dp2[r][c+1])
            dp0[r][c+1] = max(now0, dp0[r][c+1])

    print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]))


if __name__ == "__main__":
    main()
