CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.buffer.readline

R, C, K = list(map(int,input().split()))
RC = [[0] * (C+1) for _ in range(R+1)]

for _ in range(K):
    r, c, v = list(map(int,input().split()))
    RC[r-1][c-1] = v


dp = [[[0] * 4 for _ in range(C+1)] for _ in range(R+1)]

dp[0][0][1] = RC[0][0]
for r in range(R):
    for c in range(C):
        for i in range(4):
            dp[r][c+1][i] = max(dp[r][c][i], dp[r][c+1][i])
            dp[r+1][c][0] = max(dp[r][c][i], dp[r+1][c][0])
            if i <= 2:
                dp[r][c+1][i+1] = max(dp[r][c][i] + RC[r][c+1], dp[r][c+1][i+1])
        dp[r+1][c][1] = max(dp[r+1][c][1], dp[r+1][c][0] + RC[r+1][c])
        
print((max(dp[-2][-2])))
            
                
            
    


# Optimized solution
import sys
input = sys.stdin.buffer.readline

R, C, K = list(map(int,input().split()))
RC = [[0] * (C+1) for _ in range(R+1)]

for _ in range(K):
    r, c, v = list(map(int,input().split()))
    RC[r-1][c-1] = v


dp = [[[0] * (C+1) for i in range(R+1)] for j in range(4)]

dp[1][0][0] = RC[0][0]
for r in range(R):
    for c in range(C):
        for i in range(4):
            dp[i][r][c+1] = max(dp[i][r][c], dp[i][r][c+1])
            if i <= 2:
                dp[i+1][r][c+1] = max(dp[i][r][c] + RC[r][c+1], dp[i+1][r][c+1])
            dp[0][r+1][c] = max(dp[i][r][c], dp[0][r+1][c])
        dp[1][r+1][c] = max(dp[1][r+1][c], dp[0][r+1][c] + RC[r+1][c])

print((max(dp[0][-2][-2], dp[1][-2][-2], dp[2][-2][-2], dp[3][-2][-2])))
# Original solution
R, C, K = list(map(int, input().split()))
item = [[0] * C for i in range(R)]
for i in range(K):
  r, c, v = list(map(int, input().split()))
  item[r - 1][c - 1] = v

dp = [[[0] * 4 for j in range(C)] for i in range(R)] 
#print(dp, item)


for i in range(R):
  for j in range(C):
    for k in range(4):
      if (i == 0) and (j == 0):
        continue
      else:
        if i == 0:
          dp[i][j][k] = dp[i][j - 1][k]
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])  
        elif j == 0:
          dp[i][j][k] = dp[i - 1][j][3]
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])           
        else:
          dp[i][j][k] = dp[i][j - 1][k]
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3])
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])       
    #for k in range(3):
    #  dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k])
    
    for k in range(3):
          if (item[i][j] != 0):
            dp[i][j][3 - k] = max(dp[i][j][3 - k], dp[i][j][2 - k] + item[i][j])          

#print(item)            
#print(dp)
print((dp[-1][-1][-1]))  



# Optimized solution
R,C,K = list(map(int,input().split()))
J=[0] * K
for i in range(K):
  J[i] = list(map(int,input().split()))

#DP0 = [[0]*(C+2) for i in range(R+2)]
DP1 = [[0]*(C+2) for i in range(R+2)]
DP2 = [[0]*(C+2) for i in range(R+2)]
DP3 = [[0]*(C+2) for i in range(R+2)]

MAP = [[0]*C for i in range(R)]

for x,y,v in J:
    MAP[x-1][y-1] = v
#print(MAP)
for i in range(R):
    for j in range(C):
        DP1[i][j] = max(DP1[i][j-1],DP1[i-1][j]+MAP[i][j],DP2[i-1][j]+MAP[i][j],DP3[i-1][j]+MAP[i][j])
        DP2[i][j] = max(DP2[i][j-1],DP1[i][j-1]+MAP[i][j])
        DP3[i][j] = max(DP3[i][j-1],DP2[i][j-1]+MAP[i][j])

