CodeEff/ECCO huggingface dataset:
# Original solution
#!/usr/bin/env python3
r, c, k = list(map(int, input().split()))
items = [[0] * (c+1) for _ in range(r+1)]
for _ in range(k):
    R, C, V = list(map(int, input().split()))
    items[R][C] = V

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(1, r+1):
    for j in range(1, c+1):
        dp1[i][j] = max(dp1[i][j-1], dp1[i-1][j] + items[i][j], dp2[i-1][j] + items[i][j], dp3[i-1][j] + items[i][j])
        dp2[i][j] = max(dp2[i][j-1], dp1[i][j-1] + items[i][j])
        dp3[i][j] = max(dp3[i][j-1], dp2[i][j-1] + items[i][j])

ans = max(dp1[r][c], dp2[r][c], dp3[r][c])
print(ans)

# Optimized solution
#!/usr/bin/env python3
r, c, k = list(map(int, input().split()))
items = [[0] * (c+1) for _ in range(r+1)]
for _ in range(k):
    R, C, V = list(map(int, input().split()))
    items[R][C] = V

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(1, r+1):
    for j in range(1, c+1):
        v = items[i][j]
        dp1[i][j] = max(dp1[i][j-1], dp1[i-1][j] + v, dp2[i-1][j] + v, dp3[i-1][j] + v)
        dp2[i][j] = max(dp2[i][j-1], dp1[i][j-1] + v)
        dp3[i][j] = max(dp3[i][j-1], dp2[i][j-1] + v)

ans = max(dp1[r][c], dp2[r][c], dp3[r][c])
print(ans)
# Original solution
R,C,K = list(map(int,input().split()))
goods = [[-1]*(C+1) for _ in range(R+1)]
INF = 10**18
for i in range(K):
    ri,ci,vi = list(map(int,input().split()))
    goods[ri][ci] = vi
DP = [[[-INF,-INF,-INF,-INF] for _ in range(C+1)] for _ in range(R+1)]
DP[1][1][0] = 0
if goods[1][1] != -1:
    DP[1][1][1] = goods[1][1]
for j in range(1,C):
    DP[1][j+1][0] = 0
    if goods[1][j+1] != -1:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j][0]+goods[1][j+1])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j][1]+goods[1][j+1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j][2]+goods[1][j+1])
    else:
        DP[1][j+1][1] = DP[1][j][1]
        DP[1][j+1][2] = DP[1][j][2]
        DP[1][j+1][3] = DP[1][j][3]
for i in range(1,R):
    DP[i+1][1][0] = max(DP[i][1])
    if goods[i+1][1] != -1:
        DP[i+1][1][1] = DP[i+1][1][0] + goods[i+1][1]
for i in range(2,R+1):
    for j in range(2,C+1):
        DP[i][j][0] = max(max(DP[i-1][j]),DP[i][j-1][0])
        if goods[i][j] != -1:
            DP[i][j][1] = max(DP[i][j][0]+goods[i][j],DP[i][j-1][1])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j-1][1]+goods[i][j])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j-1][2]+goods[i][j])
        else:
            DP[i][j][1] = DP[i][j-1][1]
            DP[i][j][2] = DP[i][j-1][2]
            DP[i][j][3] = DP[i][j-1][3]
print((max(DP[R][C])))

# Optimized solution
import sys
input = sys.stdin.buffer.readline
R,C,K = list(map(int,input().split()))
goods = [[-1]*(C+1) for _ in range(R+1)]
INF = 10**16
for i in range(K):
    ri,ci,vi = list(map(int,input().split()))
    goods[ri][ci] = vi
DP = [[[-INF,-INF,-INF,-INF] for _ in range(C+1)] for _ in range(R+1)]
DP[1][1][0] = 0
if goods[1][1] != -1:
    DP[1][1][1] = goods[1][1]
