CodeEff/ECCO huggingface dataset:
# Original solution
def main():
    r, c, k = list(map(int, input().split()))
    d = [[0]*c for _ in [0]*r]
    for _ in range(k):
        t1, t2, t3 = list(map(int, input().split()))
        d[t1-1][t2-1] = t3
    dp = [[[0]*4 for _ in [0]*c] for _ in [0]*r]
    dp[0][0][1] = d[0][0]
    for x in range(r):
        for y in range(c):
            for z in range(4):
                if y != c-1:
                    if z != 3:
                        dp[x][y+1][z+1] = max(dp[x][y+1]
                                              [z+1], dp[x][y][z]+d[x][y+1])
                    dp[x][y+1][z] = max(dp[x][y+1][z], dp[x][y][z])
            if x != r-1:
                dp[x+1][y][1] = max(dp[x+1][y][1], max(dp[x][y])+d[x+1][y])
                dp[x+1][y][0] = max(dp[x+1][y][0], max(dp[x][y]))
    print((max(dp[r-1][c-1])))


main()


# Optimized solution
def main():
    r, c, k = list(map(int, input().split()))
    d = dict()
    for _ in range(k):
        t1, t2, t3 = list(map(int, input().split()))
        d[(t1-1, t2-1)] = t3
    dp = [[0]*4 for _ in [0]*c]
    if (0, 0) in d:
        dp[0][1] = d[(0, 0)]
    for x in range(r):
        dp2 = [[0]*4 for _ in [0]*c]
        for y in range(c):
            for z in range(4):
                if y != c-1:
                    if z != 3:
                        if (x, y+1) in d:
                            dp[y+1][z+1] = max(dp[y+1]
                                               [z+1], dp[y][z]+d[(x, y+1)])
                    dp[y+1][z] = max(dp[y+1][z], dp[y][z])
            if x != r-1:
                if (x+1, y) in d:
                    dp2[y][1] = max(dp2[y][1],
                                    max(dp[y])+d[(x+1, y)])
                dp2[y][0] = max(dp2[y][0], max(dp[y]))
        if x != r-1:
            dp = dp2
    print((max(dp[c-1])))


main()
# Original solution
def main():
    r, c, k = list(map(int, input().split()))
    d = dict()
    for _ in range(k):
        t1, t2, t3 = list(map(int, input().split()))
        d[(t1-1, t2-1)] = t3
    dp = [[0]*4 for _ in [0]*c]
    if (0, 0) in d:
        dp[0][1] = d[(0, 0)]
    for x in range(r):
        dp2 = [[0]*4 for _ in [0]*c]
        for y in range(c):
            for z in range(4):
                if y != c-1:
                    if z != 3:
                        if (x, y+1) in d:
                            dp[y+1][z+1] = max(dp[y+1]
                                               [z+1], dp[y][z]+d[(x, y+1)])
                    dp[y+1][z] = max(dp[y+1][z], dp[y][z])
            if x != r-1:
                if (x+1, y) in d:
                    dp2[y][1] = max(dp2[y][1],
                                    max(dp[y])+d[(x+1, y)])
                dp2[y][0] = max(dp2[y][0], max(dp[y]))
        if x != r-1:
            dp = dp2
    print((max(dp[c-1])))


main()


# Optimized solution
def main():
    r, c, k = list(map(int, input().split()))
    d = [[0]*c for _ in [0]*r]
    for _ in range(k):
        t1, t2, t3 = list(map(int, input().split()))
        d[t1-1][t2-1] = t3
    dp = [[0]*4 for _ in [0]*c]
    dp[0][1] = d[0][0]
    for x in range(r):
        dp2 = [[0]*4 for _ in [0]*c]
        for y in range(c):
            for z in range(4):
                if y != c-1:
                    if z != 3:
                        dp[y+1][z+1] = max(dp[y+1]
                                           [z+1], dp[y][z]+d[x][y+1])
                    dp[y+1][z] = max(dp[y+1][z], dp[y][z])
            if x != r-1:
                dp2[y][1] = max(dp2[y][1],
                                max(dp[y])+d[x+1][y])
                dp2[y][0] = max(dp2[y][0], max(dp[y]))
        if x != r-1:
            dp = dp2
    print((max(dp[c-1])))


