R,C,K=map(int,input().split())
rcv = [[0]*C for _ in range(R)]
for i in range(K):
    r,c,v = map(int,input().split())
    rcv[r-1][c-1]=v

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

dp[1][0][0]=rcv[0][0]

#左端を縦に
j=0
for i in range(1,R):
    if rcv[i][j]==0:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
    else:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
        dp[1][i][j] = max(dp[r][i-1][j] for r in range(4))+rcv[i][j]

#上端を右に 
i=0
for j in range(1,C):
    if rcv[i][j]==0:
        for r in range(4):#左から引き継ぐだけ
            dp[r][i][j] = dp[r][i][j-1]
    else:
        dp[0][i][j] = dp[0][i][j-1]
        for r in range(1,4):
            dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j])

for i in range(1,R):
    for j in range(1,C):
        if rcv[i][j]==0:#左から引き継ぐか、上から引き継いでカウントキャンセル
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
        else:
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] )
            for r in [2,3]:#左からのみ
                dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] )
print(max( dp[i][-1][-1] for i in range(4)) ) R,C,K=map(int,input().split()) R map(int,input().split()) map int input().split() ().split () input split C K [0]*C for _ in range(R) for _ in range(R) _ range(R) range R for _ in range(R) [0]*C [0] 0 C rcv = [[0]*C for _ in range(R)] rcv [[0]*C for _ in range(R)] for i in range(K):
    r,c,v = map(int,input().split())
    rcv[r-1][c-1]=v i range(K) range K r,c,v = map(int,input().split()) r map(int,input().split()) map int input().split() ().split () input split c v rcv[r-1][c-1]=v rcv[r-1][c-1] [r-1] rcv r-1 r 1 c-1 c 1 v [[0]*C for _ in range(R)] for _ in range(0,4) for _ in range(0,4) _ range(0,4) range 0 4 for _ in range(0,4) [[0]*C for _ in range(R)] dp = [[[0]*C for _ in range(R)] for _ in range(0,4)] dp [[[0]*C for _ in range(R)] for _ in range(0,4)] dp[1][0][0]=rcv[0][0] dp[1][0][0] [0] [1] dp 1 0 0 rcv[0][0] [0] rcv 0 0 j=0 j 0 for i in range(1,R):
    if rcv[i][j]==0:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
    else:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
        dp[1][i][j] = max(dp[r][i-1][j] for r in range(4))+rcv[i][j]

#上端を右に  i range(1,R) range 1 R if rcv[i][j]==0:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
    else:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
        dp[1][i][j] = max(dp[r][i-1][j] for r in range(4))+rcv[i][j]

