import sys

input=sys.stdin.readline

def main():

    r,c,k=map(int,input().split())

    v=[[0]*c for _ in range(r)]

    for _ in range(k):

        ri,ci,a=map(int,input().split())

        ri-=1

        ci-=1

        v[ri][ci]=a

    dp=[[[0]*c for _ in range(r)] for i in range(4)]

    #print(dp)

    if v[0][0]>0:

        dp[1][0][0]=v[0][0]



    for i in range(r):

        for j in range(c):

            if i>0:

                dp[1][i][j]=max(dp[1][i][j],dp[0][i-1][j]+v[i][j])

                dp[0][i][j]=max(dp[0][i][j],dp[0][i-1][j])

                dp[1][i][j]=max(dp[1][i][j],dp[1][i-1][j]+v[i][j])

                dp[0][i][j]=max(dp[0][i][j],dp[1][i-1][j])

                dp[1][i][j]=max(dp[1][i][j],dp[2][i-1][j]+v[i][j])

                dp[0][i][j]=max(dp[0][i][j],dp[2][i-1][j])

                dp[1][i][j]=max(dp[1][i][j],dp[3][i-1][j]+v[i][j])

                dp[0][i][j]=max(dp[0][i][j],dp[3][i-1][j])

            if j>0:

                dp[1][i][j]=max(dp[1][i][j],dp[0][i][j-1]+v[i][j])

                dp[2][i][j]=max(dp[2][i][j],dp[1][i][j-1]+v[i][j])

                dp[3][i][j]=max(dp[3][i][j],dp[2][i][j-1]+v[i][j])

                dp[0][i][j]=max(dp[0][i][j],dp[0][i][j-1])

                dp[1][i][j]=max(dp[1][i][j],dp[1][i][j-1])

                dp[2][i][j]=max(dp[2][i][j],dp[2][i][j-1])

                dp[3][i][j]=max(dp[3][i][j],dp[3][i][j-1])

    #print(dp)

    ans=0

    for i in range(4):

        ans=max(dp[i][r-1][c-1],ans)

    return print(ans)

if __name__=="__main__":

    main()



import sys

input=sys.stdin.readline

def main():

    r,c,k=map(int,input().split())

    v=[0] * (c * r)

    for _ in range(k):

        ri,ci,a=map(int,input().split())

        v[(ri-1)*c + (ci-1)]=a

    dp= [0] * (c * r * 4)

    #print(dp)

    x = c * r

    if v[0]>0: dp[x] = v[0]



    for i in range(r):

        for j in range(c):

            idx = i*c+j

            idx2 = (i-1)*c + j

            idx3 = i*c + j-1

            if i>0:

                dp[x + idx] = max(dp[x + idx], dp[idx2]+v[idx])

                dp[idx] = max(dp[idx],dp[idx2])

                dp[x + idx]=max(dp[x + idx],dp[x + idx2]+v[idx])

                dp[idx]=max(dp[idx],dp[x + idx2])

                dp[x + idx]=max(dp[x + idx],dp[2*x + idx2]+v[idx])

                dp[idx]=max(dp[idx],dp[2*x + idx2])

                dp[x + idx]=max(dp[x + idx],dp[3*x + idx2]+v[idx])

                dp[idx]=max(dp[idx],dp[3*x + idx2])

            if j>0:

                dp[x + idx]=max(dp[x + idx],dp[idx3]+v[idx])

                dp[2*x + idx]=max(dp[2*x + idx],dp[x + idx3]+v[idx])

                dp[3*x + idx]=max(dp[3*x + idx],dp[2*x + idx3]+v[idx])

                dp[idx]=max(dp[idx],dp[idx3])

                dp[x + idx]=max(dp[x + idx],dp[x + idx3])

                dp[2*x + idx]=max(dp[2*x + idx],dp[2*x + idx3])

                dp[3*x + idx]=max(dp[3*x + idx],dp[3*x + idx3])



    #print(dp)

    ans=0

    for i in range(4): ans=max(dp[i*x + (r-1)*c + (c-1)],ans)

    return print(ans)