main()
# Original solution
from sys import stdin
input = stdin.readline
def main():
    R, C, K = list(map(int, input().split()))
    items = [[0 for _ in range(C)] for _ in range(R)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        items[r - 1][c - 1] = v
    dp = [[[0 for _ in range(C)] for _ in range(R)] for _ in range(4)]
    dp[1][0][0] = items[0][0]
    for r in range(R):
        for c in range(C):
            for i in range(4):
                tmp = dp[i][r][c]
                # right
                if c != C - 1:
                    # unpick
                    dp[i][r][c + 1] = max(dp[i][r][c+1], tmp)
                    # pick
                    if i < 3:
                        dp[i+1][r][c + 1] = max(dp[i+1][r]
                                                [c + 1], tmp + items[r][c + 1])
                # down
                if r != R - 1:
                    # unpick
                    dp[i][r + 1][c] = max(dp[i][r + 1][c], tmp)
                    # pick
                    dp[1][r + 1][c] = max(dp[1][r + 1][c],
                                          tmp + items[r + 1][c])
    ans = 0
    for i in range(4):
        ans = max(ans, dp[i][-1][-1])
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
from sys import stdin
input = stdin.readline
def main():
    R, C, K = list(map(int, input().split()))
    items = [[0 for _ in range(C)] for _ in range(R)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        items[r - 1][c - 1] = v
    dp = [[[0 for _ in range(C)] for _ in range(R)] for _ in range(4)]
    dp[1][0][0] = items[0][0]
    for r in range(R):
        for c in range(C):
            for i in range(4):
                tmp = dp[i][r][c]
                # right
                if c != C - 1:
                    # unpick
                    dp[i][r][c + 1] = max(dp[i][r][c+1], tmp)
                    # pick
                    if i < 3:
                        dp[i+1][r][c + 1] = max(dp[i+1][r]
                                                [c + 1], tmp + items[r][c + 1])
                # down
                if r != R - 1:
                    # unpick
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], tmp)
                    # pick
                    dp[1][r + 1][c] = max(dp[1][r + 1][c],
                                          tmp + items[r + 1][c])
    ans = 0
    for i in range(4):
        ans = max(ans, dp[i][-1][-1])
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
f,g,h=range,max,input
R,C,K=list(map(int,h().split()))
G=[[0]*-~C for i in f(R+1)]
for i in'_'*K:r,c,v=list(map(int,h().split()));G[r][c]=v
F=[[[0]*-~C for i in f(R+1)]for i in f(4)]
for r in f(1,R+1):
	for x in f(1,4):
		for c in f(1,C+1):F[x][r][c]=g(F[x-1][r][c],F[x-1][r][c-1]+G[r][c],F[x][r][c-1],(x<2)*(G[r][c]+g(F[1][r-1][c],F[2][r-1][c],F[3][r-1][c])))
print((F[3][R][C]))

# Optimized solution
f,g,h=range,max,input
R,C,K=list(map(int,h().split()))
G=[[0]*-~C for i in f(R+1)]
for i in'_'*K:r,c,v=list(map(int,h().split()));G[r][c]=v
F=[[[0]*-~C for i in f(R+1)]for i in f(4)]
for r in f(1,R+1):
	for x in f(1,4):
		for c in f(1,C+1):F[x][r][c]=g(F[x-1][r][c],F[x-1][r][c-1]+G[r][c],F[x][r][c-1],(x<2)*(G[r][c]+F[3][r-1][c]))
print((F[3][R][C]))
# Original solution
f,h=range,input
R,C,K=list(map(int,h().split()))
G=[-~C*[0]for i in f(R+1)]
for i in'_'*K:r,c,v=list(map(int,h().split()));G[r][c]=v
F=[[-~C*[0]for i in f(R+1)]for i in f(4)]
for r in f(1,R+1):
	for x in f(1,4):
		for c in f(1,C+1):F[x][r][c]=max(F[x-1][r][c],max(F[x-1][r][c-1],(x<2)*F[3][r-1][c])+G[r][c],F[x][r][c-1])
print((F[3][R][C]))

# Optimized solution
f,h=range,input
R,C,K=list(map(int,h().split()))
l=lambda:[-~C*[0]for i in f(R+1)]
G=l()
for i in'_'*K:r,c,v=list(map(int,h().split()));G[r][c]=v
F=[l()for i in f(4)]
for r in f(1,R+1):
	for x in f(1,4):
		for c in f(1,C+1):F[x][r][c]=max(F[x-1][r][c],max(F[x-1][r][c-1],(x<2)*F[3][r-1][c])+G[r][c],F[x][r][c-1])
