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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(eval(input()))
def FI(): return int(eval(input()))
def MI(): return list(map(int,input().split()))
def MF(): return list(map(float,input().split()))
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	r,c,k=MI()
	G=[[0]*(c+1) for _ in range(r+1)]
	dp=[[[0]*5 for i in range(c+1)] for j in range(r+1)]
	for _ in range(k):
		rrr,ccc,v=MI()
		G[rrr][ccc]=v
	#print(G)

	#print(dp)

	for rr in range(1,r+1):
		for cc in range(1,c+1):
			if G[rr][cc]==0:
				dp[rr][cc][1]=dp[rr][cc-1][1]
				dp[rr][cc][2]=dp[rr][cc-1][2]
				dp[rr][cc][3]=dp[rr][cc-1][3]
				dp[rr][cc][4]=max(dp[rr][cc-1][4],dp[rr-1][cc][4])
			else:
				dp[rr][cc][1]=max(dp[rr-1][cc][4]+G[rr][cc],dp[rr][cc-1][1])
				dp[rr][cc][2]=max(dp[rr][cc-1][1]+G[rr][cc],dp[rr][cc-1][2])
				dp[rr][cc][3]=max(dp[rr][cc-1][2]+G[rr][cc],dp[rr][cc-1][3])
				dp[rr][cc][4]=max(dp[rr][cc][1:4])
	#print(dp)
	print((dp[-1][-1][-1]))















if __name__ == "__main__":
	main()


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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(eval(input()))
def FI(): return int(eval(input()))
def MI(): return list(map(int,input().split()))
def MF(): return list(map(float,input().split()))
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	r,c,k=MI()
	G=[[0]*(c+1) for _ in range(r+1)]
	dp=[[[0]*4 for i in range(c+1)] for j in range(r+1)]
	for _ in range(k):
		rrr,ccc,v=MI()
		G[rrr][ccc]=v
	#print(G)

	#print(dp)

	for rr in range(1,r+1):
		for cc in range(1,c+1):
			if G[rr][cc]==0:
				for i in range(4):
					dp[rr][cc][i]=max(dp[rr][cc-1][i],dp[rr-1][cc][3])
			else:
				dp[rr][cc][0]=max(dp[rr-1][cc][3],dp[rr][cc-1][0])
				dp[rr][cc][1]=max(dp[rr][cc][0]+G[rr][cc],dp[rr][cc-1][1])
				dp[rr][cc][2]=max(dp[rr][cc-1][1]+G[rr][cc],dp[rr][cc-1][2],dp[rr][cc][1])
				dp[rr][cc][3]=max(dp[rr][cc-1][2]+G[rr][cc],dp[rr][cc-1][3],dp[rr][cc][2])
	#print(dp)
	print((dp[-1][-1][-1]))















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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(eval(input()))
def FI(): return int(eval(input()))
def MI(): return list(map(int,input().split()))
def MF(): return list(map(float,input().split()))
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	r,c,k=MI()
	G=[[0]*(c+1) for _ in range(r+1)]
	dp=[[[0]*(c+1) for i in range(r+1)] for j in range(4)]
	for _ in range(k):
		rrr,ccc,v=MI()
		G[rrr][ccc]=v
	#print(G)

	#print(dp)

	for rr in range(1,r+1):
		for cc in range(1,c+1):
			if G[rr][cc]==0:
				for i in range(4):
					dp[i][rr][cc]=max(dp[i][rr][cc-1],dp[3][rr-1][cc])
			else:
				dp[0][rr][cc]=max(dp[3][rr-1][cc],dp[0][rr][cc-1])
				dp[1][rr][cc]=max(dp[0][rr][cc]+G[rr][cc],dp[1][rr][cc-1])
				dp[2][rr][cc]=max(dp[1][rr][cc-1]+G[rr][cc],dp[2][rr][cc-1],dp[1][rr][cc])
				dp[3][rr][cc]=max(dp[2][rr][cc-1]+G[rr][cc],dp[3][rr][cc-1],dp[2][rr][cc])
	#print(dp)
	print((dp[-1][-1][-1]))