for j in range(1,C):
    DP[1][j+1][0] = 0
    if goods[1][j+1] != -1:
        DP[1][j+1][1] = max(DP[1][j][1],DP[1][j][0]+goods[1][j+1])
        DP[1][j+1][2] = max(DP[1][j][2],DP[1][j][1]+goods[1][j+1])
        DP[1][j+1][3] = max(DP[1][j][3],DP[1][j][2]+goods[1][j+1])
    else:
        DP[1][j+1][1] = DP[1][j][1]
        DP[1][j+1][2] = DP[1][j][2]
        DP[1][j+1][3] = DP[1][j][3]
for i in range(1,R):
    DP[i+1][1][0] = max(DP[i][1])
    if goods[i+1][1] != -1:
        DP[i+1][1][1] = DP[i+1][1][0] + goods[i+1][1]
for i in range(2,R+1):
    for j in range(2,C+1):
        DP[i][j][0] = max(max(DP[i-1][j]),DP[i][j-1][0])
        if goods[i][j] != -1:
            DP[i][j][1] = max(DP[i][j][0]+goods[i][j],DP[i][j-1][1])
            DP[i][j][2] = max(DP[i][j-1][2],DP[i][j-1][1]+goods[i][j])
            DP[i][j][3] = max(DP[i][j-1][3],DP[i][j-1][2]+goods[i][j])
        else:
            DP[i][j][1] = DP[i][j-1][1]
            DP[i][j][2] = DP[i][j-1][2]
            DP[i][j][3] = DP[i][j-1][3]
print((max(DP[R][C])))
# Original solution
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n, m, q = list(map(int, input().split()))
g = [[0 for _ in range(m)] for _ in range(n)]
for _ in range(q):
	r, c, v = list(map(int, input().split()))
	g[r-1][c-1] = v

dp = [[[0 for _ in range(4)] for _ in range(m)] for _ in range(n)]
dp[0][0][1] = g[0][0]
for i in range(n):
	for j in range(m):
		if i > 0:
			dp[i][j][0] = max(dp[i][j][0], max(dp[i-1][j]))
		if j > 0:
			dp[i][j][0] = max(dp[i][j][0], dp[i][j-1][0])
			for k in range(1, 4):
				dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
				dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + g[i][j])
			if i > 0:
				dp[i][j][1] = max(dp[i][j][1], max(dp[i-1][j]) + g[i][j])
		if i > 0 and j == 0:
			dp[i][0][1] = max(dp[i][0][1], max(dp[i-1][0]) + g[i][j])

print((max(dp[n-1][m-1])))

# Optimized solution
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n, m, q = list(map(int, input().split()))
g = [[0 for _ in range(m)] for _ in range(n)]
for _ in range(q):
	r, c, v = list(map(int, input().split()))
	g[r-1][c-1] = v

pre = [0 for _ in range(m)]
dp = [[0 for _ in range(4)] for _ in range(m)]
dp[0][1] = g[0][0]
for i in range(n):
	for j in range(m):
		dp[j][0] = max(dp[j][0], pre[j])
		if j > 0:
			dp[j][0] = max(dp[j][0], dp[j-1][0])
			for k in range(1, 4):
				dp[j][k] = max(dp[j][k], dp[j-1][k])
				dp[j][k] = max(dp[j][k], dp[j-1][k-1] + g[i][j])
			if i > 0:
				dp[j][1] = max(dp[j][1], pre[j] + g[i][j])
		else:
			dp[0][1] = max(dp[0][1], pre[0] + g[i][j])
	pre = [max(dp[j]) for j in range(m)]
	dp = [[0 for _ in range(4)] for _ in range(m)]

print((pre[-1]))
# Original solution
import sys
read = sys.stdin.buffer.read
readlines = sys.stdin.buffer.readlines
input = sys.stdin.buffer.readline
H, W, K = list(map(int, input().split()))
B = {}

for _ in range(K):
    r, c, v = list(map(int, input().split()))
    B[(r-1,c-1)] = v

dp = [[0]*(W+1) for _ in range(4)]
 