#print(DP1)
#print(DP2)
#print(DP3)
ANS=0
for i in range(R):
    for j in range(C):
        ANS=max(ANS,DP1[i][j],DP2[i][j],DP3[i][j])
print(ANS)
# Original solution
import sys
input = sys.stdin.readline

def main():
  h, w, k = list(map(int, input().split()))
  U = h*w
  G = [0]*U
  for _ in range(k):
    r, c, v = list(map(int, input().split()))
    r -= 1
    c -= 1
    G[r*w+c] = v
  dp = [[0]*4 for _ in range(U)]
  dp[0][0] = 0
  dp[0][1] = G[0]
  for i in range(h):
    for j in range(w):
      if j:
        dp[i*w+j][0] = dp[i*w+j-1][0]
        for k in range(1, 4):
          dp[i*w+j][k] = max(dp[i*w+j][k], dp[i*w+j-1][k-1]+G[i*w+j], dp[i*w+j-1][k])
      if i:
        temp = max(dp[(i-1)*w+j])
        dp[i*w+j][0] = max(dp[i*w+j][0], temp)
        dp[i*w+j][1] = max(dp[i*w+j][1], temp+G[i*w+j])
  ans = max(dp[U-1])
  print(ans)
if __name__ == "__main__":
  main()

# Optimized solution
import sys
input = sys.stdin.readline

h, w, k = list(map(int, input().split()))
G = [[0]*w for _ in range(h)]
for _ in range(k):
  r, c, v = list(map(int, input().split()))
  r -= 1
  c -= 1
  G[r][c] = v
dp = [[[0]*w for i in range(h)] for j in range(4)]
dp[0][0][0] = 0
dp[1][0][0] = G[0][0]
for i in range(h):
  for j in range(w):
    if j:
      dp[0][i][j] = dp[0][i][j-1]
      for k in range(1, 4):
        dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1]+G[i][j], dp[k][i][j-1])
    if i:
      dp[0][i][j] = max(dp[0][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j]))
      dp[1][i][j] = max(dp[1][i][j], max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])+G[i][j])
ans = max(dp[0][h-1][w-1],dp[1][h-1][w-1],dp[2][h-1][w-1],dp[3][h-1][w-1])
print(ans)
# Original solution
H, W, K = list(map(int, input().split()))
A = [[0] * W for _ in range(H)]
for i in range(K):
    y, x, v = list(map(int, input().split()))
    A[y - 1][x - 1] = v
dp = [[0] * 4 for i in range(W)]
pre = []
dp[0][1] = A[0][0]
for i in range(H):
    for j in range(W):
        for k in range(4):
            if i:
                if k == 1:
                        dp[j][k] = max(dp[j][k], max(pre[j]) + A[i][j])
                if k == 0:
                        dp[j][k] = max(dp[j][k], max(pre[j]))
            if j:
                if 0 < k:
                    dp[j][k] = max(dp[j][k], dp[j - 1][k - 1] + A[i][j])
                dp[j][k] = max(dp[j][k], dp[j - 1][k])
    pre = dp
ans = 0
for j in range(W):
    ans = max(ans, max(dp[j]))
print(ans)


# Optimized solution
H, W, K = list(map(int, input().split()))
A = [[0] * W for _ in range(H)]
for i in range(K):
    y, x, v = list(map(int, input().split()))
    A[y - 1][x - 1] = v
dp = [[0] * 4 for i in range(W)]
pre = []
dp[0][1] = A[0][0]
for i in range(H):
    for j in range(W):
        if i:
            dp[j][0] = max(dp[j][0], max(pre[j]))
            dp[j][1] = max(dp[j][1], max(pre[j]) + A[i][j])
        for k in range(4):
            if j:
                if 0 < k:
                    dp[j][k] = max(dp[j][k], dp[j - 1][k - 1] + A[i][j])
                dp[j][k] = max(dp[j][k], dp[j - 1][k])
    pre = dp
