import sys

input = sys.stdin.readline

def main(R,C,K,rcv):

  inf=float('inf')

  dp0=[[-inf,-inf,-inf,0] for _ in range(C)]

  item=[[0]*C for _ in range(R)]

  for r,c,v in rcv:

    item[r-1][c-1]=v

  # dp0[c][j]:c列目の時点で残りちょうどj個取れる時の最大値

  for i in range(R):

    # i+1行目に対する操作

    # 遷移

    for j in range(C):

      if j>0:

        dp0[j][3]=max(dp0[j])

        dp0[j][0]=dp0[j-1][0]

        dp0[j][1]=dp0[j-1][1]

        dp0[j][2]=dp0[j-1][2]

      else:

        dp0[0][3]=max(dp0[0])

        dp0[0][2]=-inf

        dp0[0][1]=-inf

        dp0[0][0]=-inf 

      if item[i][j]>0:

        v=item[i][j]

        dp0[j][0]=max(dp0[j][0],dp0[j][1]+v)

        dp0[j][1]=max(dp0[j][1],dp0[j][2]+v)

        dp0[j][2]=max(dp0[j][2],dp0[j][3]+v)

  print((max(dp0[-1])))

if __name__=='__main__':

  R,C,K=list(map(int,input().split()))

  rcv=[list(map(int,input().split())) for _ in range(K)]

  main(R,C,K,rcv)

R,C,K=list(map(int,input().split()))

rcv=[list(map(int,input().split())) for _ in range(K)]

mat=[[0]*C for _ in range(R)]

for r,c,v in rcv:

  mat[r-1][c-1]=v

dp=[0]*C

for i in range(R):

  ndp=[0]*C

  wdp=[0]*4

  for j in range(C):

    wdp[0]=max(wdp[0],dp[j])

    if mat[i][j]>0:

      v=mat[i][j]

      wdp[3]=max(wdp[3],wdp[2]+v)

      wdp[2]=max(wdp[2],wdp[1]+v)

      wdp[1]=max(wdp[1],wdp[0]+v)

    ndp[j]=max(wdp)

  dp=ndp.copy()

print((max(wdp)))
import sys

input = sys.stdin.readline


def main(R, C, K, rcv):
    inf = float("inf")
    dp0 = [[-inf, -inf, -inf, 0] for _ in range(C)]
    item = [[0] * C for _ in range(R)]
    for r, c, v in rcv:
        item[r - 1][c - 1] = v
    # dp0[c][j]:c列目の時点で残りちょうどj個取れる時の最大値
    for i in range(R):
        # i+1行目に対する操作
        # 遷移
        for j in range(C):
            if j > 0:
                dp0[j][3] = max(dp0[j])
                dp0[j][0] = dp0[j - 1][0]
                dp0[j][1] = dp0[j - 1][1]
                dp0[j][2] = dp0[j - 1][2]
            else:
                dp0[0][3] = max(dp0[0])
                dp0[0][2] = -inf
                dp0[0][1] = -inf
                dp0[0][0] = -inf
            if item[i][j] > 0:
                v = item[i][j]
                dp0[j][0] = max(dp0[j][0], dp0[j][1] + v)
                dp0[j][1] = max(dp0[j][1], dp0[j][2] + v)
                dp0[j][2] = max(dp0[j][2], dp0[j][3] + v)
    print((max(dp0[-1])))


if __name__ == "__main__":
    R, C, K = list(map(int, input().split()))
    rcv = [list(map(int, input().split())) for _ in range(K)]
    main(R, C, K, rcv)

R, C, K = list(map(int, input().split()))
rcv = [list(map(int, input().split())) for _ in range(K)]
mat = [[0] * C for _ in range(R)]
for r, c, v in rcv:
    mat[r - 1][c - 1] = v
dp = [0] * C
for i in range(R):
    ndp = [0] * C
    wdp = [0] * 4
    for j in range(C):
        wdp[0] = max(wdp[0], dp[j])
        if mat[i][j] > 0:
            v = mat[i][j]
            wdp[3] = max(wdp[3], wdp[2] + v)
            wdp[2] = max(wdp[2], wdp[1] + v)
            wdp[1] = max(wdp[1], wdp[0] + v)
        ndp[j] = max(wdp)
    dp = ndp.copy()
print((max(wdp)))

-import sys - -input = sys.stdin.readline - - -def main(R, C, K, rcv): -    inf = float("inf") -    dp0 = [[-inf, -inf, -inf, 0] for _ in range(C)] -    item = [[0] * C for _ in range(R)] -    for r, c, v in rcv: -        item[r - 1][c - 1] = v -    # dp0[c][j]:c列目の時点で残りちょうどj個取れる時の最大値 -    for i in range(R): -        # i+1行目に対する操作 -        # 遷移 -        for j in range(C): -            if j > 0: -                dp0[j][3] = max(dp0[j]) -                dp0[j][0] = dp0[j - 1][0] -                dp0[j][1] = dp0[j - 1][1] -                dp0[j][2] = dp0[j - 1][2] -            else: -                dp0[0][3] = max(dp0[0]) -                dp0[0][2] = -inf -                dp0[0][1] = -inf -                dp0[0][0] = -inf -            if item[i][j] > 0: -                v = item[i][j] -                dp0[j][0] = max(dp0[j][0], dp0[j][1] + v) -                dp0[j][1] = max(dp0[j][1], dp0[j][2] + v) -                dp0[j][2] = max(dp0[j][2], dp0[j][3] + v) -    print((max(dp0[-1]))) - - -if __name__ == "__main__": -    R, C, K = list(map(int, input().split())) -    rcv = [list(map(int, input().split())) for _ in range(K)] -    main(R, C, K, rcv) +R, C, K = list(map(int, input().split())) +rcv = [list(map(int, input().split())) for _ in range(K)] +mat = [[0] * C for _ in range(R)] +for r, c, v in rcv: +    mat[r - 1][c - 1] = v +dp = [0] * C +for i in range(R): +    ndp = [0] * C +    wdp = [0] * 4 +    for j in range(C): +        wdp[0] = max(wdp[0], dp[j]) +        if mat[i][j] > 0: +            v = mat[i][j] +            wdp[3] = max(wdp[3], wdp[2] + v) +            wdp[2] = max(wdp[2], wdp[1] + v) +            wdp[1] = max(wdp[1], wdp[0] + v) +        ndp[j] = max(wdp) +    dp = ndp.copy() +print((max(wdp)))