if __name__ == "__main__":
	main()


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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(eval(input()))
def FI(): return int(eval(input()))
def MI(): return list(map(int,input().split()))
def MF(): return list(map(float,input().split()))
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	r,c,k=MI()
	G=[[0]*(c+1) for _ in range(r+1)]
	dp=[[0]*(c+1) for j in range(4)]
	for _ in range(k):
		rrr,ccc,v=MI()
		G[rrr][ccc]=v
	#print(G)

	#print(dp)

	for rr in range(1,r+1):
		for cc in range(1,c+1):
			if G[rr][cc]==0:
				for i in range(4):
					dp[i][cc]=max(dp[i][cc-1],dp[3][cc])
			else:
				dp[0][cc]=max(dp[3][cc],dp[0][cc-1])
				dp[1][cc]=max(dp[0][cc]+G[rr][cc],dp[1][cc-1])
				dp[2][cc]=max(dp[1][cc-1]+G[rr][cc],dp[2][cc-1],dp[1][cc])
				dp[3][cc]=max(dp[2][cc-1]+G[rr][cc],dp[3][cc-1],dp[2][cc])
	#print(dp)
	print((dp[-1][-1]))















if __name__ == "__main__":
	main()
# Original solution
import sys

input=sys.stdin.readline

R,C,K=list(map(int,input().split()))
item={}
for i in range(K):
    r,c,v=list(map(int,input().split()))
    item[r-1,c-1]=0
    item[r-1,c-1]=v

dp=[[0 for i in range(3)] for j in range(C)]

if (R-1,C-1) in item:
    dp[C-1][0]=item[R-1,C-1]
    dp[C-1][1]=item[R-1,C-1]
    dp[C-1][2]=item[R-1,C-1]

for j in range(C-2,-1,-1):
    if (R-1,j) in item:
        dp[j][0]=max(dp[j+1][1]+item[R-1,j],dp[j+1][0])
        dp[j][1]=max(dp[j+1][2]+item[R-1,j],dp[j+1][1])
        dp[j][2]=max(item[R-1,j],dp[j+1][2])
    else:
        dp[j][0]=dp[j+1][0]
        dp[j][1]=dp[j+1][1]
        dp[j][2]=dp[j+1][2]

for i in range(R-2,-1,-1):
    ndp=[[0 for i in range(3)] for j in range(C)]
    if (i,C-1) in item:
        ndp[C-1][0]=dp[C-1][0]+item[i,C-1]
        ndp[C-1][1]=dp[C-1][0]+item[i,C-1]
        ndp[C-1][2]=dp[C-1][0]+item[i,C-1]
    else:
        ndp[C-1][0]=dp[C-1][0]
        ndp[C-1][1]=dp[C-1][0]
        ndp[C-1][2]=dp[C-1][0]
    for j in range(C-2,-1,-1):
        if (i,j) in item:
            ndp[j][0]=max(max(dp[j][0],ndp[j+1][1])+item[i,j],ndp[j+1][0])
            ndp[j][1]=max(max(dp[j][0],ndp[j+1][2])+item[i,j],ndp[j+1][1])
            ndp[j][2]=max(dp[j][0]+item[i,j],ndp[j+1][2])
        else:
            ndp[j][0]=max(dp[j][0],ndp[j+1][0])
            ndp[j][1]=max(dp[j][0],ndp[j+1][1])
            ndp[j][2]=max(dp[j][0],ndp[j+1][2])
    dp=ndp

print((dp[0][0]))


# Optimized solution
import sys

input=sys.stdin.readline

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

dp=[[0 for i in range(3)] for j in range(C)]
dp[C-1][0]=item[R-1][C-1]
dp[C-1][1]=item[R-1][C-1]
dp[C-1][2]=item[R-1][C-1]

for j in range(C-2,-1,-1):
    dp[j][0]=max(dp[j+1][1]+item[R-1][j],dp[j+1][0])
    dp[j][1]=max(dp[j+1][2]+item[R-1][j],dp[j+1][1])
    dp[j][2]=max(item[R-1][j],dp[j+1][2])


