import numpy as np
from numba import njit


@njit
def solve():
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    for i in range(1, R + 1):
        for j in range(1, C + 1):
            for k in range(4):
                dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][3])
            if items[i][j] == 0:
                continue
            for k in range(3, 0, -1):
                dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + items[i][j])
    return dp[R][C][-1]


R, C, K, *rcv = map(int, open(0).read().split())

items = np.zeros((R + 1, C + 1), dtype=np.int32)
for r, c, v in zip(*[iter(rcv)] * 3):
    items[r][c] = v

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


@njit
def solve():
    dp = np.zeros((R + 1, C + 1, 4), dtype=np.int64)
    for i in range(1, R + 1):
        for j in range(1, C + 1):
            for k in range(4):
                dp[i][j][k] = max(dp[i][j - 1][k], dp[i - 1][j][3])
            if items[i][j] == 0:
                continue
            for k in range(3, 0, -1):
                dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + items[i][j])
    return dp[R][C][-1]


R, C, K, *rcv = map(int, open(0).read().split())

items = np.zeros((R + 1, C + 1), dtype=np.int32)
for r, c, v in zip(*[iter(rcv)] * 3):
    items[r][c] = v

print(solve())
