#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

dp = [[[-1] * C for _ in [None] * R] for _ in [None] * 4]

dp[0][0][0] = 0
for r in range(R):
    for c in range(C):
        v = V[r][c]
        for i in range(4):
            if dp[i][r][c] > -1:
                if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v)

v = V[R - 1][C - 1]
ans = dp[3][R - 1][C - 1]
for i in range(3):
    ans = max(ans, dp[i][R - 1][C - 1] + v)
print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 [None] * R for _ in [None] * R _ [None] * R [None] None R for _ in [None] * R [0] * C [0] 0 C V = [[0] * C for _ in [None] * R] V [[0] * C for _ in [None] * R] for _ in [None] * K:
    r, c, v = map(int, input().split())
    V[r - 1][c - 1] = v _ [None] * K [None] None K r, c, v = map(int, input().split()) r map(int, input().split()) map int input().split() ().split () input split c v V[r - 1][c - 1] = v V[r - 1][c - 1] [r - 1] V r - 1 r 1 c - 1 c 1 v [[-1] * C for _ in [None] * R] for _ in [None] * 4 for _ in [None] * 4 _ [None] * 4 [None] None 4 for _ in [None] * 4 [[-1] * C for _ in [None] * R] dp = [[[-1] * C for _ in [None] * R] for _ in [None] * 4] dp [[[-1] * C for _ in [None] * R] for _ in [None] * 4] dp[0][0][0] = 0 dp[0][0][0] [0] [0] dp 0 0 0 0 for r in range(R):
    for c in range(C):
        v = V[r][c]
        for i in range(4):
            if dp[i][r][c] > -1:
                if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v) r range(R) range R for c in range(C):
        v = V[r][c]
        for i in range(4):
            if dp[i][r][c] > -1:
                if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v) c range(C) range C v = V[r][c] v V[r][c] [r] V r c for i in range(4):
            if dp[i][r][c] > -1:
                if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v) i range(4) range 4 if dp[i][r][c] > -1:
                if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v) dp[i][r][c] > -1 dp[i][r][c] [r] [i] dp i r c -1 if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                 r < R - 1 r R - 1 R 1 dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c]) dp[0][r + 1][c] [r + 1] [0] dp 0 r + 1 r 1 c max(dp[0][r + 1][c], dp[i][r][c]) max dp[0][r + 1][c] [r + 1] [0] dp 0 r + 1 r 1 c dp[i][r][c] [r] [i] dp i r c if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                 c < C - 1 c C - 1 C 1 dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c]) dp[i][r][c + 1] [r] [i] dp i r c + 1 c 1 max(dp[i][r][c + 1], dp[i][r][c]) max dp[i][r][c + 1] [r] [i] dp i r c + 1 c 1 dp[i][r][c] [r] [i] dp i r c if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v) i < 3 and v i < 3 i 3 v if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                     r < R - 1 r R - 1 R 1 dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v) dp[0][r + 1][c] [r + 1] [0] dp 0 r + 1 r 1 c max(dp[0][r + 1][c], dp[i][r][c] + v) max dp[0][r + 1][c] [r + 1] [0] dp 0 r + 1 r 1 c dp[i][r][c] + v dp[i][r][c] [r] [i] dp i r c v if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v) c < C - 1 c C - 1 C 1 dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v) dp[i + 1][r][c + 1] [r] [i + 1] dp i + 1 i 1 r c + 1 c 1 max(dp[i + 1][r][c + 1], dp[i][r][c] + v) max dp[i + 1][r][c + 1] [r] [i + 1] dp i + 1 i 1 r c + 1 c 1 dp[i][r][c] + v dp[i][r][c] [r] [i] dp i r c v v = V[R - 1][C - 1] v V[R - 1][C - 1] [R - 1] V R - 1 R 1 C - 1 C 1 ans = dp[3][R - 1][C - 1] ans dp[3][R - 1][C - 1] [R - 1] [3] dp 3 R - 1 R 1 C - 1 C 1 for i in range(3):
    ans = max(ans, dp[i][R - 1][C - 1] + v) i range(3) range 3 ans = max(ans, dp[i][R - 1][C - 1] + v) ans max(ans, dp[i][R - 1][C - 1] + v) max ans dp[i][R - 1][C - 1] + v dp[i][R - 1][C - 1] [R - 1] [i] dp i R - 1 R 1 C - 1 C 1 v print(ans) print ans input = sys.stdin.readline sys.stdin.readline input v = V[R - 1][C - 1] V[R - 1][C - 1] v dp = [[[-1] * C for _ in [None] * R] for _ in [None] * 4] [[[-1] * C for _ in [None] * R] for _ in [None] * 4] dp R, C, K = map(int, input().split()) map(int, input().split()) R c, v = map(int, input().split()) map(int, input().split()) c C, K = map(int, input().split()) map(int, input().split()) C ans = max(ans, dp[i][R - 1][C - 1] + v) max(ans, dp[i][R - 1][C - 1] + v) ans v = map(int, input().split()) map(int, input().split()) v r, c, v = map(int, input().split()) map(int, input().split()) r K = map(int, input().split()) map(int, input().split()) K ans = dp[3][R - 1][C - 1] dp[3][R - 1][C - 1] ans v = V[r][c] V[r][c] v V = [[0] * C for _ in [None] * R] [[0] * C for _ in [None] * R] V
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

dp = [[[-1] * C for _ in [None] * R] for _ in [None] * 4]

dp[0][0][0] = 0
for r in range(R):
    for c in range(C):
        v = V[r][c]
        for i in range(4):
            if dp[i][r][c] > -1:
                if r < R - 1:
                    dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c])
                if c < C - 1:
                    dp[i][r][c + 1] = max(dp[i][r][c + 1], dp[i][r][c])
                if i < 3 and v:
                    if r < R - 1:
                        dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[i][r][c] + v)
                    if c < C - 1:
                        dp[i + 1][r][c + 1] = max(dp[i + 1][r][c + 1], dp[i][r][c] + v)

v = V[R - 1][C - 1]
ans = dp[3][R - 1][C - 1]
for i in range(3):
    ans = max(ans, dp[i][R - 1][C - 1] + v)
print(ans)