if __name__=="__main__":

    main()



import sys

input = sys.stdin.readline


def main():
    r, c, k = map(int, input().split())
    v = [[0] * c for _ in range(r)]
    for _ in range(k):
        ri, ci, a = map(int, input().split())
        ri -= 1
        ci -= 1
        v[ri][ci] = a
    dp = [[[0] * c for _ in range(r)] for i in range(4)]
    # print(dp)
    if v[0][0] > 0:
        dp[1][0][0] = v[0][0]
    for i in range(r):
        for j in range(c):
            if i > 0:
                dp[1][i][j] = max(dp[1][i][j], dp[0][i - 1][j] + v[i][j])
                dp[0][i][j] = max(dp[0][i][j], dp[0][i - 1][j])
                dp[1][i][j] = max(dp[1][i][j], dp[1][i - 1][j] + v[i][j])
                dp[0][i][j] = max(dp[0][i][j], dp[1][i - 1][j])
                dp[1][i][j] = max(dp[1][i][j], dp[2][i - 1][j] + v[i][j])
                dp[0][i][j] = max(dp[0][i][j], dp[2][i - 1][j])
                dp[1][i][j] = max(dp[1][i][j], dp[3][i - 1][j] + v[i][j])
                dp[0][i][j] = max(dp[0][i][j], dp[3][i - 1][j])
            if j > 0:
                dp[1][i][j] = max(dp[1][i][j], dp[0][i][j - 1] + v[i][j])
                dp[2][i][j] = max(dp[2][i][j], dp[1][i][j - 1] + v[i][j])
                dp[3][i][j] = max(dp[3][i][j], dp[2][i][j - 1] + v[i][j])
                dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1])
                dp[1][i][j] = max(dp[1][i][j], dp[1][i][j - 1])
                dp[2][i][j] = max(dp[2][i][j], dp[2][i][j - 1])
                dp[3][i][j] = max(dp[3][i][j], dp[3][i][j - 1])
    # print(dp)
    ans = 0
    for i in range(4):
        ans = max(dp[i][r - 1][c - 1], ans)
    return print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline


def main():
    r, c, k = map(int, input().split())
    v = [0] * (c * r)
    for _ in range(k):
        ri, ci, a = map(int, input().split())
        v[(ri - 1) * c + (ci - 1)] = a
    dp = [0] * (c * r * 4)
    # print(dp)
    x = c * r
    if v[0] > 0:
        dp[x] = v[0]
    for i in range(r):
        for j in range(c):
            idx = i * c + j
            idx2 = (i - 1) * c + j
            idx3 = i * c + j - 1
            if i > 0:
                dp[x + idx] = max(dp[x + idx], dp[idx2] + v[idx])
                dp[idx] = max(dp[idx], dp[idx2])
                dp[x + idx] = max(dp[x + idx], dp[x + idx2] + v[idx])
                dp[idx] = max(dp[idx], dp[x + idx2])
                dp[x + idx] = max(dp[x + idx], dp[2 * x + idx2] + v[idx])
                dp[idx] = max(dp[idx], dp[2 * x + idx2])
                dp[x + idx] = max(dp[x + idx], dp[3 * x + idx2] + v[idx])
                dp[idx] = max(dp[idx], dp[3 * x + idx2])
            if j > 0:
                dp[x + idx] = max(dp[x + idx], dp[idx3] + v[idx])
                dp[2 * x + idx] = max(dp[2 * x + idx], dp[x + idx3] + v[idx])
                dp[3 * x + idx] = max(dp[3 * x + idx], dp[2 * x + idx3] + v[idx])
                dp[idx] = max(dp[idx], dp[idx3])
                dp[x + idx] = max(dp[x + idx], dp[x + idx3])
                dp[2 * x + idx] = max(dp[2 * x + idx], dp[2 * x + idx3])
                dp[3 * x + idx] = max(dp[3 * x + idx], dp[3 * x + idx3])
    # print(dp)
    ans = 0
    for i in range(4):
        ans = max(dp[i * x + (r - 1) * c + (c - 1)], ans)
    return print(ans)