for i in range(R-2,-1,-1):
    ndp=[[0 for i in range(3)] for j in range(C)]
    ndp[C-1][0]=dp[C-1][0]+item[i][C-1]
    ndp[C-1][1]=dp[C-1][0]+item[i][C-1]
    ndp[C-1][2]=dp[C-1][0]+item[i][C-1]
    for j in range(C-2,-1,-1):
        ndp[j][0]=max(max(dp[j][0],ndp[j+1][1])+item[i][j],ndp[j+1][0])
        ndp[j][1]=max(max(dp[j][0],ndp[j+1][2])+item[i][j],ndp[j+1][1])
        ndp[j][2]=max(dp[j][0]+item[i][j],ndp[j+1][2])
    dp=ndp

print((dp[0][0]))
# Original solution
import sys
input = sys.stdin.readline

def main():
    r,c,k = list(map(int, input().split()))
    rcv = [ [0]*c for _ in range(r) ]

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

    # dp[0] = [ [0]*c for _ in range(r) ]
    # dp1 = [ [0]*c for _ in range(r) ]
    # dp2 = [ [0]*c for _ in range(r) ]
    # dp[3] = [ [0]*c for _ in range(r) ]
    dp = [ [ [0]*c  for _ in range(r) ] for _ in range(4) ]


    dp[1][0][0] = rcv[0][0]
    for i in range(r):
        for j in range(c):
            if i+1 < r:
                dp[0][i+1][j] = max(dp[0][i][j], dp[1][i][j], dp[2][i][j], dp[3][i][j], dp[0][i+1][j])
                if rcv[i+1][j] > 0:
                    dp[1][i+1][j] = max(dp[1][i+1][j],  max(dp[0][i][j], dp[1][i][j], dp[2][i][j], dp[3][i][j])+rcv[i+1][j])

            if j+1 < c:
                dp[0][i][j+1] = max(dp[0][i][j], dp[0][i][j+1])
                dp[1][i][j+1] = max(dp[1][i][j], dp[1][i][j+1])
                dp[2][i][j+1] = max(dp[2][i][j], dp[2][i][j+1])
                dp[3][i][j+1] = max(dp[3][i][j], dp[3][i][j+1])
                if rcv[i][j+1] > 0:
                    dp[1][i][j+1] = max(dp[1][i][j+1], dp[0][i][j]+rcv[i][j+1])
                    dp[2][i][j+1] = max(dp[2][i][j+1], dp[1][i][j]+rcv[i][j+1])
                    dp[3][i][j+1] = max(dp[3][i][j+1], dp[2][i][j]+rcv[i][j+1])

    ans = max(dp[0][r-1][c-1], dp[1][r-1][c-1], dp[2][r-1][c-1], dp[3][r-1][c-1])
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
r,c,k = list(map(int, input().split()))
rckl = [ [0]*c for _ in range(r) ]

for _ in range(k):
    r_,c_,v_ = list(map(int, input().split()))
    rckl[r_-1][c_-1] = v_

dp0 = [ [0]*c for _ in range(r) ]
dp1 = [ [0]*c for _ in range(r) ]
dp2 = [ [0]*c for _ in range(r) ]
dp3 = [ [0]*c for _ in range(r) ]


dp1[0][0] = rckl[0][0]

for i in range(r):
    for j in range(c):
        if j+1 < c: 
            dp0[i][j+1] = max(dp0[i][j], dp0[i][j+1]) 
            dp1[i][j+1] = max(dp1[i][j], dp1[i][j+1]) 
            dp2[i][j+1] = max(dp2[i][j], dp2[i][j+1]) 
            dp3[i][j+1] = max(dp3[i][j], dp3[i][j+1]) 
            if rckl[i][j+1] > 0:
                v = rckl[i][j+1] 
                dp1[i][j+1] = max(dp1[i][j+1], dp0[i][j] + v)
                dp2[i][j+1] = max(dp2[i][j+1], dp1[i][j] + v)
                dp3[i][j+1] = max(dp3[i][j+1], dp2[i][j] + v)

        if i+1 < r: 
            dp0[i+1][j] = max(dp0[i][j], dp1[i][j], dp2[i][j], dp3[i][j], dp0[i+1][j]) 
            if rckl[i+1][j] > 0:
                v = rckl[i+1][j] 
                dp1[i+1][j] = max(dp1[i+1][j], dp0[i][j]+v, dp1[i][j]+v, dp2[i][j]+v, dp3[i][j]+v)


