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)
 import sys sys int(x) for x in sys.stdin.read().split() for x in sys.stdin.read().split() x sys.stdin.read().split() sys.stdin.read().split sys.stdin.read() sys.stdin.read sys.stdin sys stdin read split for x in sys.stdin.read().split() int(x) int x inp = [int(x) for x in sys.stdin.read().split()] inp [int(x) for x in sys.stdin.read().split()] ii = 0 ii 0 r, c, k = inp[ii:ii+3] r inp[ii:ii+3] inp ii:ii+3 ii ii+3 ii 3 c k ii += 3 ii 3 v=[0] * (c * r) v [0] * (c * r) [0] 0 c * r c r for _ in range(k):
    ri,ci,a = inp[ii:ii+3]
    ii += 3
    v[(ri-1)*c + (ci-1)]=a _ range(k) range k ri,ci,a = inp[ii:ii+3] ri inp[ii:ii+3] inp ii:ii+3 ii ii+3 ii 3 ci a ii += 3 ii 3 v[(ri-1)*c + (ci-1)]=a v[(ri-1)*c + (ci-1)] v (ri-1)*c + (ci-1) (ri-1)*c ri-1 ri 1 c ci-1 ci 1 a dp= [0] * (c * r * 4) dp [0] * (c * r * 4) [0] 0 c * r * 4 c * r c r 4 x = c * r x c * r c r if v[0]>0: dp[x] = v[0] v[0]>0 v[0] v 0 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]) i range(r) 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]) j range(c) range c idx = i*c+j idx i*c+j i*c i c j idx2 = (i-1)*c + j idx2 (i-1)*c + j (i-1)*c i-1 i 1 c j idx3 = i*c + j-1 idx3 i*c + j-1 i*c + j i*c i c j 1 val = v[idx] val v[idx] 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

         i>0 i 0 a = max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2]) a max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2]) max dp[idx2] dp idx2 dp[x + idx2] dp x + idx2 x idx2 dp[2*x+idx2] dp 2*x+idx2 2*x 2 x idx2 dp[3*x+idx2] dp 3*x+idx2 3*x 3 x idx2 dp[idx]= a dp[idx] dp idx a dp[x+idx]= a + val dp[x+idx] dp x+idx x idx a + val 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]) j>0 j 0 dp[idx]=max(dp[idx],dp[idx3]) dp[idx] dp idx max(dp[idx],dp[idx3]) max dp[idx] dp idx dp[idx3] dp idx3 dp[x + idx]=max(dp[x + idx],dp[idx3]+val,dp[x + idx3]) dp[x + idx] dp x + idx x idx max(dp[x + idx],dp[idx3]+val,dp[x + idx3]) max dp[x + idx] dp x + idx x idx dp[idx3]+val dp[idx3] dp idx3 val dp[x + idx3] dp x + idx3 x idx3 dp[2*x + idx]=max(dp[x+idx3]+val,dp[2*x+idx3]) dp[2*x + idx] dp 2*x + idx 2*x 2 x idx max(dp[x+idx3]+val,dp[2*x+idx3]) max dp[x+idx3]+val dp[x+idx3] dp x+idx3 x idx3 val dp[2*x+idx3] dp 2*x+idx3 2*x 2 x idx3 dp[3*x + idx]=max(dp[2*x+idx3]+val,dp[3*x+idx3]) dp[3*x + idx] dp 3*x + idx 3*x 3 x idx max(dp[2*x+idx3]+val,dp[3*x+idx3]) max dp[2*x+idx3]+val dp[2*x+idx3] dp 2*x+idx3 2*x 2 x idx3 val dp[3*x+idx3] dp 3*x+idx3 3*x 3 x idx3 ans=0 ans 0 for i in range(4): ans = max(dp[i*x + (r-1)*c + (c-1)],ans) i range(4) range 4 print(ans) print ans ii += 3 3 ii v=[0] * (c * r) [0] * (c * r) v a = max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2]) max(dp[idx2],dp[x + idx2],dp[2*x+idx2],dp[3*x+idx2]) a c, k = inp[ii:ii+3] inp[ii:ii+3] c a = inp[ii:ii+3] inp[ii:ii+3] a idx3 = i*c + j-1 i*c + j-1 idx3 ci,a = inp[ii:ii+3] inp[ii:ii+3] ci idx2 = (i-1)*c + j (i-1)*c + j idx2 val = v[idx] v[idx] val dp= [0] * (c * r * 4) [0] * (c * r * 4) dp k = inp[ii:ii+3] inp[ii:ii+3] k ans=0 0 ans x = c * r c * r x idx = i*c+j i*c+j idx inp = [int(x) for x in sys.stdin.read().split()] [int(x) for x in sys.stdin.read().split()] inp r, c, k = inp[ii:ii+3] inp[ii:ii+3] r ii += 3 3 ii ii = 0 0 ii ri,ci,a = inp[ii:ii+3] inp[ii:ii+3] ri
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)