#上端を右に  rcv[i][j]==0 rcv[i][j] [i] rcv i j 0 dp[0][i][j] = max(dp[r][i-1][j] for r in range(4)) dp[0][i][j] [i] [0] dp 0 i j max(dp[r][i-1][j] for r in range(4)) max dp[r][i-1][j] [i-1] [r] dp r i-1 i 1 j dp[0][i][j] = max(dp[r][i-1][j] for r in range(4)) dp[0][i][j] [i] [0] dp 0 i j max(dp[r][i-1][j] for r in range(4)) max dp[r][i-1][j] [i-1] [r] dp r i-1 i 1 j dp[1][i][j] = max(dp[r][i-1][j] for r in range(4))+rcv[i][j] dp[1][i][j] [i] [1] dp 1 i j max(dp[r][i-1][j] for r in range(4))+rcv[i][j] max(dp[r][i-1][j] for r in range(4)) max dp[r][i-1][j] [i-1] [r] dp r i-1 i 1 j rcv[i][j] [i] rcv i j i=0 i 0 for j in range(1,C):
    if rcv[i][j]==0:
        for r in range(4):#左から引き継ぐだけ
            dp[r][i][j] = dp[r][i][j-1]
    else:
        dp[0][i][j] = dp[0][i][j-1]
        for r in range(1,4):
            dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j]) j range(1,C) range 1 C if rcv[i][j]==0:
        for r in range(4):#左から引き継ぐだけ
            dp[r][i][j] = dp[r][i][j-1]
    else:
        dp[0][i][j] = dp[0][i][j-1]
        for r in range(1,4):
            dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j]) rcv[i][j]==0 rcv[i][j] [i] rcv i j 0 for r in range(4):#左から引き継ぐだけ
            dp[r][i][j] = dp[r][i][j-1]
     r range(4) range 4 dp[r][i][j] = dp[r][i][j-1] dp[r][i][j] [i] [r] dp r i j dp[r][i][j-1] [i] [r] dp r i j-1 j 1 dp[0][i][j] = dp[0][i][j-1] dp[0][i][j] [i] [0] dp 0 i j dp[0][i][j-1] [i] [0] dp 0 i j-1 j 1 for r in range(1,4):
            dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j]) r range(1,4) range 1 4 dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j]) dp[r][i][j] [i] [r] dp r i j max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j]) max dp[r][i][j-1] [i] [r] dp r i j-1 j 1 dp[r-1][i][j-1]+rcv[i][j] dp[r-1][i][j-1] [i] [r-1] dp r-1 r 1 i j-1 j 1 rcv[i][j] [i] rcv i j for i in range(1,R):
    for j in range(1,C):
        if rcv[i][j]==0:#左から引き継ぐか、上から引き継いでカウントキャンセル
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
        else:
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] )
            for r in [2,3]:#左からのみ
                dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] ) i range(1,R) range 1 R for j in range(1,C):
        if rcv[i][j]==0:#左から引き継ぐか、上から引き継いでカウントキャンセル
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
        else:
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] )
            for r in [2,3]:#左からのみ
                dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] ) j range(1,C) range 1 C if rcv[i][j]==0:#左から引き継ぐか、上から引き継いでカウントキャンセル
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
        else:
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] )
            for r in [2,3]:#左からのみ
                dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] ) rcv[i][j]==0 rcv[i][j] [i] rcv i j 0 dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) ) dp[0][i][j] [i] [0] dp 0 i j max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) ) max dp[0][i][j-1] [i] [0] dp 0 i j-1 j 1 max(dp[r][i-1][j] for r in range(4) ) max dp[r][i-1][j] [i-1] [r] dp r i-1 i 1 j for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
         r range(1,4) range 1 4 dp[r][i][j] = dp[r][i][j-1] dp[r][i][j] [i] [r] dp r i j dp[r][i][j-1] [i] [r] dp r i j-1 j 1 dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) ) dp[0][i][j] [i] [0] dp 0 i j max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) ) max dp[0][i][j-1] [i] [0] dp 0 i j-1 j 1 max(dp[r][i-1][j] for r in range(4) ) max dp[r][i-1][j] [i-1] [r] dp r i-1 i 1 j dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] ) dp[1][i][j] [i] [1] dp 1 i j max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] ) max dp[1][i][j-1] [i] [1] dp 1 i j-1 j 1 dp[0][i][j-1]+rcv[i][j] dp[0][i][j-1] [i] [0] dp 0 i j-1 j 1 rcv[i][j] [i] rcv i j max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] max(dp[r][i-1][j] for r in range(4) ) max dp[r][i-1][j] [i-1] [r] dp r i-1 i 1 j rcv[i][j] [i] rcv i j for r in [2,3]:#左からのみ
                dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] ) r [2,3] 2 3 dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] ) dp[r][i][j] [i] [r] dp r i j max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] ) max dp[r][i][j-1] [i] [r] dp r i j-1 j 1 dp[r-1][i][j-1]+rcv[i][j] dp[r-1][i][j-1] [i] [r-1] dp r-1 r 1 i j-1 j 1 rcv[i][j] [i] rcv i j print(max( dp[i][-1][-1] for i in range(4)) ) print max( dp[i][-1][-1] for i in range(4)) max dp[i][-1][-1] [-1] [i] dp i -1 -1 i=0 0 i dp = [[[0]*C for _ in range(R)] for _ in range(0,4)] [[[0]*C for _ in range(R)] for _ in range(0,4)] dp j=0 0 j K=map(int,input().split()) map(int,input().split()) K rcv = [[0]*C for _ in range(R)] [[0]*C for _ in range(R)] rcv r,c,v = map(int,input().split()) map(int,input().split()) r C,K=map(int,input().split()) map(int,input().split()) C R,C,K=map(int,input().split()) map(int,input().split()) R c,v = map(int,input().split()) map(int,input().split()) c v = map(int,input().split()) map(int,input().split()) v
R,C,K=map(int,input().split())
rcv = [[0]*C for _ in range(R)]
for i in range(K):
    r,c,v = map(int,input().split())
    rcv[r-1][c-1]=v

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

dp[1][0][0]=rcv[0][0]

#左端を縦に
j=0
for i in range(1,R):
    if rcv[i][j]==0:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
    else:
        dp[0][i][j] = max(dp[r][i-1][j] for r in range(4))
        dp[1][i][j] = max(dp[r][i-1][j] for r in range(4))+rcv[i][j]

#上端を右に 
i=0
for j in range(1,C):
    if rcv[i][j]==0:
        for r in range(4):#左から引き継ぐだけ
            dp[r][i][j] = dp[r][i][j-1]
    else:
        dp[0][i][j] = dp[0][i][j-1]
        for r in range(1,4):
            dp[r][i][j] = max(dp[r][i][j-1] , dp[r-1][i][j-1]+rcv[i][j])

for i in range(1,R):
    for j in range(1,C):
        if rcv[i][j]==0:#左から引き継ぐか、上から引き継いでカウントキャンセル
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            for r in range(1,4):#左から引き継ぐだけ
                dp[r][i][j] = dp[r][i][j-1]
        else:
            dp[0][i][j] = max(dp[0][i][j-1],
                              max(dp[r][i-1][j] for r in range(4) ) )
            dp[1][i][j] = max(dp[1][i][j-1], dp[0][i][j-1]+rcv[i][j],
                              max(dp[r][i-1][j] for r in range(4) )+rcv[i][j] )
            for r in [2,3]:#左からのみ
                dp[r][i][j] = max(dp[r][i][j-1], dp[r-1][i][j-1]+rcv[i][j] )
print(max( dp[i][-1][-1] for i in range(4)) )