import sys
import copy
R, C, K = map(int, input().split())
item = [[0] * (C + 1) for _ in range(R + 1)]  # dp配列と合わせるために, 0行目, 0列目を追加している.
for s in sys.stdin.readlines():
    r, c, v = map(int, s.split())
    r -= 1
    c -= 1
    item[r][c] = v
    
dp0 = [[0] * (C+1) for r in range(R+1)]
dp1 = [[0] * (C+1) for r in range(R+1)]
dp2 = [[0] * (C+1) for r in range(R+1)]
dp3 = [[0] * (C+1) for r in range(R+1)]
 
for r in range(R):
   for c in range(C):
      dp3[r][c] = max(dp3[r][c], dp2[r][c] + item[r][c])
      dp2[r][c] = max(dp2[r][c], dp1[r][c] + item[r][c])
      dp1[r][c] = max(dp1[r][c], dp0[r][c] + item[r][c])
      dp1[r][c+1] = max(dp1[r][c], dp1[r][c+1])
      dp2[r][c+1] = max(dp2[r][c], dp2[r][c+1])
      dp3[r][c+1] = max(dp3[r][c], dp3[r][c+1])
      dp0[r+1][c] = max(dp0[r+1][c], dp1[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp2[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp3[r][c])
ans = max(dp0[R-1][C-1], dp1[R-1][C-1])
ans = max(ans, dp3[R-1][C-1])
ans = max(ans, dp2[R-1][C-1])
print(ans) import sys sys import copy copy R, C, K = map(int, input().split()) R map(int, input().split()) map int input().split() ().split () input split C K [0] * (C + 1) for _ in range(R + 1) for _ in range(R + 1) _ range(R + 1) range R + 1 R 1 for _ in range(R + 1) [0] * (C + 1) [0] 0 C + 1 C 1 item = [[0] * (C + 1) for _ in range(R + 1)] item [[0] * (C + 1) for _ in range(R + 1)] for s in sys.stdin.readlines():
    r, c, v = map(int, s.split())
    r -= 1
    c -= 1
    item[r][c] = v
     s sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines r, c, v = map(int, s.split()) r map(int, s.split()) map int s.split() s.split s split c v r -= 1 r 1 c -= 1 c 1 item[r][c] = v item[r][c] [r] item r c v [0] * (C+1) for r in range(R+1) for r in range(R+1) r range(R+1) range R+1 R 1 for r in range(R+1) [0] * (C+1) [0] 0 C+1 C 1 dp0 = [[0] * (C+1) for r in range(R+1)] dp0 [[0] * (C+1) for r in range(R+1)] [0] * (C+1) for r in range(R+1) for r in range(R+1) r range(R+1) range R+1 R 1 for r in range(R+1) [0] * (C+1) [0] 0 C+1 C 1 dp1 = [[0] * (C+1) for r in range(R+1)] dp1 [[0] * (C+1) for r in range(R+1)] [0] * (C+1) for r in range(R+1) for r in range(R+1) r range(R+1) range R+1 R 1 for r in range(R+1) [0] * (C+1) [0] 0 C+1 C 1 dp2 = [[0] * (C+1) for r in range(R+1)] dp2 [[0] * (C+1) for r in range(R+1)] [0] * (C+1) for r in range(R+1) for r in range(R+1) r range(R+1) range R+1 R 1 for r in range(R+1) [0] * (C+1) [0] 0 C+1 C 1 dp3 = [[0] * (C+1) for r in range(R+1)] dp3 [[0] * (C+1) for r in range(R+1)] for r in range(R):
   for c in range(C):
      dp3[r][c] = max(dp3[r][c], dp2[r][c] + item[r][c])
      dp2[r][c] = max(dp2[r][c], dp1[r][c] + item[r][c])
      dp1[r][c] = max(dp1[r][c], dp0[r][c] + item[r][c])
      dp1[r][c+1] = max(dp1[r][c], dp1[r][c+1])
      dp2[r][c+1] = max(dp2[r][c], dp2[r][c+1])
      dp3[r][c+1] = max(dp3[r][c], dp3[r][c+1])
      dp0[r+1][c] = max(dp0[r+1][c], dp1[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp2[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp3[r][c]) r range(R) range R for c in range(C):
      dp3[r][c] = max(dp3[r][c], dp2[r][c] + item[r][c])
      dp2[r][c] = max(dp2[r][c], dp1[r][c] + item[r][c])
      dp1[r][c] = max(dp1[r][c], dp0[r][c] + item[r][c])
      dp1[r][c+1] = max(dp1[r][c], dp1[r][c+1])
      dp2[r][c+1] = max(dp2[r][c], dp2[r][c+1])
      dp3[r][c+1] = max(dp3[r][c], dp3[r][c+1])
      dp0[r+1][c] = max(dp0[r+1][c], dp1[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp2[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp3[r][c]) c range(C) range C dp3[r][c] = max(dp3[r][c], dp2[r][c] + item[r][c]) dp3[r][c] [r] dp3 r c max(dp3[r][c], dp2[r][c] + item[r][c]) max dp3[r][c] [r] dp3 r c dp2[r][c] + item[r][c] dp2[r][c] [r] dp2 r c item[r][c] [r] item r c dp2[r][c] = max(dp2[r][c], dp1[r][c] + item[r][c]) dp2[r][c] [r] dp2 r c max(dp2[r][c], dp1[r][c] + item[r][c]) max dp2[r][c] [r] dp2 r c dp1[r][c] + item[r][c] dp1[r][c] [r] dp1 r c item[r][c] [r] item r c dp1[r][c] = max(dp1[r][c], dp0[r][c] + item[r][c]) dp1[r][c] [r] dp1 r c max(dp1[r][c], dp0[r][c] + item[r][c]) max dp1[r][c] [r] dp1 r c dp0[r][c] + item[r][c] dp0[r][c] [r] dp0 r c item[r][c] [r] item r c dp1[r][c+1] = max(dp1[r][c], dp1[r][c+1]) dp1[r][c+1] [r] dp1 r c+1 c 1 max(dp1[r][c], dp1[r][c+1]) max dp1[r][c] [r] dp1 r c dp1[r][c+1] [r] dp1 r c+1 c 1 dp2[r][c+1] = max(dp2[r][c], dp2[r][c+1]) dp2[r][c+1] [r] dp2 r c+1 c 1 max(dp2[r][c], dp2[r][c+1]) max dp2[r][c] [r] dp2 r c dp2[r][c+1] [r] dp2 r c+1 c 1 dp3[r][c+1] = max(dp3[r][c], dp3[r][c+1]) dp3[r][c+1] [r] dp3 r c+1 c 1 max(dp3[r][c], dp3[r][c+1]) max dp3[r][c] [r] dp3 r c dp3[r][c+1] [r] dp3 r c+1 c 1 dp0[r+1][c] = max(dp0[r+1][c], dp1[r][c]) dp0[r+1][c] [r+1] dp0 r+1 r 1 c max(dp0[r+1][c], dp1[r][c]) max dp0[r+1][c] [r+1] dp0 r+1 r 1 c dp1[r][c] [r] dp1 r c dp0[r+1][c] = max(dp0[r+1][c], dp2[r][c]) dp0[r+1][c] [r+1] dp0 r+1 r 1 c max(dp0[r+1][c], dp2[r][c]) max dp0[r+1][c] [r+1] dp0 r+1 r 1 c dp2[r][c] [r] dp2 r c dp0[r+1][c] = max(dp0[r+1][c], dp3[r][c]) dp0[r+1][c] [r+1] dp0 r+1 r 1 c max(dp0[r+1][c], dp3[r][c]) max dp0[r+1][c] [r+1] dp0 r+1 r 1 c dp3[r][c] [r] dp3 r c ans = max(dp0[R-1][C-1], dp1[R-1][C-1]) ans max(dp0[R-1][C-1], dp1[R-1][C-1]) max dp0[R-1][C-1] [R-1] dp0 R-1 R 1 C-1 C 1 dp1[R-1][C-1] [R-1] dp1 R-1 R 1 C-1 C 1 ans = max(ans, dp3[R-1][C-1]) ans max(ans, dp3[R-1][C-1]) max ans dp3[R-1][C-1] [R-1] dp3 R-1 R 1 C-1 C 1 ans = max(ans, dp2[R-1][C-1]) ans max(ans, dp2[R-1][C-1]) max ans dp2[R-1][C-1] [R-1] dp2 R-1 R 1 C-1 C 1 print(ans) print ans dp2 = [[0] * (C+1) for r in range(R+1)] [[0] * (C+1) for r in range(R+1)] dp2 v = map(int, s.split()) map(int, s.split()) v r, c, v = map(int, s.split()) map(int, s.split()) r c, v = map(int, s.split()) map(int, s.split()) c dp1 = [[0] * (C+1) for r in range(R+1)] [[0] * (C+1) for r in range(R+1)] dp1 C, K = map(int, input().split()) map(int, input().split()) C r -= 1 1 r ans = max(dp0[R-1][C-1], dp1[R-1][C-1]) max(dp0[R-1][C-1], dp1[R-1][C-1]) ans ans = max(ans, dp3[R-1][C-1]) max(ans, dp3[R-1][C-1]) ans ans = max(ans, dp2[R-1][C-1]) max(ans, dp2[R-1][C-1]) ans c -= 1 1 c R, C, K = map(int, input().split()) map(int, input().split()) R item = [[0] * (C + 1) for _ in range(R + 1)] [[0] * (C + 1) for _ in range(R + 1)] item dp3 = [[0] * (C+1) for r in range(R+1)] [[0] * (C+1) for r in range(R+1)] dp3 dp0 = [[0] * (C+1) for r in range(R+1)] [[0] * (C+1) for r in range(R+1)] dp0 K = map(int, input().split()) map(int, input().split()) K
import sys
import copy
R, C, K = map(int, input().split())
item = [[0] * (C + 1) for _ in range(R + 1)]  # dp配列と合わせるために, 0行目, 0列目を追加している.
for s in sys.stdin.readlines():
    r, c, v = map(int, s.split())
    r -= 1
    c -= 1
    item[r][c] = v
    
dp0 = [[0] * (C+1) for r in range(R+1)]
dp1 = [[0] * (C+1) for r in range(R+1)]
dp2 = [[0] * (C+1) for r in range(R+1)]
dp3 = [[0] * (C+1) for r in range(R+1)]
 
for r in range(R):
   for c in range(C):
      dp3[r][c] = max(dp3[r][c], dp2[r][c] + item[r][c])
      dp2[r][c] = max(dp2[r][c], dp1[r][c] + item[r][c])
      dp1[r][c] = max(dp1[r][c], dp0[r][c] + item[r][c])
      dp1[r][c+1] = max(dp1[r][c], dp1[r][c+1])
      dp2[r][c+1] = max(dp2[r][c], dp2[r][c+1])
      dp3[r][c+1] = max(dp3[r][c], dp3[r][c+1])
      dp0[r+1][c] = max(dp0[r+1][c], dp1[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp2[r][c])
      dp0[r+1][c] = max(dp0[r+1][c], dp3[r][c])
ans = max(dp0[R-1][C-1], dp1[R-1][C-1])
ans = max(ans, dp3[R-1][C-1])
ans = max(ans, dp2[R-1][C-1])
print(ans)