import numpy as np
from numba import njit

rr,cc,kk=map(int,input().split())
goods=np.array([list(map(int,input().split())) for _ in range(kk)])
gg=np.zeros((rr,cc))
for cr,ccc,ck in goods:
    gg[cr-1][ccc-1]=ck

@njit
def calc(r,c,g):
    dp=-1*np.ones((r+1,c+1,4))
    maxlst=np.zeros((r+1,c+1))

    dp[:,0,0]=0
    dp[0,:,0]=0
    
    for i in range(r):
        for j in range(c):
            tmp=0
            for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
            dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
            tmp=max(tmp,dp[i+1][j+1][0])
            
            if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
            maxlst[i+1][j+1]=tmp
    
    return maxlst[r][c]
            
print(int(calc(rr,cc,gg))) import numpy as np numpy from numba import njit rr,cc,kk=map(int,input().split()) rr map(int,input().split()) map int input().split() ().split () input split cc kk list(map(int,input().split())) for _ in range(kk) for _ in range(kk) _ range(kk) range kk for _ in range(kk) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split goods=np.array([list(map(int,input().split())) for _ in range(kk)]) goods np.array([list(map(int,input().split())) for _ in range(kk)]) np.array np array [list(map(int,input().split())) for _ in range(kk)] gg=np.zeros((rr,cc)) gg np.zeros((rr,cc)) np.zeros np zeros (rr,cc) rr cc for cr,ccc,ck in goods:
    gg[cr-1][ccc-1]=ck cr ccc ck goods gg[cr-1][ccc-1]=ck gg[cr-1][ccc-1] [cr-1] gg cr-1 cr 1 ccc-1 ccc 1 ck def calc(r,c,g):
    dp=-1*np.ones((r+1,c+1,4))
    maxlst=np.zeros((r+1,c+1))

    dp[:,0,0]=0
    dp[0,:,0]=0
    
    for i in range(r):
        for j in range(c):
            tmp=0
            for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
            dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
            tmp=max(tmp,dp[i+1][j+1][0])
            
            if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
            maxlst[i+1][j+1]=tmp
    
    return maxlst[r][c]
             calc dp=-1*np.ones((r+1,c+1,4)) dp -1*np.ones((r+1,c+1,4)) -1 np.ones((r+1,c+1,4)) np.ones np ones (r+1,c+1,4) r+1 r 1 c+1 c 1 4 maxlst=np.zeros((r+1,c+1)) maxlst np.zeros((r+1,c+1)) np.zeros np zeros (r+1,c+1) r+1 r 1 c+1 c 1 dp[:,0,0]=0 dp[:,0,0] dp : 0 dp[0,:,0]=0 dp[0,:,0] dp 0 0 for i in range(r):
        for j in range(c):
            tmp=0
            for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
            dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
            tmp=max(tmp,dp[i+1][j+1][0])
            
            if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
            maxlst[i+1][j+1]=tmp
    
     i range(r) range r for j in range(c):
            tmp=0
            for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
            dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
            tmp=max(tmp,dp[i+1][j+1][0])
            
            if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
            maxlst[i+1][j+1]=tmp
    
     j range(c) range c tmp=0 tmp 0 for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
             k range(3) range 3 if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                 g[i][j]>0 and dp[i+1][j][k]>=0 g[i][j]>0 g[i][j] [i] g i j 0 dp[i+1][j][k]>=0 dp[i+1][j][k] [j] [i+1] dp i+1 i 1 j k 0 dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j]) dp[i+1][j+1][k+1] [j+1] [i+1] dp i+1 i 1 j+1 j 1 k+1 k 1 max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j]) max dp[i+1][j][k+1] [j] [i+1] dp i+1 i 1 j k+1 k 1 dp[i+1][j][k]+g[i][j] dp[i+1][j][k] [j] [i+1] dp i+1 i 1 j k g[i][j] [i] g i j dp[i+1][j+1][k+1]=dp[i+1][j][k+1] dp[i+1][j+1][k+1] [j+1] [i+1] dp i+1 i 1 j+1 j 1 k+1 k 1 dp[i+1][j][k+1] [j] [i+1] dp i+1 i 1 j k+1 k 1 tmp=max(tmp,dp[i+1][j+1][k+1]) tmp max(tmp,dp[i+1][j+1][k+1]) max tmp dp[i+1][j+1][k+1] [j+1] [i+1] dp i+1 i 1 j+1 j 1 k+1 k 1 dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0]) dp[i+1][j+1][0] [j+1] [i+1] dp i+1 i 1 j+1 j 1 0 max(maxlst[i][j+1],dp[i+1][j][0]) max maxlst[i][j+1] [i] maxlst i j+1 j 1 dp[i+1][j][0] [j] [i+1] dp i+1 i 1 j 0 tmp=max(tmp,dp[i+1][j+1][0]) tmp max(tmp,dp[i+1][j+1][0]) max tmp dp[i+1][j+1][0] [j+1] [i+1] dp i+1 i 1 j+1 j 1 0 if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
             g[i][j]>0 and dp[i+1][j+1][0]>=0 g[i][j]>0 g[i][j] [i] g i j 0 dp[i+1][j+1][0]>=0 dp[i+1][j+1][0] [j+1] [i+1] dp i+1 i 1 j+1 j 1 0 0 dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j]) dp[i+1][j+1][1] [j+1] [i+1] dp i+1 i 1 j+1 j 1 1 max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j]) max dp[i+1][j+1][1] [j+1] [i+1] dp i+1 i 1 j+1 j 1 1 dp[i+1][j+1][0]+g[i][j] dp[i+1][j+1][0] [j+1] [i+1] dp i+1 i 1 j+1 j 1 0 g[i][j] [i] g i j tmp=max(tmp,dp[i+1][j+1][1]) tmp max(tmp,dp[i+1][j+1][1]) max tmp dp[i+1][j+1][1] [j+1] [i+1] dp i+1 i 1 j+1 j 1 1 maxlst[i+1][j+1]=tmp maxlst[i+1][j+1] [i+1] maxlst i+1 i 1 j+1 j 1 tmp return maxlst[r][c] maxlst[r][c] [r] maxlst r c r r c c g g @njit calc njit calc print(int(calc(rr,cc,gg))) print int(calc(rr,cc,gg)) int calc(rr,cc,gg) calc rr cc gg cc,kk=map(int,input().split()) map(int,input().split()) cc def calc(r,c,g):
    dp=-1*np.ones((r+1,c+1,4))
    maxlst=np.zeros((r+1,c+1))

    dp[:,0,0]=0
    dp[0,:,0]=0
    
    for i in range(r):
        for j in range(c):
            tmp=0
            for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
            dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
            tmp=max(tmp,dp[i+1][j+1][0])
            
            if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
            maxlst[i+1][j+1]=tmp
    
    return maxlst[r][c]
             def calc(r,c,g):
    dp=-1*np.ones((r+1,c+1,4))
    maxlst=np.zeros((r+1,c+1))

    dp[:,0,0]=0
    dp[0,:,0]=0
    
    for i in range(r):
        for j in range(c):
            tmp=0
            for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
            dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
            tmp=max(tmp,dp[i+1][j+1][0])
            
            if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
            maxlst[i+1][j+1]=tmp
    
    return maxlst[r][c]
             calc gg=np.zeros((rr,cc)) np.zeros((rr,cc)) gg kk=map(int,input().split()) map(int,input().split()) kk goods=np.array([list(map(int,input().split())) for _ in range(kk)]) np.array([list(map(int,input().split())) for _ in range(kk)]) goods rr,cc,kk=map(int,input().split()) map(int,input().split()) rr