for i in range(H):
    for j in range(W):
        if (i, j) in B:
            v = B[(i, j)]
            dp[0][j+1] = max(dp[0][j], dp[0][j+1], dp[1][j+1], dp[2][j+1], dp[3][j+1])
            dp[1][j+1] = max(dp[1][j], dp[0][j+1]+v)
            dp[2][j+1] = max(dp[2][j], dp[1][j]+v)
            dp[3][j+1] = max(dp[3][j], dp[2][j]+v)
        else:
            dp[0][j+1] = max(dp[0][j], dp[0][j+1], dp[1][j+1], dp[2][j+1], dp[3][j+1])
            dp[1][j+1] = dp[1][j]
            dp[2][j+1] = dp[2][j]
            dp[3][j+1] = dp[3][j]
print((max(dp[i][-1] for i in range(4))))

# Optimized solution
import sys
input = sys.stdin.buffer.readline
H, W, K = list(map(int, input().split()))
B = {}
for _ in range(K):
    r, c, v = list(map(int, input().split()))
    B[(r, c)] = v

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

for i in range(1, H+1):
    for j in range(1, W+1):
        if (i, j) in B:
            v = B[(i, j)]
            dp[0][j] = max(dp[0][j-1], dp[0][j], dp[1][j], dp[2][j], dp[3][j])
            dp[1][j] = max(dp[1][j-1], dp[0][j]+v)
            dp[2][j] = max(dp[2][j-1], dp[1][j-1]+v)
            dp[3][j] = max(dp[3][j-1], dp[2][j-1]+v)
        else:
            dp[0][j] = max(dp[0][j-1], dp[0][j], dp[1][j], dp[2][j], dp[3][j])
            dp[1][j] = dp[1][j-1]
            dp[2][j] = dp[2][j-1]
            dp[3][j] = dp[3][j-1]

print((max(dp[i][-1] for i in range(4))))
# Original solution
# -*- coding: utf-8 -*-

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

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

DP_prev = [0 for _ in range(C+1)]

for r in range(1,R+1):
  DP = [[0 for _ in range(C+1)],[0 for _ in range(C+1)],[0 for _ in range(C+1)]]
  for c in range(1,C+1):
    item = items[r][c]
    DP1 = max(DP_prev[c]+item,DP[0][c-1])
    DP2 = max(DP1,DP[1][c-1],DP[0][c-1]+item)
    DP3 = max(DP2,DP[2][c-1],DP[1][c-1]+item)
    DP[0][c] = DP1
    DP[1][c] = DP2
    DP[2][c] = DP3
  DP_prev = DP[2]
        
print((DP_prev[C]))

# Optimized solution
# -*- coding: utf-8 -*-

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

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

DP_prev = [0 for _ in range(C+1)]
for r in range(1,R+1):
  DP1 = [0 for _ in range(C+1)]
  DP2 = [0 for _ in range(C+1)]
  DP3 = [0 for _ in range(C+1)]
  for c in range(1,C+1):
    item = items[r][c]
    DP1[c] = max(DP_prev[c]+item,DP1[c-1])
    DP2[c] = max(DP1[c],DP2[c-1],DP1[c-1]+item)
    DP3[c] = max(DP2[c],DP3[c-1],DP2[c-1]+item)
  DP_prev = DP3
        
print((DP_prev[C]))
# Original solution
import sys
input = sys.stdin.readline
r,c,K=list(map(int, input().split()))
grid=[[0]*(c+1) for _ in range(r+1)]
for i in range(K):
    ri,ci,vi=list(map(int, input().split()))
    grid[ri][ci]=vi

dp=[[[0]*4 for _ in range(c+1)] for _ in range(r+1)]

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

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

grid=[[0]*C for _ in range(R)]
for i in range(K):
    x,y,v = list(map(int,input().split()))
    grid[x-1][y-1] = v
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):
    for j in range(C):
        dp1[i][j] = max(dp1[i][j-1],dp1[i-1][j]+grid[i][j],
                    dp2[i-1][j]+grid[i][j],dp3[i-1][j]+grid[i][j])
        dp2[i][j] = max(dp2[i][j-1],dp1[i][j-1]+grid[i][j])
        dp3[i][j] = max(dp3[i][j-1],dp2[i][j-1]+grid[i][j])
ans=0
for i in range(R):
    for j in range(C):
        ans=max(ans,dp1[i][j],dp2[i][j],dp3[i][j])
