import numpy as np
from numba import njit, i8


@njit(i8(i8, i8, i8[:, :]), cache=True)
def solve(R, C, items):
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    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] + items[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])
    return dp[R][C - 1][0]


R, C, K, *rcv = map(int, open(0).read().split())
items = np.zeros((R + 1, C + 1), dtype=np.int64)
for r, c, v in zip(*[iter(rcv)] * 3):
    items[r - 1][c - 1] = v
print(solve(R, C, items)) import numpy as np numpy from numba import njit, i8 def solve(R, C, items):
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    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] + items[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])
    return dp[R][C - 1][0] solve dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64) dp np.zeros((R + 1, C + 1, 4), dtype=np.int64) np.zeros np zeros (R + 1, C + 1, 4) R + 1 R 1 C + 1 C 1 4 dtype=np.int64 dtype np.int64 np int64 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] + items[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])
     i range(R) 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] + items[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])
     j range(C) range C for k in range(2, -1, -1):
                dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + items[i][j])
             k range(2, -1, -1) range 2 -1 -1 dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + items[i][j]) dp[i][j][k + 1] [j] [i] dp i j k + 1 k 1 max(dp[i][j][k + 1], dp[i][j][k] + items[i][j]) max dp[i][j][k + 1] [j] [i] dp i j k + 1 k 1 dp[i][j][k] + items[i][j] dp[i][j][k] [j] [i] dp i j k items[i][j] [i] items 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])
     k range(4) range 4 dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]) dp[i + 1][j][0] [j] [i + 1] dp i + 1 i 1 j 0 max(dp[i + 1][j][0], dp[i][j][k]) max dp[i + 1][j][0] [j] [i + 1] dp i + 1 i 1 j 0 dp[i][j][k] [j] [i] dp i j k dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]) dp[i][j + 1][k] [j + 1] [i] dp i j + 1 j 1 k max(dp[i][j + 1][k], dp[i][j][k]) max dp[i][j + 1][k] [j + 1] [i] dp i j + 1 j 1 k dp[i][j][k] [j] [i] dp i j k return dp[R][C - 1][0] dp[R][C - 1][0] [C - 1] [R] dp R C - 1 C 1 0 R R C C items items @njit(i8(i8, i8, i8[:, :]), cache=True) solve i8(i8, i8, i8[:, :]), cache=True @njit(i8(i8, i8, i8[:, :]), cache=True) njit i8(i8, i8, i8[:, :]) i8 i8 i8 i8[:, :] i8 : cache=True cache True solve R, C, K, *rcv = map(int, open(0).read().split()) R map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split C K *rcv rcv items = np.zeros((R + 1, C + 1), dtype=np.int64) items np.zeros((R + 1, C + 1), dtype=np.int64) np.zeros np zeros (R + 1, C + 1) R + 1 R 1 C + 1 C 1 dtype=np.int64 dtype np.int64 np int64 for r, c, v in zip(*[iter(rcv)] * 3):
    items[r - 1][c - 1] = v r c v zip(*[iter(rcv)] * 3) zip *[iter(rcv)] * 3 [iter(rcv)] * 3 [iter(rcv)] iter(rcv) iter rcv 3 items[r - 1][c - 1] = v items[r - 1][c - 1] [r - 1] items r - 1 r 1 c - 1 c 1 v print(solve(R, C, items)) print solve(R, C, items) solve R C items R, C, K, *rcv = map(int, open(0).read().split()) map(int, open(0).read().split()) R C, K, *rcv = map(int, open(0).read().split()) map(int, open(0).read().split()) C def solve(R, C, items):
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    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] + items[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])
    return dp[R][C - 1][0] def solve(R, C, items):
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    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] + items[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])
    return dp[R][C - 1][0] solve items = np.zeros((R + 1, C + 1), dtype=np.int64) np.zeros((R + 1, C + 1), dtype=np.int64) items K, *rcv = map(int, open(0).read().split()) map(int, open(0).read().split()) K
import numpy as np
from numba import njit, i8


@njit(i8(i8, i8, i8[:, :]), cache=True)
def solve(R, C, items):
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    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] + items[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])
    return dp[R][C - 1][0]


R, C, K, *rcv = map(int, open(0).read().split())
items = np.zeros((R + 1, C + 1), dtype=np.int64)
for r, c, v in zip(*[iter(rcv)] * 3):
    items[r - 1][c - 1] = v
print(solve(R, C, items))