ans = max(dp0[-1][-1], dp1[-1][-1], dp2[-1][-1], dp3[-1][-1])
print(ans)
# Original solution
import sys
input = lambda: sys.stdin.readline().rstrip()
r, c, k = list(map(int, input().split()))
dp = [[[0 for _ in range(3)] for _ in range(c + 5)] for _ in range(r + 5)]
pt = [[0 for _ in range(c + 5)] for _ in range(r + 5)]

for i in range(k):
    r1, c1, v = list(map(int, input().split()))
    dp[r1][c1][0] = v
    pt[r1][c1] = v

for i in range(1, r + 1):
    for j in range(1, c + 1):
        for k in range(3):
            if k == 0:
                dp[i][j][k] = max(dp[i - 1][j][2] + pt[i][j], dp[i][j - 1][0])
            elif k == 1:
                dp[i][j][k] = max(dp[i][j - 1][0] + pt[i][j],
                                  max(dp[i][j - 1][1], dp[i][j][0]))
            else:
                dp[i][j][k] = max(dp[i][j - 1][1] + pt[i][j],
                                  max(dp[i][j - 1][2], dp[i][j][1]))

print((dp[r][c][2]))

# デバッグ用
# for i in dp:
#     print(i)


# Optimized solution
import sys
# import time
input = lambda: sys.stdin.readline().rstrip()
r, c, k = list(map(int, input().split()))
# dp = [[[0] * 3 for _ in range(c + 5)] for _ in range(r + 5)]
pt = [[0 for _ in range(c + 5)] for _ in range(r + 5)]

for i in range(k):
    r1, c1, v = list(map(int, input().split()))
    # dp[r1][c1][0] = v
    pt[r1][c1] = v

# start = time.time()
a = 0


def solve():
    memo = [[0] * 4 for _ in range(c + 1)]
    prev = [[0] * 4 for _ in range(c + 1)]
    for i in range(1, r + 1):
        memo, prev = prev, memo
        for j in range(1, c + 1):
            V = pt[i][j]
            memo[j][0] = max(prev[j][3], memo[j - 1][0])
            memo[j][1] = max(memo[j][0] + V, memo[j - 1][1])
            memo[j][2] = max(memo[j][1], memo[j - 1][1] + V, memo[j - 1][2])
            memo[j][3] = max(memo[j][2], memo[j - 1][2] + V, memo[j - 1][3])

            # dp[i][j][0] = max(dp[i - 1][j][2] + V, dp[i][j - 1][0])
            # dp[i][j][1] = max(dp[i][j - 1][0] + V, dp[i][j - 1][1],
            #                   dp[i][j][0])
            # dp[i][j][2] = max(dp[i][j - 1][1] + V, dp[i][j - 1][2],
            #                   dp[i][j][1])

    # process_time = time.time() - start

    # print(process_time)

    # print(dp[r][c][2])
    return memo[-1][3]





if __name__ == '__main__':
    print((solve()))
# Original solution
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

R, C, K = list(map(int, readline().split()))

data = [[0]*(C+1) for _ in range(R+1)]
for i in range(K):
    r,c,v = list(map(int,readline().split()))
    data[r][c] = v

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

dp[1][1][1] = data[1][1]

for i in range(1,R+1):
    for j in range(1,C+1):
        for k in range(4):
            if i+1 < R+1:
                dp[i+1][j][0] = max(dp[i+1][j][0],dp[i][j][k])
                dp[i+1][j][1] = max(dp[i+1][j][1],dp[i][j][k]+data[i+1][j])     
            if j+1 < C+1:
                dp[i][j+1][k] = max(dp[i][j][k],dp[i][j+1][k])
                if k+1 <= 3:
                    dp[i][j+1][k+1] = max(dp[i][j+1][k+1],dp[i][j][k]+data[i][j+1])
            
print((max(dp[i][j])))

# Optimized solution
# -*- coding: utf-8 -*-
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

R, C, K = list(map(int, readline().split()))

V = [[0]*(C+1) for _ in range(R+1)]
for i in range(K):
    r,c,v = list(map(int,readline().split()))
    V[r][c] = v

dp = [0] * (C+1)
dp[0] = 0

