import sys
input = sys.stdin.readline
r,c,k = map(int,input().split())
l = [[0]*c for i in range(r)] 
for i in range(k):
    x,y,v = map(int,input().split())
    l[x-1][y-1] = v
dp = [[0]*4 for i in range(c+1)]

for i in range(r):
    ndp = [[0]*4 for i in range(c+1)]
    for j in range(c):
        M = max(dp[j+1])
        if l[i][j]>0:
            v = l[i][j]
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(M+v,dp[j][0]+v,ndp[j][0]+v,dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][1]+v,ndp[j][1]+v,dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][2]+v,ndp[j][2]+v,dp[j][3],ndp[j][3])
        else:
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][3],ndp[j][3])
    dp = ndp
print(max(dp[-1])) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline r,c,k = map(int,input().split()) r map(int,input().split()) map int input().split() ().split () input split c k [0]*c for i in range(r) for i in range(r) i range(r) range r for i in range(r) [0]*c [0] 0 c l = [[0]*c for i in range(r)] l [[0]*c for i in range(r)] for i in range(k):
    x,y,v = map(int,input().split())
    l[x-1][y-1] = v i range(k) range k x,y,v = map(int,input().split()) x map(int,input().split()) map int input().split() ().split () input split y v l[x-1][y-1] = v l[x-1][y-1] [x-1] l x-1 x 1 y-1 y 1 v [0]*4 for i in range(c+1) for i in range(c+1) i range(c+1) range c+1 c 1 for i in range(c+1) [0]*4 [0] 0 4 dp = [[0]*4 for i in range(c+1)] dp [[0]*4 for i in range(c+1)] for i in range(r):
    ndp = [[0]*4 for i in range(c+1)]
    for j in range(c):
        M = max(dp[j+1])
        if l[i][j]>0:
            v = l[i][j]
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(M+v,dp[j][0]+v,ndp[j][0]+v,dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][1]+v,ndp[j][1]+v,dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][2]+v,ndp[j][2]+v,dp[j][3],ndp[j][3])
        else:
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][3],ndp[j][3])
    dp = ndp i range(r) range r [0]*4 for i in range(c+1) for i in range(c+1) i range(c+1) range c+1 c 1 for i in range(c+1) [0]*4 [0] 0 4 ndp = [[0]*4 for i in range(c+1)] ndp [[0]*4 for i in range(c+1)] for j in range(c):
        M = max(dp[j+1])
        if l[i][j]>0:
            v = l[i][j]
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(M+v,dp[j][0]+v,ndp[j][0]+v,dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][1]+v,ndp[j][1]+v,dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][2]+v,ndp[j][2]+v,dp[j][3],ndp[j][3])
        else:
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][3],ndp[j][3])
     j range(c) range c M = max(dp[j+1]) M max(dp[j+1]) max dp[j+1] dp j+1 j 1 if l[i][j]>0:
            v = l[i][j]
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(M+v,dp[j][0]+v,ndp[j][0]+v,dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][1]+v,ndp[j][1]+v,dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][2]+v,ndp[j][2]+v,dp[j][3],ndp[j][3])
        else:
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][3],ndp[j][3])
     l[i][j]>0 l[i][j] [i] l i j 0 v = l[i][j] v l[i][j] [i] l i j ndp[j+1][0] = max(M,dp[j][0],ndp[j][0]) ndp[j+1][0] [j+1] ndp j+1 j 1 0 max(M,dp[j][0],ndp[j][0]) max M dp[j][0] [j] dp j 0 ndp[j][0] [j] ndp j 0 ndp[j+1][1] = max(M+v,dp[j][0]+v,ndp[j][0]+v,dp[j][1],ndp[j][1]) ndp[j+1][1] [j+1] ndp j+1 j 1 1 max(M+v,dp[j][0]+v,ndp[j][0]+v,dp[j][1],ndp[j][1]) max M+v M v dp[j][0]+v dp[j][0] [j] dp j 0 v ndp[j][0]+v ndp[j][0] [j] ndp j 0 v dp[j][1] [j] dp j 1 ndp[j][1] [j] ndp j 1 ndp[j+1][2] = max(dp[j][1]+v,ndp[j][1]+v,dp[j][2],ndp[j][2]) ndp[j+1][2] [j+1] ndp j+1 j 1 2 max(dp[j][1]+v,ndp[j][1]+v,dp[j][2],ndp[j][2]) max dp[j][1]+v dp[j][1] [j] dp j 1 v ndp[j][1]+v ndp[j][1] [j] ndp j 1 v dp[j][2] [j] dp j 2 ndp[j][2] [j] ndp j 2 ndp[j+1][3] = max(dp[j][2]+v,ndp[j][2]+v,dp[j][3],ndp[j][3]) ndp[j+1][3] [j+1] ndp j+1 j 1 3 max(dp[j][2]+v,ndp[j][2]+v,dp[j][3],ndp[j][3]) max dp[j][2]+v dp[j][2] [j] dp j 2 v ndp[j][2]+v ndp[j][2] [j] ndp j 2 v dp[j][3] [j] dp j 3 ndp[j][3] [j] ndp j 3 ndp[j+1][0] = max(M,dp[j][0],ndp[j][0]) ndp[j+1][0] [j+1] ndp j+1 j 1 0 max(M,dp[j][0],ndp[j][0]) max M dp[j][0] [j] dp j 0 ndp[j][0] [j] ndp j 0 ndp[j+1][1] = max(dp[j][1],ndp[j][1]) ndp[j+1][1] [j+1] ndp j+1 j 1 1 max(dp[j][1],ndp[j][1]) max dp[j][1] [j] dp j 1 ndp[j][1] [j] ndp j 1 ndp[j+1][2] = max(dp[j][2],ndp[j][2]) ndp[j+1][2] [j+1] ndp j+1 j 1 2 max(dp[j][2],ndp[j][2]) max dp[j][2] [j] dp j 2 ndp[j][2] [j] ndp j 2 ndp[j+1][3] = max(dp[j][3],ndp[j][3]) ndp[j+1][3] [j+1] ndp j+1 j 1 3 max(dp[j][3],ndp[j][3]) max dp[j][3] [j] dp j 3 ndp[j][3] [j] ndp j 3 dp = ndp dp ndp print(max(dp[-1])) print max(dp[-1]) max dp[-1] dp -1 dp = ndp ndp dp dp = [[0]*4 for i in range(c+1)] [[0]*4 for i in range(c+1)] dp c,k = map(int,input().split()) map(int,input().split()) c l = [[0]*c for i in range(r)] [[0]*c for i in range(r)] l v = map(int,input().split()) map(int,input().split()) v k = map(int,input().split()) map(int,input().split()) k ndp = [[0]*4 for i in range(c+1)] [[0]*4 for i in range(c+1)] ndp v = l[i][j] l[i][j] v x,y,v = map(int,input().split()) map(int,input().split()) x input = sys.stdin.readline sys.stdin.readline input r,c,k = map(int,input().split()) map(int,input().split()) r y,v = map(int,input().split()) map(int,input().split()) y M = max(dp[j+1]) max(dp[j+1]) M
import sys
input = sys.stdin.readline
r,c,k = map(int,input().split())
l = [[0]*c for i in range(r)] 
for i in range(k):
    x,y,v = map(int,input().split())
    l[x-1][y-1] = v
dp = [[0]*4 for i in range(c+1)]

for i in range(r):
    ndp = [[0]*4 for i in range(c+1)]
    for j in range(c):
        M = max(dp[j+1])
        if l[i][j]>0:
            v = l[i][j]
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(M+v,dp[j][0]+v,ndp[j][0]+v,dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][1]+v,ndp[j][1]+v,dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][2]+v,ndp[j][2]+v,dp[j][3],ndp[j][3])
        else:
            ndp[j+1][0] = max(M,dp[j][0],ndp[j][0])
            ndp[j+1][1] = max(dp[j][1],ndp[j][1])
            ndp[j+1][2] = max(dp[j][2],ndp[j][2])
            ndp[j+1][3] = max(dp[j][3],ndp[j][3])
    dp = ndp
print(max(dp[-1]))