print((F[3][R][C]))
# Original solution
H,W,k = list(map(int, input().split()))
# アイテムが何処に何個あるか
fs = [[0]*(W+1) for i in range(H+1)]
for i in range(k):
    h, w, v = list(map(int, input().split()))
    h -= 1
    w -= 1
    fs[h][w] = v

mvs = [(0,1),(1,0)]
dp = [[[-1<<50 for i in range(W+1)] for j in range(H+1)] for k in range(4)]
# dp[n][i][j] n個取ったときの、i,jにおけるアイテム価値の最大値
from collections import deque
q = deque([])
q.append((0,0))
dp[0][0][0] = 0
dp[1][0][0] = fs[0][0]
for h in range(H):
    for w in range(W):
        dp[0][h][w+1] = max(dp[0][h][w+1], dp[0][h][w])
        dp[1][h][w+1] = max(dp[1][h][w+1], dp[1][h][w], dp[0][h][w] + fs[h][w+1])
        dp[2][h][w+1] = max(dp[2][h][w+1], dp[2][h][w], dp[1][h][w] + fs[h][w+1])
        dp[3][h][w+1] = max(dp[3][h][w+1], dp[3][h][w], dp[2][h][w] + fs[h][w+1])
    for w in range(W):
        mx = 0
        for i in range(4):
            # print("fe",i,h,w,"dp",dp[i][h][w])
            mx = max(mx,dp[i][h][w])
        dp[0][h+1][w] = mx
        dp[1][h+1][w] = mx + fs[h+1][w]

H -= 1
W -= 1
ans0 = dp[0][H][W]
ans1 = dp[1][H][W]
ans2 = dp[2][H][W]
ans3 = dp[3][H][W]
# print(fs)
# for i in range(3):
#     d = dp[i]
#     for r in d:
#         print(*r)
#     print("----------")
print((max(ans1,ans2,ans3)))

# Optimized solution
H, W, k = list(map(int, input().split()))
"""
dp[k][i][j]
i行目、j番目まで来た時に、k個アイテムをとったときの価値の最大値
"""
fs = [[0]*(W+1) for i in range(H+1)]
dp = [[[-1]*(W+1) for i in range(H+1)] for j in range(4)]

# 価値を受け取る
for i in range(k):
    h, w, v = list(map(int, input().split()))
    fs[h-1][w-1] += v

# initialize
dp[0][0][0] = 0
dp[1][0][0] = fs[0][0]

for h in range(H):
    for w in range(W):
        for i in range(4):
            if i == 0:
                dp[i][h][w+1] = max(dp[0][h][w], dp[0][h][w+1])
            else:
                dp[i][h][w+1] = max(dp[i][h][w], dp[i][h][w+1], dp[i-1][h][w]+fs[h][w+1])
    for w in range(W):
        dp[0][h+1][w] = max(dp[0][h][w],dp[1][h][w],dp[2][h][w],dp[3][h][w],0)
        dp[1][h+1][w] = dp[0][h+1][w] + fs[h+1][w]

mx = 0
for i in range(4):
    mx = max(dp[i][H-1][W-1],mx)
print(mx)
# Original solution
def main():
    import sys
    input = sys.stdin.readline

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

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

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

    for r in range(R):
        for c in range(C):
            dp[r][c][0] = max(
                dp[r - 1][c][0],
                dp[r - 1][c][1],
                dp[r - 1][c][2],
                dp[r - 1][c][3],
                dp[r][c - 1][0]
            )
            for i in range(1, 4):
                dp[r][c][i] = max(dp[r][c][i], dp[r][c - 1][i])
            for i in range(2, -1, -1):
                dp[r][c][i + 1] = max(dp[r][c][i + 1], dp[r][c][i] + G[r][c])

    # print(f'{dp=}')
    ans = max(dp[-2][-2])
    print(ans)


if __name__ == '__main__':
    main()

# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok


# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

    H, W, K = list(map(int, input().split()))

    G = [[0] * W for _ in range(H)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        G[r - 1][c - 1] = v

    dp = [[[0] * 4 for _ in range(W + 1)] for _ in range(H + 1)]
    for r in range(H):
        for c in range(W):
            cell = dp[r][c]
            up = dp[r - 1][c]
            left = dp[r][c - 1]

            for k in range(4):
                cell[0] = max(cell[0], up[k])
                cell[k] = max(cell[k], left[k])

            for k in range(2, -1, -1):
                cell[k + 1] = max(
                    cell[k + 1],
                    cell[k] + G[r][c])

    # for k in range(4):
    #     for row in range(H):
    #         for col in range(W):
    #             print(dp[row][col][k], end=' ')
    #         print()
    #     print()

    # print(*G, sep='\n')

    ans = max(dp[-2][-2])
    print(ans)


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

    H, W, K = list(map(int, input().split()))

    G = [[0] * W for _ in range(H)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        G[r - 1][c - 1] = v

    dp = [[[0] * 4 for _ in range(W + 1)] for _ in range(H + 1)]
    for r in range(H):
        for c in range(W):
            cell = dp[r][c]
            up = dp[r - 1][c]
            left = dp[r][c - 1]

            for k in range(4):
                cell[0] = max(cell[0], up[k])
                cell[k] = max(cell[k], left[k])

            for k in range(2, -1, -1):
                cell[k + 1] = max(
                    cell[k + 1],
                    cell[k] + G[r][c])

    # for k in range(4):
    #     for row in range(H):
    #         for col in range(W):
    #             print(dp[row][col][k], end=' ')
    #         print()
    #     print()

    # print(*G, sep='\n')

    ans = max(dp[-2][-2])
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

    H, W, K = list(map(int, input().split()))

    G = [[0] * W for _ in range(H)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        G[r - 1][c - 1] = v

    pdp = [[0] * 4 for _ in range(W)]
    for r in range(H):
        dp = [[0] * 4 for _ in range(W)]
        left = [0] * 4
        for c in range(W):
            cell = dp[c]

            for k in range(4):
                cell[k] = left[k]

            up = max(pdp[c])
            if cell[0] < up:
                cell[0] = up

            for k in range(2, -1, -1):
                cell[k + 1] = max(
                    cell[k + 1],
                    cell[k] + G[r][c])
            left = cell
        pdp = dp

    # for k in range(4):
    #     for row in range(H):
    #         for col in range(W):
    #             print(dp[row][col][k], end=' ')
    #         print()
    #     print()

    # print(*G, sep='\n')

    ans = max(dp[-1])
    print(ans)


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

    H, W, K = list(map(int, input().split()))

    G = [[0] * W for _ in range(H)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        G[r - 1][c - 1] = v

    pdp = [[0] * 4 for _ in range(W)]
    for r in range(H):
        dp = [[0] * 4 for _ in range(W)]
        left = [0] * 4
        for c in range(W):
            cell = dp[c]

            for k in range(4):
                cell[k] = left[k]

            up = max(pdp[c])
            if cell[0] < up:
                cell[0] = up

            for k in range(2, -1, -1):
                cell[k + 1] = max(
                    cell[k + 1],
                    cell[k] + G[r][c])
            left = cell
        pdp = dp

    # for k in range(4):
    #     for row in range(H):
    #         for col in range(W):
    #             print(dp[row][col][k], end=' ')
    #         print()
    #     print()

    # print(*G, sep='\n')

    ans = max(dp[-1])
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
# https://atcoder.jp/contests/abc175/submissions/15965930

def main():
    import sys
    input = sys.stdin.readline

    H, W, K = list(map(int, input().split()))

    G = [[0] * W for _ in range(H)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        G[r - 1][c - 1] = v

    dp0 = [0] * (W + 1)
    dp1 = [0] * (W + 1)
    dp2 = [0] * (W + 1)
    dp3 = [0] * (W + 1)
    # dpx:=行内取得x個以下の最大値
    # <cは更新済の同じ行の値
    # c<=は前の行の値

    for r in range(H):
        for c in range(W):
            v = G[r][c]

            dp0[c] = max(dp0[c], dp3[c])
            up = dp0[c] + v

            dp3[c] = max(dp3[c - 1], dp2[c - 1] + v, up)
            dp2[c] = max(dp2[c - 1], dp1[c - 1] + v, up)
            dp1[c] = max(dp1[c - 1], dp0[c - 1] + v, up)
            # 左から来て取らない
            # 左から来て取る
            # 上から来て取らない:=dp0
            # 上から来て取る:=dpx(x>0)

            # print(f'{dp0=}')
            # print(f'{dp1=}')
            # print(f'{dp2=}')
            # print(f'{dp3=}')
            # print('*******************')

    print((dp3[-2]))


if __name__ == '__main__':
    main()
# Original solution
R,C,K=list(map(int, input().split()))
M=[[0 for i in range(C)]for i in range(R)]
for i in range(K):
  r,c,v=list(map(int, input().split()))
  r-=1
  c-=1
  M[r][c]=v
dp0=[[0 for j in range(C)] for i in range(R)]
dp1=[[0 for j in range(C)] for i in range(R)]
dp2=[[0 for j in range(C)] for i in range(R)]
dp3=[[0 for j in range(C)] for i in range(R)]
dp1[0][0]+=M[0][0]
for r in range(R):
  for c in range(C):
    if 0<r:
      dp1[r][c]=max(max(dp1[r-1][c],dp2[r-1][c],dp3[r-1][c])+M[r][c],dp1[r][c])
      dp0[r][c]=max(max(dp1[r-1][c],dp2[r-1][c],dp3[r-1][c]),dp0[r][c])
    if 0<c:
      dp3[r][c]=max(dp2[r][c-1]+M[r][c],dp3[r][c],dp3[r][c-1])
      dp2[r][c]=max(dp1[r][c-1]+M[r][c],dp2[r][c],dp2[r][c-1])
      dp1[r][c]=max(dp0[r][c-1]+M[r][c],dp1[r][c],dp1[r][c-1])
      dp0[r][c]=max(dp0[r][c-1],dp0[r][c])
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()))
M=[[0 for i in range(C)]for i in range(R)]
for i in range(K):
  r,c,v=list(map(int, input().split()))
  r-=1
  c-=1
  M[r][c]=v
dp0=[[0 for j in range(C)] for i in range(R)]
dp1=[[0 for j in range(C)] for i in range(R)]
dp2=[[0 for j in range(C)] for i in range(R)]
dp3=[[0 for j in range(C)] for i in range(R)]
dp1[0][0]+=M[0][0]
for r in range(R):
  for c in range(C):
    if 0<r:
      dp1[r][c]=max(max(dp1[r-1][c],dp2[r-1][c],dp3[r-1][c])+M[r][c],dp1[r][c])
      dp0[r][c]=max(max(dp1[r-1][c],dp2[r-1][c],dp3[r-1][c]),dp0[r][c])
    if 0<c:
      dp3[r][c]=max(dp2[r][c-1]+M[r][c],dp3[r][c],dp3[r][c-1])
      dp2[r][c]=max(dp1[r][c-1]+M[r][c],dp2[r][c],dp2[r][c-1])
      dp1[r][c]=max(dp0[r][c-1]+M[r][c],dp1[r][c],dp1[r][c-1])
      dp0[r][c]=max(dp0[r][c-1],dp0[r][c])
print((max(dp1[-1][-1],dp2[-1][-1],dp3[-1][-1])))
# Original solution

def resolve():
    R, C, K = list(map(int, input().split()))
    G = [[0] * (C) for _ in range(R)]
    for _ in range(K):
        r, c, v = list(map(int, input().split()))
        G[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):
            for k in range(2, -1, -1):
                dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + G[i][j])
            for k in range(4):
                dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k])
                dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
                
    ans = 0
    for k in range(4):
        ans = max(ans, dp[R-1][C-1][k])
    print(ans)


