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()

dp = [[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):
    dp_next = [[0]*4 for _ in range(C+1)]
    for j in range(1, C+1):
        up_max = max(dp[j])
        p = field[i][j]
        dp_next[j][0] = max(up_max, dp[j-1][0])
        dp_next[j][1] = max(up_max+p, dp_next[j-1][1], dp_next[j-1][0]+p)
        dp_next[j][2] = max(dp_next[j-1][2], dp_next[j-1][1]+p)
        dp_next[j][3] = max(dp_next[j-1][3], dp_next[j-1][2]+p)
    dp = dp_next

print(max(dp[-1])) 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 dp = [[0]*4 for _ in range(C+1)] dp [[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):
    dp_next = [[0]*4 for _ in range(C+1)]
    for j in range(1, C+1):
        up_max = max(dp[j])
        p = field[i][j]
        dp_next[j][0] = max(up_max, dp[j-1][0])
        dp_next[j][1] = max(up_max+p, dp_next[j-1][1], dp_next[j-1][0]+p)
        dp_next[j][2] = max(dp_next[j-1][2], dp_next[j-1][1]+p)
        dp_next[j][3] = max(dp_next[j-1][3], dp_next[j-1][2]+p)
    dp = dp_next i range(1, R+1) range 1 R+1 R 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 dp_next = [[0]*4 for _ in range(C+1)] dp_next [[0]*4 for _ in range(C+1)] for j in range(1, C+1):
        up_max = max(dp[j])
        p = field[i][j]
        dp_next[j][0] = max(up_max, dp[j-1][0])
        dp_next[j][1] = max(up_max+p, dp_next[j-1][1], dp_next[j-1][0]+p)
        dp_next[j][2] = max(dp_next[j-1][2], dp_next[j-1][1]+p)
        dp_next[j][3] = max(dp_next[j-1][3], dp_next[j-1][2]+p)
     j range(1, C+1) range 1 C+1 C 1 up_max = max(dp[j]) up_max max(dp[j]) max dp[j] dp j p = field[i][j] p field[i][j] [i] field i j dp_next[j][0] = max(up_max, dp[j-1][0]) dp_next[j][0] [j] dp_next j 0 max(up_max, dp[j-1][0]) max up_max dp[j-1][0] [j-1] dp j-1 j 1 0 dp_next[j][1] = max(up_max+p, dp_next[j-1][1], dp_next[j-1][0]+p) dp_next[j][1] [j] dp_next j 1 max(up_max+p, dp_next[j-1][1], dp_next[j-1][0]+p) max up_max+p up_max p dp_next[j-1][1] [j-1] dp_next j-1 j 1 1 dp_next[j-1][0]+p dp_next[j-1][0] [j-1] dp_next j-1 j 1 0 p dp_next[j][2] = max(dp_next[j-1][2], dp_next[j-1][1]+p) dp_next[j][2] [j] dp_next j 2 max(dp_next[j-1][2], dp_next[j-1][1]+p) max dp_next[j-1][2] [j-1] dp_next j-1 j 1 2 dp_next[j-1][1]+p dp_next[j-1][1] [j-1] dp_next j-1 j 1 1 p dp_next[j][3] = max(dp_next[j-1][3], dp_next[j-1][2]+p) dp_next[j][3] [j] dp_next j 3 max(dp_next[j-1][3], dp_next[j-1][2]+p) max dp_next[j-1][3] [j-1] dp_next j-1 j 1 3 dp_next[j-1][2]+p dp_next[j-1][2] [j-1] dp_next j-1 j 1 2 p dp = dp_next dp dp_next print(max(dp[-1])) print max(dp[-1]) max dp[-1] dp -1 c, v = MAP() MAP() c field = [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)] [[0]*(C+1)] + [[0]*(C+1) for _ in range(R)] field v = MAP() MAP() v def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input def ZIP(n): return zip(*(MAP() for _ in range(n))) def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP C, K = MAP() MAP() C dp = dp_next dp_next dp r, c, v = MAP() MAP() r def INT(): return int(input()) def INT(): return int(input()) INT def LIST(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) LIST dp_next = [[0]*4 for _ in range(C+1)] [[0]*4 for _ in range(C+1)] dp_next up_max = max(dp[j]) max(dp[j]) up_max p = field[i][j] field[i][j] p R, C, K = MAP() MAP() R def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP INF = float('inf') float('inf') INF K = MAP() MAP() K mod = 10 ** 9 + 7 10 ** 9 + 7 mod dp = [[0]*4 for _ in range(C+1)] [[0]*4 for _ in range(C+1)] dp
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()

dp = [[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):
    dp_next = [[0]*4 for _ in range(C+1)]
    for j in range(1, C+1):
        up_max = max(dp[j])
        p = field[i][j]
        dp_next[j][0] = max(up_max, dp[j-1][0])
        dp_next[j][1] = max(up_max+p, dp_next[j-1][1], dp_next[j-1][0]+p)
        dp_next[j][2] = max(dp_next[j-1][2], dp_next[j-1][1]+p)
        dp_next[j][3] = max(dp_next[j-1][3], dp_next[j-1][2]+p)
    dp = dp_next

print(max(dp[-1]))