print(ans)
# Original solution
R,C,K = list(map(int,input().split()))

grid=[[0]*C for _ in range(R)]
for i in range(K):
    x,y,v = list(map(int,input().split()))
    grid[x-1][y-1] = v
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):
    for j in range(C):
        dp1[i][j] = max(dp1[i][j-1],dp1[i-1][j]+grid[i][j],
                    dp2[i-1][j]+grid[i][j],dp3[i-1][j]+grid[i][j])
        dp2[i][j] = max(dp2[i][j-1],dp1[i][j-1]+grid[i][j])
        dp3[i][j] = max(dp3[i][j-1],dp2[i][j-1]+grid[i][j])
ans=0
for i in range(R):
    for j in range(C):
        ans=max(ans,dp1[i][j],dp2[i][j],dp3[i][j])
print(ans)

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

grid=[[0]*C for _ in range(R)]
for i in range(K):
    x,y,v = list(map(int,input().split()))
    grid[x-1][y-1] = 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):
    for j in range(C):
        dp1[i][j] = max(dp1[i][j-1],dp1[i-1][j]+grid[i][j],
                    dp2[i-1][j]+grid[i][j],dp3[i-1][j]+grid[i][j])
        dp2[i][j] = max(dp2[i][j-1],dp1[i][j-1]+grid[i][j])
        dp3[i][j] = max(dp3[i][j-1],dp2[i][j-1]+grid[i][j])
ans=max(dp1[R-1][C-1],dp2[R-1][C-1],dp3[R-1][C-1])
print(ans)
# Original solution
import sys
def input(): return sys.stdin.readline().rstrip()

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[i][j][3]
dp = [[[0] * (C+5) for _ in range(R + 5)] for _ in range(4)]
for i in range(1, R + 3):
    for j in range(1, C + 3):
        # 行移動
        dp[0][i][j] = max(dp[0][i][j], max(dp[k][i - 1][j] for k in range(4)))
        dp[1][i][j] = max(dp[1][i][j], max(dp[k][i - 1][j] for k in range(4)) + V[i][j])
        for k in range(4):
            # 列移動
            if k > 0:
                dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + V[i][j])
            dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
print((max(dp[k][R][C] for k in range(4))))


# Optimized solution
import sys
def input(): return sys.stdin.readline().rstrip()

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[3][i][j]
dp = [[[0] * (C + 5) for _ in range(R + 5)] for _ in range(4)]
for i in range(R + 3):
    for j in range(C + 3):
        for k in range(4):
            # 行移動
            dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[k][i][j])
            dp[1][i + 1][j] = max(dp[1][i + 1][j], dp[k][i][j] + V[i + 1][j])
            # 列移動
            dp[k][i][j + 1] = max(dp[k][i][j + 1], dp[k][i][j])
            if k < 3:
                dp[k + 1][i][j + 1] = max(dp[k + 1][i][j + 1], dp[k][i][j] + V[i][j + 1])
print((max(dp[k][R][C] for k in range(4))))
# Original solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal, ROUND_CEILING
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

R, C, K = MAP()

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

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

for _ in range(K):
    r, c, v = MAP()
    field[r][c] = v

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

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


# Optimized solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal, ROUND_CEILING
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

R, C, K = MAP()

dp1 = [[0]*4 for _ in range(C+1)]
dp2 = [[0]*4 for _ in range(C+1)]

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

for _ in range(K):
    r, c, v = MAP()
    field[r][c] = v

for i in range(1, R+1):
    for j in range(1, C+1):
        if i%2 == 0:       
            up_max = max(dp1[j])
            p = field[i][j]
            dp2[j][0] = max(up_max, dp1[j-1][0])
            dp2[j][1] = max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p)
            dp2[j][2] = max(dp2[j-1][2], dp2[j-1][1]+p)
            dp2[j][3] = max(dp2[j-1][3], dp2[j-1][2]+p)
        else:
            up_max = max(dp2[j])
            p = field[i][j]
            dp1[j][0] = max(up_max, dp2[j-1][0])
            dp1[j][1] = max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p)
            dp1[j][2] = max(dp1[j-1][2], dp1[j-1][1]+p)
            dp1[j][3] = max(dp1[j-1][3], dp1[j-1][2]+p)

