
R, C, K = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(K)]

items = [[0] * (C + 1) for _ in range(R + 1)]
for r, c, v in X:
    items[r][c] = v

dp = [[0] * 4 for _ in range(C + 1)]
for i in range(1, R + 1):
    # Vertical transition
    for j in range(1, C + 1):
        dp[j][0] = max(dp[j])
        for k in range(1, 4):
            dp[j][k] = 0

    # Horizontal or picking-up transition
    for j in range(1, C + 1):
        for k in reversed(range(4)):
            dp[j][k] = max(dp[j][k], dp[j - 1][k])
            if items[i][j] and k < 3:
                dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + items[i][j])

print(max(dp[-1])) R, C, K = map(int, input().split()) R map(int, input().split()) map int input().split() ().split () input split C K list(map(int, input().split())) for _ in range(K) for _ in range(K) _ range(K) range K for _ in range(K) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split X = [list(map(int, input().split())) for _ in range(K)] X [list(map(int, input().split())) for _ in range(K)] [0] * (C + 1) for _ in range(R + 1) for _ in range(R + 1) _ range(R + 1) range R + 1 R 1 for _ in range(R + 1) [0] * (C + 1) [0] 0 C + 1 C 1 items = [[0] * (C + 1) for _ in range(R + 1)] items [[0] * (C + 1) for _ in range(R + 1)] for r, c, v in X:
    items[r][c] = v r c v X items[r][c] = v items[r][c] [r] items r c v [0] * 4 for _ in range(C + 1) for _ in range(C + 1) _ range(C + 1) range C + 1 C 1 for _ in range(C + 1) [0] * 4 [0] 0 4 dp = [[0] * 4 for _ in range(C + 1)] dp [[0] * 4 for _ in range(C + 1)] for i in range(1, R + 1):
    # Vertical transition
    for j in range(1, C + 1):
        dp[j][0] = max(dp[j])
        for k in range(1, 4):
            dp[j][k] = 0

    # Horizontal or picking-up transition
    for j in range(1, C + 1):
        for k in reversed(range(4)):
            dp[j][k] = max(dp[j][k], dp[j - 1][k])
            if items[i][j] and k < 3:
                dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + items[i][j]) i range(1, R + 1) range 1 R + 1 R 1 for j in range(1, C + 1):
        dp[j][0] = max(dp[j])
        for k in range(1, 4):
            dp[j][k] = 0

    # Horizontal or picking-up transition
     j range(1, C + 1) range 1 C + 1 C 1 dp[j][0] = max(dp[j]) dp[j][0] [j] dp j 0 max(dp[j]) max dp[j] dp j for k in range(1, 4):
            dp[j][k] = 0

    # Horizontal or picking-up transition
     k range(1, 4) range 1 4 dp[j][k] = 0 dp[j][k] [j] dp j k 0 for j in range(1, C + 1):
        for k in reversed(range(4)):
            dp[j][k] = max(dp[j][k], dp[j - 1][k])
            if items[i][j] and k < 3:
                dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + items[i][j]) j range(1, C + 1) range 1 C + 1 C 1 for k in reversed(range(4)):
            dp[j][k] = max(dp[j][k], dp[j - 1][k])
            if items[i][j] and k < 3:
                dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + items[i][j]) k reversed(range(4)) reversed range(4) range 4 dp[j][k] = max(dp[j][k], dp[j - 1][k]) dp[j][k] [j] dp j k max(dp[j][k], dp[j - 1][k]) max dp[j][k] [j] dp j k dp[j - 1][k] [j - 1] dp j - 1 j 1 k if items[i][j] and k < 3:
                dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + items[i][j]) items[i][j] and k < 3 items[i][j] [i] items i j k < 3 k 3 dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + items[i][j]) dp[j][k + 1] [j] dp j k + 1 k 1 max(dp[j][k + 1], dp[j][k] + items[i][j]) max dp[j][k + 1] [j] dp j k + 1 k 1 dp[j][k] + items[i][j] dp[j][k] [j] dp j k items[i][j] [i] items i j print(max(dp[-1])) print max(dp[-1]) max dp[-1] dp -1 R, C, K = map(int, input().split()) map(int, input().split()) R C, K = map(int, input().split()) map(int, input().split()) C K = map(int, input().split()) map(int, input().split()) K dp = [[0] * 4 for _ in range(C + 1)] [[0] * 4 for _ in range(C + 1)] dp items = [[0] * (C + 1) for _ in range(R + 1)] [[0] * (C + 1) for _ in range(R + 1)] items X = [list(map(int, input().split())) for _ in range(K)] [list(map(int, input().split())) for _ in range(K)] X

R, C, K = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(K)]

items = [[0] * (C + 1) for _ in range(R + 1)]
for r, c, v in X:
    items[r][c] = v

dp = [[0] * 4 for _ in range(C + 1)]
for i in range(1, R + 1):
    # Vertical transition
    for j in range(1, C + 1):
        dp[j][0] = max(dp[j])
        for k in range(1, 4):
            dp[j][k] = 0

    # Horizontal or picking-up transition
    for j in range(1, C + 1):
        for k in reversed(range(4)):
            dp[j][k] = max(dp[j][k], dp[j - 1][k])
            if items[i][j] and k < 3:
                dp[j][k + 1] = max(dp[j][k + 1], dp[j][k] + items[i][j])

print(max(dp[-1]))
