import sys
from itertools import product
input = lambda:sys.stdin.buffer.readline().rstrip()

m, n, k = map(int, input().split())
grid = [[0] * n for _ in range(m)]
for _ in range(k):
    i, j, v = map(int, input().split())
    i -= 1; j -= 1
    grid[i][j] = v

dp = [[0] * 4 for _ in range(n)]
for i in range(m):
    ndp = [[0] * 4 for _ in range(n)]
    for j, k in product(range(n), range(4)):
        # from top
        ndp[j][0] = max(ndp[j][0], dp[j][k])
        ndp[j][1] = max(ndp[j][1], dp[j][k] + grid[i][j])
        # from left
        if j > 0:
            ndp[j][k] = max(ndp[j][k], ndp[j - 1][k])
            if k > 0:
                ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j])
    dp = ndp

print(max(dp[-1])) import sys sys from itertools import product input = lambda:sys.stdin.buffer.readline().rstrip() input lambda:sys.stdin.buffer.readline().rstrip() sys.stdin.buffer.readline().rstrip() sys.stdin.buffer.readline().rstrip sys.stdin.buffer.readline() sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline rstrip m, n, k = map(int, input().split()) m map(int, input().split()) map int input().split() ().split () input split n k [0] * n for _ in range(m) for _ in range(m) _ range(m) range m for _ in range(m) [0] * n [0] 0 n grid = [[0] * n for _ in range(m)] grid [[0] * n for _ in range(m)] for _ in range(k):
    i, j, v = map(int, input().split())
    i -= 1; j -= 1
    grid[i][j] = v _ range(k) range k i, j, v = map(int, input().split()) i map(int, input().split()) map int input().split() ().split () input split j v i -= 1 i 1 j -= 1 j 1 grid[i][j] = v grid[i][j] [i] grid i j v [0] * 4 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * 4 [0] 0 4 dp = [[0] * 4 for _ in range(n)] dp [[0] * 4 for _ in range(n)] for i in range(m):
    ndp = [[0] * 4 for _ in range(n)]
    for j, k in product(range(n), range(4)):
        # from top
        ndp[j][0] = max(ndp[j][0], dp[j][k])
        ndp[j][1] = max(ndp[j][1], dp[j][k] + grid[i][j])
        # from left
        if j > 0:
            ndp[j][k] = max(ndp[j][k], ndp[j - 1][k])
            if k > 0:
                ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j])
    dp = ndp i range(m) range m [0] * 4 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * 4 [0] 0 4 ndp = [[0] * 4 for _ in range(n)] ndp [[0] * 4 for _ in range(n)] for j, k in product(range(n), range(4)):
        # from top
        ndp[j][0] = max(ndp[j][0], dp[j][k])
        ndp[j][1] = max(ndp[j][1], dp[j][k] + grid[i][j])
        # from left
        if j > 0:
            ndp[j][k] = max(ndp[j][k], ndp[j - 1][k])
            if k > 0:
                ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j])
     j k product(range(n), range(4)) product range(n) range n range(4) range 4 ndp[j][0] = max(ndp[j][0], dp[j][k]) ndp[j][0] [j] ndp j 0 max(ndp[j][0], dp[j][k]) max ndp[j][0] [j] ndp j 0 dp[j][k] [j] dp j k ndp[j][1] = max(ndp[j][1], dp[j][k] + grid[i][j]) ndp[j][1] [j] ndp j 1 max(ndp[j][1], dp[j][k] + grid[i][j]) max ndp[j][1] [j] ndp j 1 dp[j][k] + grid[i][j] dp[j][k] [j] dp j k grid[i][j] [i] grid i j if j > 0:
            ndp[j][k] = max(ndp[j][k], ndp[j - 1][k])
            if k > 0:
                ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j])
     j > 0 j 0 ndp[j][k] = max(ndp[j][k], ndp[j - 1][k]) ndp[j][k] [j] ndp j k max(ndp[j][k], ndp[j - 1][k]) max ndp[j][k] [j] ndp j k ndp[j - 1][k] [j - 1] ndp j - 1 j 1 k if k > 0:
                ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j])
     k > 0 k 0 ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j]) ndp[j][k] [j] ndp j k max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j]) max ndp[j][k] [j] ndp j k ndp[j - 1][k - 1] + grid[i][j] ndp[j - 1][k - 1] [j - 1] ndp j - 1 j 1 k - 1 k 1 grid[i][j] [i] grid i j dp = ndp dp ndp print(max(dp[-1])) print max(dp[-1]) max dp[-1] dp -1 dp = ndp ndp dp i -= 1 1 i ndp = [[0] * 4 for _ in range(n)] [[0] * 4 for _ in range(n)] ndp m, n, k = map(int, input().split()) map(int, input().split()) m i, j, v = map(int, input().split()) map(int, input().split()) i v = map(int, input().split()) map(int, input().split()) v input = lambda:sys.stdin.buffer.readline().rstrip() lambda:sys.stdin.buffer.readline().rstrip() input dp = [[0] * 4 for _ in range(n)] [[0] * 4 for _ in range(n)] dp grid = [[0] * n for _ in range(m)] [[0] * n for _ in range(m)] grid k = map(int, input().split()) map(int, input().split()) k n, k = map(int, input().split()) map(int, input().split()) n j -= 1 1 j j, v = map(int, input().split()) map(int, input().split()) j
import sys
from itertools import product
input = lambda:sys.stdin.buffer.readline().rstrip()

m, n, k = map(int, input().split())
grid = [[0] * n for _ in range(m)]
for _ in range(k):
    i, j, v = map(int, input().split())
    i -= 1; j -= 1
    grid[i][j] = v

dp = [[0] * 4 for _ in range(n)]
for i in range(m):
    ndp = [[0] * 4 for _ in range(n)]
    for j, k in product(range(n), range(4)):
        # from top
        ndp[j][0] = max(ndp[j][0], dp[j][k])
        ndp[j][1] = max(ndp[j][1], dp[j][k] + grid[i][j])
        # from left
        if j > 0:
            ndp[j][k] = max(ndp[j][k], ndp[j - 1][k])
            if k > 0:
                ndp[j][k] = max(ndp[j][k], ndp[j - 1][k - 1] + grid[i][j])
    dp = ndp

print(max(dp[-1]))