a = max(dp1[-1])
b = max(dp2[-1])
print((max(a, b)))
# Original solution
import sys
input = sys.stdin.readline

def solve():
    INF = float('inf')

    def max2(x, y): return x if x >= y else y

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

    dp = [[[-INF]*(4) for j in range(C+1)] for i in range(R+1)]
    for i in range(R+1):
        dp[i][0][0] = 0
    for j in range(C+1):
        dp[0][j][0] = 0
    dpMax = [[0]*(C+1) for _ in range(R+1)]

    for i in range(1, R+1):
        for j in range(1, C+1):
            V = Vss[i][j]
            dp[i][j][0] = max2(dp[i][j-1][0], dpMax[i-1][j])
            dp[i][j][1] = max2(dp[i][j][0] + V, dp[i][j-1][1])
            dp[i][j][2] = max2(dp[i][j-1][1] + V, dp[i][j-1][2])
            dp[i][j][3] = max2(dp[i][j-1][2] + V, dp[i][j-1][3])
            dpMax[i][j] = max(dp[i][j])

    ans = dpMax[R][C]
    print(ans)


solve()


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

def solve():
    INF = float('inf')

    def max2(x, y): return x if x >= y else y

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

    dpMax = [0]*(C+1)
    for i in range(1, R+1):
        dp0 = [0]*(C+1)
        dp1 = [-INF]*(C+1)
        dp2 = [-INF]*(C+1)
        dp3 = [-INF]*(C+1)
        for j in range(1, C+1):
            V = Vss[i][j]
            dp0[j] = max2(dp0[j-1], dpMax[j])
            dp1[j] = max2(dp0[j] + V, dp1[j-1])
            dp2[j] = max2(dp1[j-1] + V, dp2[j-1])
            dp3[j] = max2(dp2[j-1] + V, dp3[j-1])
            dpMax[j] = max(dp0[j], dp1[j], dp2[j], dp3[j])

    ans = dpMax[C]
    print(ans)