if __name__ == "__main__":
    resolve()

# Optimized solution

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

    DP1 = [0 for _ in range(C + 1)]  # c列目までとったとき、最新の行で1個以下とったという条件のもとの最大
    DP2 = [0 for _ in range(C + 1)]  # c列目までとったとき、最新の行で2個以下とったという条件のもとの最大
    DP3 = [0 for _ in range(C + 1)]  # c列目までとったとき、最新の行で3個以下とったという条件のもとの最大

    for r in range(1, R+1):
        for c in range(1, C+1):
            # 更新の順番に注意
            # mid = max(DP1[c], DP2[c], DP3[c]) + I[r][c]
            mid = DP3[c] + G[r][c]
            DP3[c] = max(DP3[c - 1], mid, DP2[c - 1] + G[r][c])  # 3個
            DP2[c] = max(DP2[c - 1], mid, DP1[c - 1] + G[r][c])  # 2個
            DP1[c] = max(DP1[c - 1], mid)  # 1個

    print((DP3[C]))

if __name__ == "__main__":
    resolve()
# Original solution

import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()

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

    dp = [[[0] * 4 for _ in range(C + 1)] for _ in range(R + 1)]
    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                # 次の行に移動する場合
                if i + 1 <= R:
                    # 移動先のアイテムを取らない場合
                    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] + G[i + 1][j])
                # 次の列に移動する場合
                if j + 1 <= C:
                    # 移動先のアイテムを取らない場合
                    dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k])
                    # 現在のkが3未満のときのみ, 移動先のアイテムを取ることが可能
                    if k < 3:
                        dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k] + G[i][j + 1])

    ans = 0
    for k in range(4):
        ans = max(ans, dp[-1][-1][k])
    print(ans)