if __name__ == "__main__":
    main()

-    v = [[0] * c for _ in range(r)] +    v = [0] * (c * r) -        ri -= 1 -        ci -= 1 -        v[ri][ci] = a -    dp = [[[0] * c for _ in range(r)] for i in range(4)] +        v[(ri - 1) * c + (ci - 1)] = a +    dp = [0] * (c * r * 4) -    if v[0][0] > 0: -        dp[1][0][0] = v[0][0] +    x = c * r +    if v[0] > 0: +        dp[x] = v[0] +            idx = i * c + j +            idx2 = (i - 1) * c + j +            idx3 = i * c + j - 1 -                dp[1][i][j] = max(dp[1][i][j], dp[0][i - 1][j] + v[i][j]) -                dp[0][i][j] = max(dp[0][i][j], dp[0][i - 1][j]) -                dp[1][i][j] = max(dp[1][i][j], dp[1][i - 1][j] + v[i][j]) -                dp[0][i][j] = max(dp[0][i][j], dp[1][i - 1][j]) -                dp[1][i][j] = max(dp[1][i][j], dp[2][i - 1][j] + v[i][j]) -                dp[0][i][j] = max(dp[0][i][j], dp[2][i - 1][j]) -                dp[1][i][j] = max(dp[1][i][j], dp[3][i - 1][j] + v[i][j]) -                dp[0][i][j] = max(dp[0][i][j], dp[3][i - 1][j]) +                dp[x + idx] = max(dp[x + idx], dp[idx2] + v[idx]) +                dp[idx] = max(dp[idx], dp[idx2]) +                dp[x + idx] = max(dp[x + idx], dp[x + idx2] + v[idx]) +                dp[idx] = max(dp[idx], dp[x + idx2]) +                dp[x + idx] = max(dp[x + idx], dp[2 * x + idx2] + v[idx]) +                dp[idx] = max(dp[idx], dp[2 * x + idx2]) +                dp[x + idx] = max(dp[x + idx], dp[3 * x + idx2] + v[idx]) +                dp[idx] = max(dp[idx], dp[3 * x + idx2]) -                dp[1][i][j] = max(dp[1][i][j], dp[0][i][j - 1] + v[i][j]) -                dp[2][i][j] = max(dp[2][i][j], dp[1][i][j - 1] + v[i][j]) -                dp[3][i][j] = max(dp[3][i][j], dp[2][i][j - 1] + v[i][j]) -                dp[0][i][j] = max(dp[0][i][j], dp[0][i][j - 1]) -                dp[1][i][j] = max(dp[1][i][j], dp[1][i][j - 1]) -                dp[2][i][j] = max(dp[2][i][j], dp[2][i][j - 1]) -                dp[3][i][j] = max(dp[3][i][j], dp[3][i][j - 1]) +                dp[x + idx] = max(dp[x + idx], dp[idx3] + v[idx]) +                dp[2 * x + idx] = max(dp[2 * x + idx], dp[x + idx3] + v[idx]) +                dp[3 * x + idx] = max(dp[3 * x + idx], dp[2 * x + idx3] + v[idx]) +                dp[idx] = max(dp[idx], dp[idx3]) +                dp[x + idx] = max(dp[x + idx], dp[x + idx3]) +                dp[2 * x + idx] = max(dp[2 * x + idx], dp[2 * x + idx3]) +                dp[3 * x + idx] = max(dp[3 * x + idx], dp[3 * x + idx3]) -        ans = max(dp[i][r - 1][c - 1], ans) +        ans = max(dp[i * x + (r - 1) * c + (c - 1)], ans)