ans = 0
for j in range(W):
    ans = max(ans, max(dp[j]))
print(ans)
# Original solution
import sys
input = sys.stdin.readline
R,C,K = list(map(int,input().split()))
RCV = [tuple(map(int,input().split())) for i in range(K)]

goods = [dict() for _ in range(R)]
for r,c,v in RCV:
    r,c = r-1,c-1
    goods[r][c] = v

dp = [[[0]*4 for _ in range(C)] for _ in range(R)]
if 0 in goods[0]:
    dp[0][0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        if i:
            pre = max(dp[i-1][j])
            dp[i][j][0] = pre
            if j in goods[i]:
                dp[i][j][1] = pre + goods[i][j]
        if j:
            for k in range(4):
                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
            if j in goods[i]:
                for k in reversed(list(range(3))):
                    dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j-1][k] + goods[i][j])

print((max(dp[-1][-1])))

# Optimized solution
import sys
input = sys.stdin.readline
R,C,K = list(map(int,input().split()))
RCV = [tuple(map(int,input().split())) for i in range(K)]

goods = [dict() for _ in range(R)]
for r,c,v in RCV:
    r,c = r-1,c-1
    goods[r][c] = v

dp0 = [[0]*4 for _ in range(C)]
dp1 = [[0]*4 for _ in range(C)]
if 0 in goods[0]:
    dp1[0][1] = goods[0][0]
for i in range(R):
    for j in range(C):
        if i:
            pre = max(dp0[j])
            dp1[j][0] = pre
            if j in goods[i]:
                dp1[j][1] = pre + goods[i][j]
        if j:
            for k in range(4):
                dp1[j][k] = max(dp1[j][k], dp1[j-1][k])
            if j in goods[i]:
                for k in reversed(list(range(3))):
                    dp1[j][k+1] = max(dp1[j][k+1], dp1[j-1][k] + goods[i][j])
    dp0 = dp1
    dp1 = [[0]*4 for _ in range(C)]
print((max(dp0[-1])))
# Original solution
r,c,k = list(map(int,input().split()))
dp = [[[0 for _ in range(4)] for _ in range(c+1)] for _ in range(r+1)]
g = [[0]*(c+1) for _ in range(r+1)]
for _ in range(k):
    a,b,v = list(map(int,input().split()))
    g[a][b] = v

for i in range(r+1):
    for j in range(c+1):
        for m in range(4):
            tmp  =dp[i][j][m]
            if i+1<=r:
                dp[i+1][j][0] = max(tmp,dp[i+1][j][0])
                dp[i+1][j][1] = max(dp[i+1][j][1],tmp + g[i+1][j])
            if j+1<=c:
                dp[i][j+1][m] = max(dp[i][j+1][m],tmp)
                if m<3:
                    dp[i][j+1][m+1] = max(dp[i][j+1][m+1],tmp + g[i][j+1]) 
print((max(dp[-1][-1])))
 

# Optimized solution
import sys

R, C, K = list(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 = list(map(int, s.split()))
    item[r][c] = v

dp = [[[0] * (C + 1) for _ in range(R + 1)] for _ in range(4)]
for i in range(R + 1):
    for j in range(C + 1):
        for k in range(4):
            here = dp[k][i][j]
            if i + 1 <= R:
                dp[0][i + 1][j] = max(dp[0][i + 1][j], here)
                dp[1][i + 1][j] = max(dp[1][i + 1][j], here + item[i + 1][j])
            if j + 1 <= C:
                dp[k][i][j + 1] = max(dp[k][i][j + 1], here)
                if k < 3:
                    dp[k + 1][i][j + 1] = max(dp[k + 1][i][j + 1], here + item[i][j + 1])
ans = 0
for k in range(4):
    ans = max(ans, dp[k][-1][-1])
print(ans)
# Original solution
r,c,k = list(map(int,input().split()))
dp = [[[0 for _ in range(4)] for _ in range(c+1)] for _ in range(r+1)]
g = [[0]*(c+1) for _ in range(r+1)]
for _ in range(k):
    a,b,v = list(map(int,input().split()))
    g[a][b] = v

for i in range(r+1):
    for j in range(c+1):
        for k in range(4):
            here = dp[i][j][k]
            if i + 1 <= r:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], here)
                dp[i + 1][j][1] = max(dp[i + 1][j][1], here + g[i + 1][j])
            if j + 1 <= c:
                dp[i][j + 1][k] = max(dp[i][j + 1][k], here)
                if k < 3:
                    dp[i][j + 1][k+1] = max(dp[i][j + 1][k+1], here + g[i][j + 1])