solve()
# Original solution
def run():
    # sys.stdin.readline()
    import sys
    input = sys.stdin.readline
    r,c,k = list(map(int,input().split()))
    t = [[0 for i in range(c)] for i in range(r)]
    for i in range(k):
        a,b,z = list(map(int,input().split()))
        t[a-1][b-1] = z
    dp = [[[0 for i in range(c)]for i in range(r)]for i in range(4)]
    dp[0][0][0] = 0
    dp[1][0][0] = t[0][0]
    dp[2][0][0] = t[0][0]
    dp[3][0][0] = t[0][0]
    for i in range(r-1):
        dp[0][i+1][0] = dp[1][i][0]
        for j in range(1,4):
            dp[j][i+1][0] = dp[1][i][0]+t[i+1][0]
    for i in range(c-1):
        dp[0][0][i+1] = 0
        for j in range(1,4):
            dp[j][0][i+1] = max(dp[1][0][i],t[0][i+1])
        for j in range(2,4):
            dp[j][0][i+1] = max(dp[2][0][i],dp[1][0][i]+t[0][i+1])
        dp[3][0][i+1] = max(dp[3][0][i],dp[2][0][i]+t[0][i+1])
    for i in range(r-1):
        for j in range(c-1):
            for s in range(0,4):
                dp[s][i+1][j+1] =  max(dp[0][i+1][j],dp[3][i][j+1])
            for s in range(1,4):
                dp[s][i+1][j+1] = max(dp[0][i+1][j]+t[i+1][j+1],dp[1][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
            for s in range(2,4):
                dp[s][i+1][j+1] = max(dp[1][i+1][j]+t[i+1][j+1],dp[2][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
            dp[3][i+1][j+1] = max(dp[2][i+1][j]+t[i+1][j+1],dp[3][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
    print((dp[3][r-1][c-1]))

if __name__ == "__main__":
    run()

# Optimized solution
def run():
    # sys.stdin.readline()
    import sys
    input = sys.stdin.readline
    r,c,k = list(map(int,input().split()))
    t = [[0 for i in range(c)] for i in range(r)]
    for i in range(k):
        a,b,z = list(map(int,input().split()))
        t[a-1][b-1] = z
    t = tuple(t)
    dp = [[[0 for i in range(c)]for i in range(r)]for i in range(4)]
    dp[0][0][0] = 0
    dp[1][0][0] = t[0][0]
    dp[2][0][0] = t[0][0]
    dp[3][0][0] = t[0][0]
    for i in range(r-1):
        dp[0][i+1][0] = dp[1][i][0]
        for j in range(1,4):
            dp[j][i+1][0] = dp[1][i][0]+t[i+1][0]
    for i in range(c-1):
        dp[0][0][i+1] = 0
        for j in range(1,4):
            dp[j][0][i+1] = max(dp[1][0][i],t[0][i+1])
        for j in range(2,4):
            dp[j][0][i+1] = max(dp[2][0][i],dp[1][0][i]+t[0][i+1])
        dp[3][0][i+1] = max(dp[3][0][i],dp[2][0][i]+t[0][i+1])
    for i in range(r-1):
        for j in range(c-1):
            for s in range(0,4):
                dp[s][i+1][j+1] =  max(dp[0][i+1][j],dp[3][i][j+1])
            for s in range(1,4):
                dp[s][i+1][j+1] = max(dp[0][i+1][j]+t[i+1][j+1],dp[1][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
            for s in range(2,4):
                dp[s][i+1][j+1] = max(dp[1][i+1][j]+t[i+1][j+1],dp[2][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
            dp[3][i+1][j+1] = max(dp[2][i+1][j]+t[i+1][j+1],dp[3][i+1][j],dp[3][i][j+1]+t[i+1][j+1])
    print((dp[3][r-1][c-1]))

if __name__ == "__main__":
    run()
# Original solution
from sys import stdin
readline = stdin.readline

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

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

dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
for i in range(R):
    for j in range(C):
        if goods[i][j] != 0:
            for k in range(2, -1, -1):
                if dp[i][j][k] + goods[i][j] > dp[i][j][k + 1]:
                    dp[i][j][k + 1] = dp[i][j][k] + goods[i][j]
        for k in range(4):
            if dp[i][j][k] > dp[i][j + 1][k]:
                dp[i][j + 1][k] = dp[i][j][k]
            if dp[i][j][k] > dp[i + 1][j][0]:
                dp[i + 1][j][0] = dp[i][j][k]

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


# Optimized solution
from sys import stdin
readline = stdin.readline

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

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

n = [0] * (C + 1)
for i in range(R):
    c = [0] * 4
    c[0] = n[0]
    for j in range(C):
        if goods[i][j] != 0:
            for k in range(2, -1, -1):
                if c[k] + goods[i][j] > c[k + 1]:
                    c[k + 1] = c[k] + goods[i][j]
        for k in range(4):
            if c[k] > n[j]:
                n[j] = c[k]
        if n[j + 1] > c[0]:
            c[0] = n[j + 1]

print((max(c)))
# Original solution
from sys import stdin
readline = stdin.readline

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

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

n = [0] * (C + 1)
for i in range(R):
    c = [0] * 4
    c[0] = n[0]
    for j in range(C):
        if goods[i][j] != 0:
            for k in range(2, -1, -1):
                if c[k] + goods[i][j] > c[k + 1]:
                    c[k + 1] = c[k] + goods[i][j]
        for k in range(4):
            if c[k] > n[j]:
                n[j] = c[k]
        if n[j + 1] > c[0]:
            c[0] = n[j + 1]

print((max(c)))


# Optimized solution
from sys import stdin
readline = stdin.readline

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

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

dp = [0] * (C + 1)
for i in range(R):
    cur = [0] * 4
    cur[0] = dp[0]
    for j in range(C):
        if goods[i][j] != 0:
            for k in range(2, -1, -1):
                cur[k + 1] = max(cur[k + 1], cur[k] + goods[i][j])
        dp[j] = max(cur)
        cur[0] = max(cur[0], dp[j + 1])

print((max(cur)))