for i in range(1,R+1):
    ndp = [0] * (C+1)
    row = V[i]
    ldp = [0] * 4
    for j in range(1,C+1):
        # ldp[0] = max(ldp[0], dp[j])
        ldp[0] = dp[j]
        for k in range(2,-1,-1):
            ldp[k+1] = max(ldp[k+1], ldp[k] + row[j]) # 取る場合と取らない場合
        ndp[j] = max(ldp)
    dp = ndp
print((dp[C]))
# Original solution
R, C, K = list(map(int, input().split()))

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

# print(*V,sep='\n',end='\n\n')

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

# for r in dp:
#     for c in r:
#         print('[',end='')
#         for x in c:
#             print(x,end=' ')
#         print(']',end='')
#     print()
# print()

for i in range(R+1):
    for j in range(C+1):
        for k in range(3+1):
            if i-1 >= 0:
                dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][k])
                dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][k] + V[i][j])

            if j-1 >= 0:
                dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
                if k-1 >= 0:
                    dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + V[i][j])

# for r in dp:
#     for c in r:
#         print('[',end='')
#         for x in c:
#             print(x,end=' ')
#         print(']',end='')
#     print()
# print()

print((max(dp[R][C])))


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

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

pre = [[0]*(3+1) for _ in range(C+1)]
for i in range(R+1):
    now = [[0]*(3+1) for _ in range(C+1)]
    for j in range(C+1):
        for k in range(3+1):
            if i-1 >= 0:
                now[j][0] = max(now[j][0], pre[j][k])
                now[j][1] = max(now[j][1], pre[j][k] + V[i][j])

            if j-1 >= 0:
                now[j][k] = max(now[j][k], now[j-1][k])
                if k-1 >= 0:
                    now[j][k] = max(now[j][k], now[j-1][k-1] + V[i][j])
    pre = now

print((max(now[C])))
# Original solution
R, C, K = list(map(int, input().split()))

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

dp0 = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0]*(C+1) for _ in range(R+1)]
dp2 = [[0]*(C+1) for _ in range(R+1)]
dp3 = [[0]*(C+1) for _ in range(R+1)]
for i in range(R+1):
    for j in range(C+1):
        v = V[i][j]

        a = dp0[i][j]
        b = dp0[i][j]
        c = dp0[i][j]
        d = dp0[i][j]

        pa = pb = pc = pd = 0
        if i > 0:
            pa = dp0[i-1][j]
            pb = dp1[i-1][j]
            pc = dp2[i-1][j]
            pd = dp3[i-1][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

        a = max(a, pa)
        b = max(b, pa+v)
        a = max(a, la)

        a = max(a, pb)
        b = max(b, pb+v)
        b = max(b, lb, la+v)

        a = max(a, pc)
        b = max(b, pc+v)
        c = max(c, lc, lb+v)

        a = max(a, pd)
        b = max(b, pd+v)
        d = max(d, ld, lc+v)

        dp0[i][j] = a
        dp1[i][j] = b
        dp2[i][j] = c
        dp3[i][j] = d

print((max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])))


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

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

dp0 = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0]*(C+1) for _ in range(R+1)]
dp2 = [[0]*(C+1) for _ in range(R+1)]
dp3 = [[0]*(C+1) for _ in range(R+1)]
for i in range(R+1):
    for j in range(C+1):
        v = V[i][j]

        max_p = 0
        if i > 0:
            max_p = max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j])

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

        dp0[i][j] = max(dp0[i][j], max_p, la)
        dp1[i][j] = max(dp0[i][j], max_p + v, lb, la + v)
        dp2[i][j] = max(dp2[i][j], lc, lb + v)
        dp3[i][j] = max(dp3[i][j], ld, lc + v)


print((max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])))
# Original solution
R, C, K = list(map(int, input().split()))

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

dp0 = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0]*(C+1) for _ in range(R+1)]
dp2 = [[0]*(C+1) for _ in range(R+1)]
dp3 = [[0]*(C+1) for _ in range(R+1)]
for i in range(R+1):
    for j in range(C+1):
        v = V[i][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]
            dp2[i][j] = max(dp2[i][j], lc, lb + v)
            dp3[i][j] = max(dp3[i][j], ld, lc + v)

        if i > 0:
            max_p = max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j])
            dp0[i][j] = max(dp0[i][j], max_p, la)
            dp1[i][j] = max(dp0[i][j], max_p + v, lb, la + v)

print((max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])))


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

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

