import numba
from numba import jit
import numpy as np
 
#(i,j)→(i+1,j)のように動くときはアイテムを入手できる回数がリセットされる
#dp[列][今、何個アイテムを取ったか]でdpテーブルを作る
@jit
def main(r, c):
  dp = np.zeros((c+1, 4), np.int64)
  for i in range(r):
    new_dp = np.zeros((c+1, 4), np.int64)
    for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
    dp = new_dp
    for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
  return max(dp[c-1,:])
 
R, C, K = map(int, input().split())
item_lis = list(list(map(int, input().split())) for i in range(K))
item_map = np.zeros((R, C), np.int64)
for i,j,k in item_lis:
  item_map[i-1,j-1] = k
 
print(main(R, C)) import numba numba from numba import jit import numpy as np numpy def main(r, c):
  dp = np.zeros((c+1, 4), np.int64)
  for i in range(r):
    new_dp = np.zeros((c+1, 4), np.int64)
    for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
    dp = new_dp
    for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
  return max(dp[c-1,:])
  main dp = np.zeros((c+1, 4), np.int64) dp np.zeros((c+1, 4), np.int64) np.zeros np zeros (c+1, 4) c+1 c 1 4 np.int64 np int64 for i in range(r):
    new_dp = np.zeros((c+1, 4), np.int64)
    for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
    dp = new_dp
    for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
   i range(r) range r new_dp = np.zeros((c+1, 4), np.int64) new_dp np.zeros((c+1, 4), np.int64) np.zeros np zeros (c+1, 4) c+1 c 1 4 np.int64 np int64 for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
     j range(4) range 4 new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j]) new_dp[:,0] new_dp : np.maximum(new_dp[:,0], dp[:,j]) np.maximum np maximum new_dp[:,0] new_dp : dp[:,j] dp : dp = new_dp dp new_dp for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
   k range(c) range c for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
       l range(2, -1, -1) range 2 -1 -1 dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k]) dp[k,l+1] dp k max(dp[k,l+1], dp[k,l]+item_map[i,k]) max dp[k,l+1] dp k dp[k,l]+item_map[i,k] dp[k,l] dp k item_map[i,k] item_map i for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
   m range(1,4) range 1 4 dp[k+1,m] = max(dp[k+1,m], dp[k,m]) dp[k+1,m] dp k+1 k 1 max(dp[k+1,m], dp[k,m]) max dp[k+1,m] dp k+1 k 1 dp[k,m] dp k return max(dp[c-1,:]) max(dp[c-1,:]) max dp[c-1,:] dp c-1 c 1 r r c c @jit main jit main R, C, K = map(int, input().split()) R map(int, input().split()) map int input().split() ().split () input split C K item_lis = list(list(map(int, input().split())) for i in range(K)) item_lis list(list(map(int, input().split())) for i in range(K)) list list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split item_map = np.zeros((R, C), np.int64) item_map np.zeros((R, C), np.int64) np.zeros np zeros (R, C) R C np.int64 np int64 for i,j,k in item_lis:
  item_map[i-1,j-1] = k
  i j k item_lis item_map[i-1,j-1] = k item_map[i-1,j-1] item_map i-1 i 1 k print(main(R, C)) print main(R, C) main R C item_lis = list(list(map(int, input().split())) for i in range(K)) list(list(map(int, input().split())) for i in range(K)) item_lis K = map(int, input().split()) map(int, input().split()) K R, C, K = map(int, input().split()) map(int, input().split()) R def main(r, c):
  dp = np.zeros((c+1, 4), np.int64)
  for i in range(r):
    new_dp = np.zeros((c+1, 4), np.int64)
    for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
    dp = new_dp
    for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
  return max(dp[c-1,:])
  def main(r, c):
  dp = np.zeros((c+1, 4), np.int64)
  for i in range(r):
    new_dp = np.zeros((c+1, 4), np.int64)
    for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
    dp = new_dp
    for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
  return max(dp[c-1,:])
  main C, K = map(int, input().split()) map(int, input().split()) C item_map = np.zeros((R, C), np.int64) np.zeros((R, C), np.int64) item_map
import numba
from numba import jit
import numpy as np
 
#(i,j)→(i+1,j)のように動くときはアイテムを入手できる回数がリセットされる
#dp[列][今、何個アイテムを取ったか]でdpテーブルを作る
@jit
def main(r, c):
  dp = np.zeros((c+1, 4), np.int64)
  for i in range(r):
    new_dp = np.zeros((c+1, 4), np.int64)
    for j in range(4):
      new_dp[:,0] = np.maximum(new_dp[:,0], dp[:,j])
    dp = new_dp
    for k in range(c):
      for l in range(2, -1, -1):
        dp[k,l+1] = max(dp[k,l+1], dp[k,l]+item_map[i,k])
      for m in range(1,4):
        dp[k+1,m] = max(dp[k+1,m], dp[k,m])
  return max(dp[c-1,:])
 
R, C, K = map(int, input().split())
item_lis = list(list(map(int, input().split())) for i in range(K))
item_map = np.zeros((R, C), np.int64)
for i,j,k in item_lis:
  item_map[i-1,j-1] = k
 
print(main(R, C))