ans = 0
for k in range(4):
    ans = max(ans, dp[-1][-1][k])
print(ans)
# ans = 0
            # tmp  =dp[i][j][m]
            # if i+1<=r:
            #     dp[i+1][j][0] = max(tmp,dp[i+1][j][0])
            #     dp[i+1][j][1] = max(dp[i+1][j][1],tmp + g[i+1][j])
            # if j+1<=c:
            #     dp[i][j+1][m] = max(dp[i][j+1][m],tmp)
            #     if m<3:
            #         dp[i][j+1][m+1] = max(dp[i][j+1][m+1],tmp + g[i][j+1]) 
# print(max(dp[-1][-1]))
 

# Optimized solution
r,c,k = list(map(int,input().split()))
dp = [[[0]*(c+1) for _ in range(r+1)] for _ in range(4)]
g = [[0]*(c+1) for _ in range(r+1)]
for _ in range(k):
    a,b,v = list(map(int,input().split()))
    g[a][b] = v

for i in range(r+1):
    for j in range(c+1):
        for m in range(4):
            tmp  =dp[m][i][j]
            if i+1<=r:
                dp[0][i+1][j] = max(tmp,dp[0][i+1][j])
                dp[1][i+1][j] = max(dp[1][i+1][j],tmp + g[i+1][j])
            if j+1<=c:
                dp[m][i][j+1] = max(dp[m][i][j+1],tmp)
                if m<3:
                    dp[m+1][i][j+1] = max(dp[m+1][i][j+1],tmp + g[i][j+1]) 
ans = 0
for m in range(4):
    ans = max(ans,dp[m][-1][-1])
print(ans)
# Original solution
r,c,k = list(map(int, input().split()))
rcv = [list(map(int, input().split())) for i in range(k)]
d = dict()
for rr,cc,vv in rcv:
    d[(rr-1,cc-1)] = vv

DP_now = [[0]*4 for i in range(c)]
DP_prev = [[0]*4 for i in range(c)]

for i in range(r):
    for j in range(c):
        if (i,j) in d:
            v = d[(i,j)]
            max_top = max(DP_prev[j]) if i!=0 else 0
            DP_now[j][0] = max_top
            DP_now[j][1] = max_top + v
            for k in range(4):
                if j != 0:
                    if DP_now[j][k] < DP_now[j-1][k]:
                        DP_now[j][k] = DP_now[j-1][k]
                    if k!=0 and DP_now[j][k] < DP_now[j-1][k-1] + v:
                        DP_now[j][k] =  DP_now[j-1][k-1] + v
        else:
            max_top = max(DP_prev[j]) if i!=0 else 0
            DP_now[j][0] = max_top
            for k in range(4):
                if j != 0 and DP_now[j][k] < DP_now[j-1][k]:
                    DP_now[j][k] = DP_now[j-1][k]
    DP_prev = DP_now
    DP_now = [[0]*4 for i in range(c)]
ans = max(DP_prev[-1])
print(ans)


# Optimized solution
r,c,k = list(map(int, input().split()))
rcv = [list(map(int, input().split())) for i in range(k)]
d = dict()
for rr,cc,vv in rcv:
    d[(rr-1,cc-1)] = vv