if __name__ == "__main__":
    resolve()


# Optimized solution

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

    dp = [[[0] * (C + 1) for _ in range(R + 1)] for _ in range(4)]
    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                here = dp[k][i][j]
                # 次の行に移動する場合
                if i + 1 <= R:
                    # 移動先のアイテムを取らない場合
                    dp[0][i + 1][j] = max(dp[0][i + 1][j], here)
                    # 移動先のアイテムを取る場合
                    dp[1][i + 1][j] = max(dp[1][i + 1][j], here + G[i + 1][j])
                # 次の列に移動する場合
                if j + 1 <= C:
                    # 移動先のアイテムを取らない場合
                    dp[k][i][j + 1] = max(dp[k][i][j + 1], here)
                    # 現在のkが3未満のときのみ, 移動先のアイテムを取ることが可能
                    if k < 3:
                        dp[k + 1][i][j + 1] = max(dp[k + 1][i][j + 1], here + G[i][j + 1])
    ans = 0
    for k in range(4):
        ans = max(ans, dp[k][-1][-1])
    print(ans)


if __name__ == "__main__":
    resolve()
# Original solution
r,c,k=list(map(int,input().split()))

fuck=[list(map(int,input().split())) for _ in range(k)]

rck=[[0]*c for _ in range(r)]

for [rr,cc,kk] in fuck:
    rck[rr-1][cc-1]=kk

#dp[y][x][cnt]

dp=[[[0]*4 for _ in range(c)] for _ in range(r)]
#print(dp)
for y in range(r):
    for x in range(c):
        #1行目
        if y == 0:
            if x==0:
                dp[y][x][1]=rck[y][x]
            if x>=1:
                dp[y][x][1]=max(dp[y][x-1][1],rck[y][x])
                dp[y][x][2]=max(dp[y][x-1][2],dp[y][x-1][1]+rck[y][x])
            if x>=2:
                dp[y][x][3]=max(dp[y][x-1][3],dp[y][x-1][2]+rck[y][x])
        else:
            #1は、上0+1 or 左0+1 or 上1 or 左1
            #count1ケース
            for i in range(1,4):
                if i==1:
                    uemax=max([max(dp[y-1][x][1],dp[y-1][x][2],dp[y-1][x][3])])
                    dp[y][x][i]=uemax+rck[y][x]
                    if x>0:
                        dp[y][x][i]=max(dp[y][x][i],dp[y][x-1][i])
                elif i>=2:
                    if x>=1:
                        dp[y][x][i]=max(dp[y][x-1][i],dp[y][x-1][i-1]+rck[y][x])

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

# Optimized solution
def pri(x):
    for item in x:
        print(item)

r,c,k=list(map(int,input().split()))

fuck=[list(map(int,input().split())) for _ in range(k)]

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

for [rr,cc,kk] in fuck:
    rck[rr][cc]=kk
#pri(rck)
#dp[y][x][cnt]

dp=[[0]*4 for _ in range(c+1)]
#print(dp)
for y in range(1,r+1):
    for x in range(1,c+1):
        
        dp[x][0]=max(dp[x-1][0],dp[x][0],dp[x][1],dp[x][2],dp[x][3])
        dp[x][1]=max(dp[x][0]+rck[y][x],dp[x][1],dp[x-1][1])
        dp[x][2]=max(dp[x-1][1]+rck[y][x],dp[x][2],dp[x-1][2],dp[x][1])
        dp[x][3]=max(dp[x-1][2]+rck[y][x],dp[x][3],dp[x-1][3],dp[x][2])
        #print(y,x,dp)

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