import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal, ROUND_CEILING
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

R, C, K = MAP()

dp1 = [[0]*4 for _ in range(C+1)]
dp2 = [[0]*4 for _ in range(C+1)]

field = [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)]

for _ in range(K):
    r, c, v = MAP()
    field[r][c] = v

for i in range(1, R+1):
    for j in range(1, C+1):
        if i%2 == 0:       
            up_max = max(dp1[j])
            p = field[i][j]
            dp2[j][0] = max(up_max, dp1[j-1][0])
            dp2[j][1] = max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p)
            dp2[j][2] = max(dp2[j-1][2], dp2[j-1][1]+p)
            dp2[j][3] = max(dp2[j-1][3], dp2[j-1][2]+p)
        else:
            up_max = max(dp2[j])
            p = field[i][j]
            dp1[j][0] = max(up_max, dp2[j-1][0])
            dp1[j][1] = max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p)
            dp1[j][2] = max(dp1[j-1][2], dp1[j-1][1]+p)
            dp1[j][3] = max(dp1[j-1][3], dp1[j-1][2]+p)

a = max(dp1[-1])
b = max(dp2[-1])
print(max(a, b)) import sys, re sys re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce from decimal import Decimal, ROUND_CEILING def input(): return sys.stdin.readline().strip() input def INT(): return int(input()) INT def MAP(): return map(int, input().split()) MAP def LIST(): return list(map(int, input().split())) LIST MAP() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) MAP() MAP def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP n n sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = float('inf') INF float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 R, C, K = MAP() R MAP() MAP C K [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 dp1 = [[0]*4 for _ in range(C+1)] dp1 [[0]*4 for _ in range(C+1)] [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 dp2 = [[0]*4 for _ in range(C+1)] dp2 [[0]*4 for _ in range(C+1)] [0]*(C+1) for _ in range(R) for _ in range(R) _ range(R) range R for _ in range(R) [0]*(C+1) [0] 0 C+1 C 1 field = [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)] field [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)] [[0]*(C+1)] [0]*(C+1) [0] 0 C+1 C 1 [[0]*(C+1) for _ in range(R)] for _ in range(K):
    r, c, v = MAP()
    field[r][c] = v _ range(K) range K r, c, v = MAP() r MAP() MAP c v field[r][c] = v field[r][c] [r] field r c v for i in range(1, R+1):
    for j in range(1, C+1):
        if i%2 == 0:       
            up_max = max(dp1[j])
            p = field[i][j]
            dp2[j][0] = max(up_max, dp1[j-1][0])
            dp2[j][1] = max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p)
            dp2[j][2] = max(dp2[j-1][2], dp2[j-1][1]+p)
            dp2[j][3] = max(dp2[j-1][3], dp2[j-1][2]+p)
        else:
            up_max = max(dp2[j])
            p = field[i][j]
            dp1[j][0] = max(up_max, dp2[j-1][0])
            dp1[j][1] = max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p)
            dp1[j][2] = max(dp1[j-1][2], dp1[j-1][1]+p)
            dp1[j][3] = max(dp1[j-1][3], dp1[j-1][2]+p) i range(1, R+1) range 1 R+1 R 1 for j in range(1, C+1):
        if i%2 == 0:       
            up_max = max(dp1[j])
            p = field[i][j]
            dp2[j][0] = max(up_max, dp1[j-1][0])
            dp2[j][1] = max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p)
            dp2[j][2] = max(dp2[j-1][2], dp2[j-1][1]+p)
            dp2[j][3] = max(dp2[j-1][3], dp2[j-1][2]+p)
        else:
            up_max = max(dp2[j])
            p = field[i][j]
            dp1[j][0] = max(up_max, dp2[j-1][0])
            dp1[j][1] = max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p)
            dp1[j][2] = max(dp1[j-1][2], dp1[j-1][1]+p)
            dp1[j][3] = max(dp1[j-1][3], dp1[j-1][2]+p) j range(1, C+1) range 1 C+1 C 1 if i%2 == 0:       
            up_max = max(dp1[j])
            p = field[i][j]
            dp2[j][0] = max(up_max, dp1[j-1][0])
            dp2[j][1] = max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p)
            dp2[j][2] = max(dp2[j-1][2], dp2[j-1][1]+p)
            dp2[j][3] = max(dp2[j-1][3], dp2[j-1][2]+p)
        else:
            up_max = max(dp2[j])
            p = field[i][j]
            dp1[j][0] = max(up_max, dp2[j-1][0])
            dp1[j][1] = max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p)
            dp1[j][2] = max(dp1[j-1][2], dp1[j-1][1]+p)
            dp1[j][3] = max(dp1[j-1][3], dp1[j-1][2]+p) i%2 == 0 i%2 i 2 0 up_max = max(dp1[j]) up_max max(dp1[j]) max dp1[j] dp1 j p = field[i][j] p field[i][j] [i] field i j dp2[j][0] = max(up_max, dp1[j-1][0]) dp2[j][0] [j] dp2 j 0 max(up_max, dp1[j-1][0]) max up_max dp1[j-1][0] [j-1] dp1 j-1 j 1 0 dp2[j][1] = max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p) dp2[j][1] [j] dp2 j 1 max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p) max up_max+p up_max p dp2[j-1][1] [j-1] dp2 j-1 j 1 1 dp2[j-1][0]+p dp2[j-1][0] [j-1] dp2 j-1 j 1 0 p dp2[j][2] = max(dp2[j-1][2], dp2[j-1][1]+p) dp2[j][2] [j] dp2 j 2 max(dp2[j-1][2], dp2[j-1][1]+p) max dp2[j-1][2] [j-1] dp2 j-1 j 1 2 dp2[j-1][1]+p dp2[j-1][1] [j-1] dp2 j-1 j 1 1 p dp2[j][3] = max(dp2[j-1][3], dp2[j-1][2]+p) dp2[j][3] [j] dp2 j 3 max(dp2[j-1][3], dp2[j-1][2]+p) max dp2[j-1][3] [j-1] dp2 j-1 j 1 3 dp2[j-1][2]+p dp2[j-1][2] [j-1] dp2 j-1 j 1 2 p up_max = max(dp2[j]) up_max max(dp2[j]) max dp2[j] dp2 j p = field[i][j] p field[i][j] [i] field i j dp1[j][0] = max(up_max, dp2[j-1][0]) dp1[j][0] [j] dp1 j 0 max(up_max, dp2[j-1][0]) max up_max dp2[j-1][0] [j-1] dp2 j-1 j 1 0 dp1[j][1] = max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p) dp1[j][1] [j] dp1 j 1 max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p) max up_max+p up_max p dp1[j-1][1] [j-1] dp1 j-1 j 1 1 dp1[j-1][0]+p dp1[j-1][0] [j-1] dp1 j-1 j 1 0 p dp1[j][2] = max(dp1[j-1][2], dp1[j-1][1]+p) dp1[j][2] [j] dp1 j 2 max(dp1[j-1][2], dp1[j-1][1]+p) max dp1[j-1][2] [j-1] dp1 j-1 j 1 2 dp1[j-1][1]+p dp1[j-1][1] [j-1] dp1 j-1 j 1 1 p dp1[j][3] = max(dp1[j-1][3], dp1[j-1][2]+p) dp1[j][3] [j] dp1 j 3 max(dp1[j-1][3], dp1[j-1][2]+p) max dp1[j-1][3] [j-1] dp1 j-1 j 1 3 dp1[j-1][2]+p dp1[j-1][2] [j-1] dp1 j-1 j 1 2 p a = max(dp1[-1]) a max(dp1[-1]) max dp1[-1] dp1 -1 b = max(dp2[-1]) b max(dp2[-1]) max dp2[-1] dp2 -1 print(max(a, b)) print max(a, b) max a b def LIST(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) LIST dp1 = [[0]*4 for _ in range(C+1)] [[0]*4 for _ in range(C+1)] dp1 C, K = MAP() MAP() C K = MAP() MAP() K field = [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)] [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)] field r, c, v = MAP() MAP() r up_max = max(dp1[j]) max(dp1[j]) up_max dp2 = [[0]*4 for _ in range(C+1)] [[0]*4 for _ in range(C+1)] dp2 v = MAP() MAP() v def INT(): return int(input()) def INT(): return int(input()) INT p = field[i][j] field[i][j] p def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP INF = float('inf') float('inf') INF a = max(dp1[-1]) max(dp1[-1]) a b = max(dp2[-1]) max(dp2[-1]) b def ZIP(n): return zip(*(MAP() for _ in range(n))) def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP mod = 10 ** 9 + 7 10 ** 9 + 7 mod R, C, K = MAP() MAP() R def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input c, v = MAP() MAP() c up_max = max(dp2[j]) max(dp2[j]) up_max p = field[i][j] field[i][j] p
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal, ROUND_CEILING
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

R, C, K = MAP()

dp1 = [[0]*4 for _ in range(C+1)]
dp2 = [[0]*4 for _ in range(C+1)]

field = [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)]

for _ in range(K):
    r, c, v = MAP()
    field[r][c] = v

for i in range(1, R+1):
    for j in range(1, C+1):
        if i%2 == 0:       
            up_max = max(dp1[j])
            p = field[i][j]
            dp2[j][0] = max(up_max, dp1[j-1][0])
            dp2[j][1] = max(up_max+p, dp2[j-1][1], dp2[j-1][0]+p)
            dp2[j][2] = max(dp2[j-1][2], dp2[j-1][1]+p)
            dp2[j][3] = max(dp2[j-1][3], dp2[j-1][2]+p)
        else:
            up_max = max(dp2[j])
            p = field[i][j]
            dp1[j][0] = max(up_max, dp2[j-1][0])
            dp1[j][1] = max(up_max+p, dp1[j-1][1], dp1[j-1][0]+p)
            dp1[j][2] = max(dp1[j-1][2], dp1[j-1][1]+p)
            dp1[j][3] = max(dp1[j-1][3], dp1[j-1][2]+p)

a = max(dp1[-1])
b = max(dp2[-1])
print(max(a, b))