DP_now = [[0]*4 for i in range(c)]
DP_prev = [[0]*4 for i in range(c)]

for i in range(r):
    for j in range(c):
        max_top = max(DP_prev[j]) if i!=0 else 0
        if (i,j) in d:
            v = d[(i,j)]
            DP_now[j][0] = max_top
            DP_now[j][1] = max_top + v
            for k in range(4):
                if j != 0:
                    if DP_now[j][k] < DP_now[j-1][k]:
                        DP_now[j][k] = DP_now[j-1][k]
                    if k!=0 and DP_now[j][k] < DP_now[j-1][k-1] + v:
                        DP_now[j][k] =  DP_now[j-1][k-1] + v
        else:
            DP_now[j][0] = max_top
            if j>0:
                DP_now[j] = DP_now[j-1][:]
            if DP_now[j][0] < max_top:
                DP_now[j][0] = max_top
    DP_prev = DP_now
    DP_now = [[0]*4 for i in range(c)]
ans = max(DP_prev[-1])
print(ans)
# Original solution
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[[0]*c for _ in range(r)]
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        ri-=1
        ci-=1
        v[ri][ci]=a
    dp=[[[0]*c for _ in range(r)] for i in range(4)]
    #print(dp)
    if v[0][0]>0:
        dp[1][0][0]=v[0][0]

    for i in range(r):
        for j in range(c):
            if i>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])
                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])
            if j>0:
                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])
                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])
                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])
                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])
                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])
                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])
                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])
    #print(dp)
    ans=0
    for i in range(4):
        ans=max(dp[i][r-1][c-1],ans)
    return print(ans)
if __name__=="__main__":
    main()



# Optimized solution
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[0] * (c * r)
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[(ri-1)*c + (ci-1)]=a
    dp= [0] * (c * r * 4)
    #print(dp)
    x = c * r
    if v[0]>0: dp[x] = v[0]

    for i in range(r):
        for j in range(c):
            idx = i*c+j
            idx2 = (i-1)*c + j
            idx3 = i*c + j-1
            if i>0:
                dp[x + idx] = max(dp[x + idx], dp[idx2]+v[idx])
                dp[idx] = max(dp[idx],dp[idx2])
                dp[x + idx]=max(dp[x + idx],dp[x + idx2]+v[idx])
                dp[idx]=max(dp[idx],dp[x + idx2])
                dp[x + idx]=max(dp[x + idx],dp[2*x + idx2]+v[idx])
                dp[idx]=max(dp[idx],dp[2*x + idx2])
                dp[x + idx]=max(dp[x + idx],dp[3*x + idx2]+v[idx])
                dp[idx]=max(dp[idx],dp[3*x + idx2])
            if j>0:
                dp[x + idx]=max(dp[x + idx],dp[idx3]+v[idx])
                dp[2*x + idx]=max(dp[2*x + idx],dp[x + idx3]+v[idx])
                dp[3*x + idx]=max(dp[3*x + idx],dp[2*x + idx3]+v[idx])
                dp[idx]=max(dp[idx],dp[idx3])
                dp[x + idx]=max(dp[x + idx],dp[x + idx3])
                dp[2*x + idx]=max(dp[2*x + idx],dp[2*x + idx3])
                dp[3*x + idx]=max(dp[3*x + idx],dp[3*x + idx3])

    #print(dp)
    ans=0
    for i in range(4): ans=max(dp[i*x + (r-1)*c + (c-1)],ans)
    return print(ans)

if __name__=="__main__":
    main()