dp0 = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0]*(C+1) for _ in range(R+1)]
dp2 = [[0]*(C+1) for _ in range(R+1)]
dp3 = [[0]*(C+1) for _ in range(R+1)]
for i in range(R+1):
    for j in range(C+1):
        v = V[i][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]
            dp2[i][j] = max(dp2[i][j], lc, lb + v)
            dp3[i][j] = max(dp3[i][j], ld, lc + v)

        if i > 0:
            max_p = max(dp0[i-1][j], dp1[i-1][j], dp2[i-1][j], dp3[i-1][j])
            dp0[i][j] = max(dp0[i][j], max_p, la)
            dp1[i][j] = max(dp1[i][j], max_p + v, lb, la + v)

print((max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])))
# Original solution
R, C, K = list(map(int, input().split()))

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

pre0 = [0]*(C+1)
pre1 = [0]*(C+1)
pre2 = [0]*(C+1)
pre3 = [0]*(C+1)
for i in range(R+1):
    now0 = [0]*(C+1)
    now1 = [0]*(C+1)
    now2 = [0]*(C+1)
    now3 = [0]*(C+1)
    for j in range(C+1):
        v = V[i][j]
        la = lb = 0
        if j > 0:
            la = now0[j-1]
            lb = now1[j-1]
            now2[j] = max(now2[j], now2[j-1], lb + v)
            now3[j] = max(now3[j], now3[j-1], now2[j-1] + v)
        if i > 0:
            max_p = max(pre0[j], pre1[j], pre2[j], pre3[j])
            now0[j] = max(now0[j], max_p, la)
            now1[j] = max(now1[j], max_p + v, la + v, lb)
    pre0 = now0
    pre1 = now1
    pre2 = now2
    pre3 = now3

print((max(now0[C], now1[C], now2[C], now3[C])))


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

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

max_p = [0]*(C+1)
for i in range(R+1):
    now0 = [0]*(C+1)
    now1 = [0]*(C+1)
    now2 = [0]*(C+1)
    now3 = [0]*(C+1)
    for j in range(C+1):
        v = V[i][j]
        la = lb = 0
        if j > 0:
            la = now0[j-1]
            lb = now1[j-1]
            now2[j] = max(now2[j], now2[j-1], lb + v)
            now3[j] = max(now3[j], now3[j-1], now2[j-1] + v)
        if i > 0:
            now0[j] = max(now0[j], max_p[j], la)
            now1[j] = max(now1[j], max_p[j] + v, la + v, lb)
        max_p[j] = max(now0[j], now1[j], now2[j], now3[j])

print((max(now0[C], now1[C], now2[C], now3[C])))
# Original solution
R, C, K = list(map(int, input().split()))

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

max_p = [0]*(C+1)
for i in range(R+1):
    now0 = [0]*(C+1)
    now1 = [0]*(C+1)
    now2 = [0]*(C+1)
    now3 = [0]*(C+1)
    for j in range(C+1):
        v = V[i][j]
        la = lb = 0
        if j > 0:
            la = now0[j-1]
            lb = now1[j-1]
            now2[j] = max(now2[j], now2[j-1], lb + v)
            now3[j] = max(now3[j], now3[j-1], now2[j-1] + v)
        if i > 0:
            now0[j] = max(now0[j], max_p[j], la)
            now1[j] = max(now1[j], max_p[j] + v, la + v, lb)
        max_p[j] = max(now0[j], now1[j], now2[j], now3[j])

print((max(now0[C], now1[C], now2[C], now3[C])))


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

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

max_p = [0]*(C+1)
for i in range(R+1):
    now0, now1, now2, now3 = [[0]*(C+1) for _ in range(4)]
    for j in range(C+1):
        v = V[i][j]
        la = lb = 0
        if j > 0:
            la = now0[j-1]
            lb = now1[j-1]
            now2[j] = max(now2[j], now2[j-1], lb + v)
            now3[j] = max(now3[j], now3[j-1], now2[j-1] + v)
        if i > 0:
            now0[j] = max(now0[j], max_p[j], la)
            now1[j] = max(now1[j], max_p[j] + v, la + v, lb)
        max_p[j] = max(now0[j], now1[j], now2[j], now3[j])

print((max(now0[C], now1[C], now2[C], now3[C])))