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

R, C, k = map(int, input().split())
field = [[0] * C for _ in range(R)]
for _ in range(k):
    r, c, v = map(int, input().split())
    r -= 1; c -= 1
    field[r][c] = v

# dp[taken 0 - 2][R][C]
dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)]
dp0 = dp[0]; dp1 = dp[1]; dp2 = dp[2]; dp3 = dp[3]
dp[0][0] = 0
for j in range(C):
    for i in range(R):
        ij = i * C + j
        ij1 = ij + 1
        i1j = ij + C
        item = field[i][j]
        # Use
        if item:
            dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp0[ij]) 
        dp0[ij1] = max(dp0[ij1], dp0[ij]) 
        if item:
            dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp1[ij]) 
        dp1[ij1] = max(dp1[ij1], dp1[ij]) 
        if item:
            dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp2[ij]) 
        dp2[ij1] = max(dp2[ij1], dp2[ij]) 
        dp0[i1j] = max(dp0[i1j], dp3[ij]) 
        dp3[ij1] = max(dp3[ij1], dp3[ij]) 

ans = 0
for i in range(4):
    for j in range(R + 1):
        ans = max(ans, dp[i][j * C + C - 1])
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 range(R) for _ in range(R) _ range(R) range R for _ in range(R) [0] * C [0] 0 C field = [[0] * C for _ in range(R)] field [[0] * C for _ in range(R)] for _ in range(k):
    r, c, v = map(int, input().split())
    r -= 1; c -= 1
    field[r][c] = v

# dp[taken 0 - 2][R][C] _ range(k) range k r, c, v = map(int, input().split()) r map(int, input().split()) map int input().split() ().split () input split c v r -= 1 r 1 c -= 1 c 1 field[r][c] = v field[r][c] [r] field r c v [-1] * (C + 1) * (R + 1) for _ in range(4) for _ in range(4) _ range(4) range 4 for _ in range(4) [-1] * (C + 1) * (R + 1) [-1] * (C + 1) [-1] -1 C + 1 C 1 R + 1 R 1 dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)] dp [[-1] * (C + 1) * (R + 1) for _ in range(4)] dp0 = dp[0] dp0 dp[0] dp 0 dp1 = dp[1] dp1 dp[1] dp 1 dp2 = dp[2] dp2 dp[2] dp 2 dp3 = dp[3] dp3 dp[3] dp 3 dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for j in range(C):
    for i in range(R):
        ij = i * C + j
        ij1 = ij + 1
        i1j = ij + C
        item = field[i][j]
        # Use
        if item:
            dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp0[ij]) 
        dp0[ij1] = max(dp0[ij1], dp0[ij]) 
        if item:
            dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp1[ij]) 
        dp1[ij1] = max(dp1[ij1], dp1[ij]) 
        if item:
            dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp2[ij]) 
        dp2[ij1] = max(dp2[ij1], dp2[ij]) 
        dp0[i1j] = max(dp0[i1j], dp3[ij]) 
        dp3[ij1] = max(dp3[ij1], dp3[ij])  j range(C) range C for i in range(R):
        ij = i * C + j
        ij1 = ij + 1
        i1j = ij + C
        item = field[i][j]
        # Use
        if item:
            dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp0[ij]) 
        dp0[ij1] = max(dp0[ij1], dp0[ij]) 
        if item:
            dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp1[ij]) 
        dp1[ij1] = max(dp1[ij1], dp1[ij]) 
        if item:
            dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp2[ij]) 
        dp2[ij1] = max(dp2[ij1], dp2[ij]) 
        dp0[i1j] = max(dp0[i1j], dp3[ij]) 
        dp3[ij1] = max(dp3[ij1], dp3[ij])  i range(R) range R ij = i * C + j ij i * C + j i * C i C j ij1 = ij + 1 ij1 ij + 1 ij 1 i1j = ij + C i1j ij + C ij C item = field[i][j] item field[i][j] [i] field i j if item:
            dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
         item dp1[ij1] = max(dp1[ij1], dp0[ij] + item) dp1[ij1] dp1 ij1 max(dp1[ij1], dp0[ij] + item) max dp1[ij1] dp1 ij1 dp0[ij] + item dp0[ij] dp0 ij item dp0[i1j] = max(dp0[i1j], dp0[ij] + item) dp0[i1j] dp0 i1j max(dp0[i1j], dp0[ij] + item) max dp0[i1j] dp0 i1j dp0[ij] + item dp0[ij] dp0 ij item dp0[i1j] = max(dp0[i1j], dp0[ij]) dp0[i1j] dp0 i1j max(dp0[i1j], dp0[ij]) max dp0[i1j] dp0 i1j dp0[ij] dp0 ij dp0[ij1] = max(dp0[ij1], dp0[ij]) dp0[ij1] dp0 ij1 max(dp0[ij1], dp0[ij]) max dp0[ij1] dp0 ij1 dp0[ij] dp0 ij if item:
            dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
         item dp2[ij1] = max(dp2[ij1], dp1[ij] + item) dp2[ij1] dp2 ij1 max(dp2[ij1], dp1[ij] + item) max dp2[ij1] dp2 ij1 dp1[ij] + item dp1[ij] dp1 ij item dp0[i1j] = max(dp0[i1j], dp1[ij] + item) dp0[i1j] dp0 i1j max(dp0[i1j], dp1[ij] + item) max dp0[i1j] dp0 i1j dp1[ij] + item dp1[ij] dp1 ij item dp0[i1j] = max(dp0[i1j], dp1[ij]) dp0[i1j] dp0 i1j max(dp0[i1j], dp1[ij]) max dp0[i1j] dp0 i1j dp1[ij] dp1 ij dp1[ij1] = max(dp1[ij1], dp1[ij]) dp1[ij1] dp1 ij1 max(dp1[ij1], dp1[ij]) max dp1[ij1] dp1 ij1 dp1[ij] dp1 ij if item:
            dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
         item dp3[ij1] = max(dp3[ij1], dp2[ij] + item) dp3[ij1] dp3 ij1 max(dp3[ij1], dp2[ij] + item) max dp3[ij1] dp3 ij1 dp2[ij] + item dp2[ij] dp2 ij item dp0[i1j] = max(dp0[i1j], dp2[ij] + item) dp0[i1j] dp0 i1j max(dp0[i1j], dp2[ij] + item) max dp0[i1j] dp0 i1j dp2[ij] + item dp2[ij] dp2 ij item dp0[i1j] = max(dp0[i1j], dp2[ij]) dp0[i1j] dp0 i1j max(dp0[i1j], dp2[ij]) max dp0[i1j] dp0 i1j dp2[ij] dp2 ij dp2[ij1] = max(dp2[ij1], dp2[ij]) dp2[ij1] dp2 ij1 max(dp2[ij1], dp2[ij]) max dp2[ij1] dp2 ij1 dp2[ij] dp2 ij dp0[i1j] = max(dp0[i1j], dp3[ij]) dp0[i1j] dp0 i1j max(dp0[i1j], dp3[ij]) max dp0[i1j] dp0 i1j dp3[ij] dp3 ij dp3[ij1] = max(dp3[ij1], dp3[ij]) dp3[ij1] dp3 ij1 max(dp3[ij1], dp3[ij]) max dp3[ij1] dp3 ij1 dp3[ij] dp3 ij ans = 0 ans 0 for i in range(4):
    for j in range(R + 1):
        ans = max(ans, dp[i][j * C + C - 1]) i range(4) range 4 for j in range(R + 1):
        ans = max(ans, dp[i][j * C + C - 1]) j range(R + 1) range R + 1 R 1 ans = max(ans, dp[i][j * C + C - 1]) ans max(ans, dp[i][j * C + C - 1]) max ans dp[i][j * C + C - 1] [i] dp i j * C + C - 1 j * C + C j * C j C C 1 print(ans) print ans input = sys.stdin.readline sys.stdin.readline input ij = i * C + j i * C + j ij c, v = map(int, input().split()) map(int, input().split()) c r -= 1 1 r item = field[i][j] field[i][j] item k = map(int, input().split()) map(int, input().split()) k dp0 = dp[0] dp[0] dp0 c -= 1 1 c C, k = map(int, input().split()) map(int, input().split()) C dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)] [[-1] * (C + 1) * (R + 1) for _ in range(4)] dp v = map(int, input().split()) map(int, input().split()) v ans = max(ans, dp[i][j * C + C - 1]) max(ans, dp[i][j * C + C - 1]) ans dp2 = dp[2] dp[2] dp2 R, C, k = map(int, input().split()) map(int, input().split()) R i1j = ij + C ij + C i1j r, c, v = map(int, input().split()) map(int, input().split()) r dp3 = dp[3] dp[3] dp3 dp1 = dp[1] dp[1] dp1 ans = 0 0 ans ij1 = ij + 1 ij + 1 ij1 field = [[0] * C for _ in range(R)] [[0] * C for _ in range(R)] field
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