import numpy as np
from numba import njit

rr,cc,kk=map(int,input().split())
goods=np.array([list(map(int,input().split())) for _ in range(kk)])
gg=np.zeros((rr,cc))
for cr,ccc,ck in goods:
    gg[cr-1][ccc-1]=ck

@njit
def calc(r,c,g):
    dp=-1*np.ones((r+1,c+1,4))
    maxlst=np.zeros((r+1,c+1))

    dp[:,0,0]=0
    dp[0,:,0]=0
    
    for i in range(r):
        for j in range(c):
            tmp=0
            for k in range(3):
                if g[i][j]>0 and dp[i+1][j][k]>=0:
                    dp[i+1][j+1][k+1]=max(dp[i+1][j][k+1],dp[i+1][j][k]+g[i][j])
                else:
                    dp[i+1][j+1][k+1]=dp[i+1][j][k+1]
                tmp=max(tmp,dp[i+1][j+1][k+1])
            
            dp[i+1][j+1][0]=max(maxlst[i][j+1],dp[i+1][j][0])
            tmp=max(tmp,dp[i+1][j+1][0])
            
            if g[i][j]>0 and dp[i+1][j+1][0]>=0:
                dp[i+1][j+1][1]=max(dp[i+1][j+1][1],dp[i+1][j+1][0]+g[i][j])
                tmp=max(tmp,dp[i+1][j+1][1])
            
            maxlst[i+1][j+1]=tmp
    
    return maxlst[r][c]
            
print(int(calc(rr,cc,gg)))