import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    R,C,K = LI()

    m = [[0] * (C+1) for _ in range(R+1)]
    dp = [[0] * 4 for _ in range(C+1)]
    for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

    for i in range(1,R+1):
        for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
    print(max(dp[-1]))

if __name__ == '__main__':
    main() import sys,queue,math,copy,itertools,bisect,collections,heapq sys queue math copy itertools bisect collections heapq def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    R,C,K = LI()

    m = [[0] * (C+1) for _ in range(R+1)]
    dp = [[0] * 4 for _ in range(C+1)]
    for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

    for i in range(1,R+1):
        for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
    print(max(dp[-1])) main int(x) for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() int(x) int x LI = lambda : [int(x) for x in sys.stdin.readline().split()] LI lambda : [int(x) for x in sys.stdin.readline().split()] [int(x) for x in sys.stdin.readline().split()] R,C,K = LI() R LI() LI C K [0] * (C+1) for _ in range(R+1) for _ in range(R+1) _ range(R+1) range R+1 R 1 for _ in range(R+1) [0] * (C+1) [0] 0 C+1 C 1 m = [[0] * (C+1) for _ in range(R+1)] m [[0] * (C+1) for _ in range(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 = [[0] * 4 for _ in range(C+1)] dp [[0] * 4 for _ in range(C+1)] for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

     _ range(K) range K r,c,v = LI() r LI() LI c v m[r][c] = v m[r][c] [r] m r c v for i in range(1,R+1):
        for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
     i range(1,R+1) range 1 R+1 R 1 for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
     j range(1,C+1) range 1 C+1 C 1 b_max = max(dp[j]) b_max max(dp[j]) max dp[j] dp j dp[j][0] = max(dp[j-1][0],b_max) dp[j][0] [j] dp j 0 max(dp[j-1][0],b_max) max dp[j-1][0] [j-1] dp j-1 j 1 0 b_max if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
     m[i][j] > 0 m[i][j] [i] m i j 0 for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                 k range(1,4) range 1 4 dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j]) dp[j][k] [j] dp j k max(dp[j-1][k],dp[j-1][k-1]+m[i][j]) max dp[j-1][k] [j-1] dp j-1 j 1 k dp[j-1][k-1]+m[i][j] dp[j-1][k-1] [j-1] dp j-1 j 1 k-1 k 1 m[i][j] [i] m i j dp[j][1] = max(dp[j][1],b_max+m[i][j]) dp[j][1] [j] dp j 1 max(dp[j][1],b_max+m[i][j]) max dp[j][1] [j] dp j 1 b_max+m[i][j] b_max m[i][j] [i] m i j for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
     k range(1,4) range 1 4 dp[j][k] = dp[j-1][k] dp[j][k] [j] dp j k dp[j-1][k] [j-1] dp j-1 j 1 k print(max(dp[-1])) print max(dp[-1]) max dp[-1] dp -1 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    R,C,K = LI()

    m = [[0] * (C+1) for _ in range(R+1)]
    dp = [[0] * 4 for _ in range(C+1)]
    for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

    for i in range(1,R+1):
        for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
    print(max(dp[-1])) def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    R,C,K = LI()

    m = [[0] * (C+1) for _ in range(R+1)]
    dp = [[0] * 4 for _ in range(C+1)]
    for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

    for i in range(1,R+1):
        for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
    print(max(dp[-1])) main
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    R,C,K = LI()

    m = [[0] * (C+1) for _ in range(R+1)]
    dp = [[0] * 4 for _ in range(C+1)]
    for _ in range(K):
        r,c,v = LI()
        m[r][c] = v

    for i in range(1,R+1):
        for j in range(1,C+1):
            b_max = max(dp[j])
            dp[j][0] = max(dp[j-1][0],b_max)
            if m[i][j] > 0:
                for k in range(1,4):
                    dp[j][k] = max(dp[j-1][k],dp[j-1][k-1]+m[i][j])
                dp[j][1] = max(dp[j][1],b_max+m[i][j])
            else:
                for k in range(1,4):
                    dp[j][k] = dp[j-1][k]
    print(max(dp[-1]))

if __name__ == '__main__':
    main()