# Original solution
import sys
input=sys.stdin.readline
def main():
    r,c,k=map(int,input().split())
    v=[0] * (c * r)
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[(ri-1)*c + (ci-1)]=a
    dp= [0] * (c * r * 4)
    #print(dp)
    x = c * r
    if v[0]>0: dp[x] = v[0]

    for i in range(r):
        for j in range(c):
            idx = i*c+j
            idx2 = (i-1)*c + j
            idx3 = i*c + j-1
            if i>0:
                dp[x + idx] = max(dp[x + idx], dp[idx2]+v[idx])
                dp[idx] = max(dp[idx],dp[idx2])
                dp[x + idx]=max(dp[x + idx],dp[x + idx2]+v[idx])
                dp[idx]=max(dp[idx],dp[x + idx2])
                dp[x + idx]=max(dp[x + idx],dp[2*x + idx2]+v[idx])
                dp[idx]=max(dp[idx],dp[2*x + idx2])
                dp[x + idx]=max(dp[x + idx],dp[3*x + idx2]+v[idx])
                dp[idx]=max(dp[idx],dp[3*x + idx2])
            if j>0:
                dp[x + idx]=max(dp[x + idx],dp[idx3]+v[idx])
                dp[2*x + idx]=max(dp[2*x + idx],dp[x + idx3]+v[idx])
                dp[3*x + idx]=max(dp[3*x + idx],dp[2*x + idx3]+v[idx])
                dp[idx]=max(dp[idx],dp[idx3])
                dp[x + idx]=max(dp[x + idx],dp[x + idx3])
                dp[2*x + idx]=max(dp[2*x + idx],dp[2*x + idx3])
                dp[3*x + idx]=max(dp[3*x + idx],dp[3*x + idx3])

    #print(dp)
    ans=0
    for i in range(4): ans=max(dp[i*x + (r-1)*c + (c-1)],ans)
    return print(ans)

if __name__=="__main__":
    main()



# Optimized solution
import sys
input=sys.stdin.readline

def main():
    r,c,k=map(int,input().split())
    v=[0] * (c * r)
    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[(ri-1)*c + (ci-1)]=a

    dp= [0] * (c * r * 4)

    #print(dp)
    x = c * r
    if v[0]>0: dp[x] = v[0]

    for i in range(r):
        for j in range(c):
            idx = i*c+j
            idx2 = (i-1)*c + j
            idx3 = i*c + j-1

            if i>0:
                a = max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2])+v[idx]
                dp[x+idx]=max(dp[x+idx], a)
                dp[idx]=max(dp[idx],dp[idx2],dp[x+idx2],dp[2*x+idx2],dp[3*x+idx2])

            if j>0:
                dp[idx]=max(dp[idx],dp[idx3])
                dp[x + idx]=max(dp[x + idx],dp[idx3]+v[idx],dp[x + idx3])
                dp[2*x + idx]=max(dp[2*x + idx],dp[x + idx3]+v[idx],dp[2*x + idx3])
                dp[3*x + idx]=max(dp[3*x + idx],dp[2*x + idx3]+v[idx],dp[3*x + idx3])

    #print(dp)
    ans=0
    for i in range(4): ans = max(dp[i*x + (r-1)*c + (c-1)],ans)
    return print(ans)

if __name__=="__main__":
    main()
# Original solution
import sys
input=sys.stdin.readline

class list2D:
    def __init__(self, H, W, num):
        self.__H = H
        self.__W = W
        self.__dat = [num] * (H * W)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__W+a[1]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__W+a[1]] = b

    def debug(self):
        print(self.__dat)

class list3D:
    def __init__(self, H, W, D, num):
        self.__H = H
        self.__W = W
        self.__D = D
        self.__X = W * D
        self.__dat = [num] * (H * W * D)

    def __getitem__(self, a):
        return self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]]

    def __setitem__(self, a, b):
        self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]] = b

    def debug(self):
        print(self.__dat)