R, C, k = map(int, input().split())
field = [[0] * C for _ in range(R)]
for _ in range(k):
    r, c, v = map(int, input().split())
    r -= 1; c -= 1
    field[r][c] = v

# dp[taken 0 - 2][R][C]
dp = [[-1] * (C + 1) * (R + 1) for _ in range(4)]
dp0 = dp[0]; dp1 = dp[1]; dp2 = dp[2]; dp3 = dp[3]
dp[0][0] = 0
for j in range(C):
    for i in range(R):
        ij = i * C + j
        ij1 = ij + 1
        i1j = ij + C
        item = field[i][j]
        # Use
        if item:
            dp1[ij1] = max(dp1[ij1], dp0[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp0[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp0[ij]) 
        dp0[ij1] = max(dp0[ij1], dp0[ij]) 
        if item:
            dp2[ij1] = max(dp2[ij1], dp1[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp1[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp1[ij]) 
        dp1[ij1] = max(dp1[ij1], dp1[ij]) 
        if item:
            dp3[ij1] = max(dp3[ij1], dp2[ij] + item) 
            dp0[i1j] = max(dp0[i1j], dp2[ij] + item) 
        dp0[i1j] = max(dp0[i1j], dp2[ij]) 
        dp2[ij1] = max(dp2[ij1], dp2[ij]) 
        dp0[i1j] = max(dp0[i1j], dp3[ij]) 
        dp3[ij1] = max(dp3[ij1], dp3[ij]) 

ans = 0
for i in range(4):
    for j in range(R + 1):
        ans = max(ans, dp[i][j * C + C - 1])
print(ans)