def main():
    r,c,k=map(int,input().split())
    v = list2D(r, c, 0)

    for _ in range(k):
        ri,ci,a=map(int,input().split())
        v[ri-1, ci-1] = a

    dp = list3D(r, c, 4, 0)
    #print(dp)
    if v[0, 0]>0: dp[0, 0, 1] = v[0, 0]

    for i in range(r):
        for j in range(c):
            val = v[i, j]
            if i>0:
                x = max(dp[i-1, j, 0], dp[i-1, j, 1], dp[i-1, j, 2], dp[i-1, j, 3])
                dp[i, j, 1]=x+val
                dp[i, j, 0]=x

            if j>0:
                X = dp[i, j-1, 0]
                Y = dp[i, j-1, 1]
                V = dp[i, j-1, 2]
                Z = dp[i, j-1, 3]
                dp[i, j, 0]=max(dp[i, j, 0],X)
                dp[i, j, 1]=max(dp[i, j, 1],X+val,Y)
                dp[i, j, 2]=max(Y+val,V)
                dp[i, j, 3]=max(V+val,Z)

    #print(dp)
    ans=0
    for i in range(4): ans=max(dp[r-1, c-1, i],ans)
    return print(ans)
if __name__=="__main__":
    main()


# Optimized solution
import sys

inp = [int(x) for x in sys.stdin.read().split()]
ii = 0

r, c, k = inp[ii:ii+3]
ii += 3
v=[0] * (c * r)
for _ in range(k):
    ri,ci,a = inp[ii:ii+3]
    ii += 3
    v[(ri-1)*c + (ci-1)]=a
dp= [0] * (c * r * 4)
x = c * r
if v[0]>0: dp[x] = v[0]

for i in range(r):
    for j in range(c):
        idx = i*c+j
        idx2 = (i-1)*c + j
        idx3 = i*c + j-1
        val = v[idx]
        if i>0:
            a = max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2])
            dp[idx]= a
            dp[x+idx]= a + val

        if j>0:
            dp[idx]=max(dp[idx],dp[idx3])
            dp[x + idx]=max(dp[x + idx],dp[idx3]+val,dp[x + idx3])
            dp[2*x + idx]=max(dp[x+idx3]+val,dp[2*x+idx3])
            dp[3*x + idx]=max(dp[2*x+idx3]+val,dp[3*x+idx3])


ans=0
for i in range(4): ans = max(dp[i*x + (r-1)*c + (c-1)],ans)
print(ans)
# Original solution
R,C,K = list(map(int, input().split()))

d = [[0 for _ in range(C+1)] for _ in range(R+1)]
dp = [[[0 for _ in range(4)] for _ in range(C+1)] for _ in range(R+1)]

for i in range(K):
    r, c, v = list(map(int, input().split()))
    d[r-1][c-1] = v
for i in range(R):
    for j in range(C):
        # kが大きい方からやらないとダメ。01ナップサックと同じ考え方。書き換えられた値を見ないように。
        for k in range(2, -1, -1):
            # 拾う遷移。d[i][j]を取るかどうかという判定
            dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k] + d[i][j])
        for k in range(4):
            dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k])
            dp[i][j+1][k] = max(dp[i][j+1][k], dp[i][j][k])
ans = 0

for k in range(4):
    ans = max(ans, dp[R-1][C-1][k])
print(ans)    

# Optimized solution
R,C,K = list(map(int, input().split()))

d = [[0 for _ in range(C+1)] for _ in range(R+1)]
dp = [[[0 for _ in range(C+1)] for _ in range(R+1)] for _ in range(4)]

for i in range(K):
    r, c, v = list(map(int, input().split()))
    d[r-1][c-1] = v
for i in range(R):
    for j in range(C):
        # kが大きい方からやらないとダメ。01ナップサックと同じ考え方。書き換えられた値を見ないように。
        for k in range(2, -1, -1):
            # 拾う遷移。d[i][j]を取るかどうかという判定
            dp[k+1][i][j] = max(dp[k+1][i][j], dp[k][i][j] + d[i][j])
        for k in range(4):
            dp[0][i+1][j] = max(dp[0][i+1][j], dp[k][i][j])
            dp[k][i][j+1] = max(dp[k][i][j+1], dp[k][i][j])
ans = 0

for k in range(4):
    ans = max(ans, dp[k][R-1][C-1])
print(ans)