import sys
import copy
from collections import deque

sys.setrecursionlimit(10**6)
input = sys.stdin.readline
#read = sys.stdin.buffer.read

def main():
    global field
    R, C, K = map(int, input().split())
    field = []
    for _ in range(R):
        tmp = [0] * C
        field.append(tmp)
    DP1 = copy.deepcopy(field)
    DP2 = copy.deepcopy(field)
    DP3 = copy.deepcopy(field)

    for _ in range(K):
        r, c, k = map(int, input().split())
        field[r-1][c-1] = k
    
    i = 0
    DP1[0][0] = field[0][0]
    for j in range(1, C):
        DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
        DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
        DP1[i][j] = max(field[i][j], DP1[i][j-1])

    for i in range(1, R):
        for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])

    ans = max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1])
    print(ans)


if __name__ == "__main__":
    main() import sys sys import copy copy from collections import deque sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def main():
    global field
    R, C, K = map(int, input().split())
    field = []
    for _ in range(R):
        tmp = [0] * C
        field.append(tmp)
    DP1 = copy.deepcopy(field)
    DP2 = copy.deepcopy(field)
    DP3 = copy.deepcopy(field)

    for _ in range(K):
        r, c, k = map(int, input().split())
        field[r-1][c-1] = k
    
    i = 0
    DP1[0][0] = field[0][0]
    for j in range(1, C):
        DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
        DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
        DP1[i][j] = max(field[i][j], DP1[i][j-1])

    for i in range(1, R):
        for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])

    ans = max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1])
    print(ans) main global field R, C, K = map(int, input().split()) R map(int, input().split()) map int input().split() ().split () input split C K field = [] field [] for _ in range(R):
        tmp = [0] * C
        field.append(tmp)
     _ range(R) range R tmp = [0] * C tmp [0] * C [0] 0 C field.append(tmp) field.append field append tmp DP1 = copy.deepcopy(field) DP1 copy.deepcopy(field) copy.deepcopy copy deepcopy field DP2 = copy.deepcopy(field) DP2 copy.deepcopy(field) copy.deepcopy copy deepcopy field DP3 = copy.deepcopy(field) DP3 copy.deepcopy(field) copy.deepcopy copy deepcopy field for _ in range(K):
        r, c, k = map(int, input().split())
        field[r-1][c-1] = k
    
     _ range(K) range K r, c, k = map(int, input().split()) r map(int, input().split()) map int input().split() ().split () input split c k field[r-1][c-1] = k field[r-1][c-1] [r-1] field r-1 r 1 c-1 c 1 k i = 0 i 0 DP1[0][0] = field[0][0] DP1[0][0] [0] DP1 0 0 field[0][0] [0] field 0 0 for j in range(1, C):
        DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
        DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
        DP1[i][j] = max(field[i][j], DP1[i][j-1])

     j range(1, C) range 1 C DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1]) DP3[i][j] [i] DP3 i j max(DP2[i][j-1] + field[i][j], DP3[i][j-1]) max DP2[i][j-1] + field[i][j] DP2[i][j-1] [i] DP2 i j-1 j 1 field[i][j] [i] field i j DP3[i][j-1] [i] DP3 i j-1 j 1 DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1]) DP2[i][j] [i] DP2 i j max(DP1[i][j-1] + field[i][j], DP2[i][j-1]) max DP1[i][j-1] + field[i][j] DP1[i][j-1] [i] DP1 i j-1 j 1 field[i][j] [i] field i j DP2[i][j-1] [i] DP2 i j-1 j 1 DP1[i][j] = max(field[i][j], DP1[i][j-1]) DP1[i][j] [i] DP1 i j max(field[i][j], DP1[i][j-1]) max field[i][j] [i] field i j DP1[i][j-1] [i] DP1 i j-1 j 1 for i in range(1, R):
        for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])

     i range(1, R) range 1 R for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])

     j range(C) range C DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1]) DP3[i][j] [i] DP3 i j max(DP2[i][j-1] + field[i][j], DP3[i][j-1]) max DP2[i][j-1] + field[i][j] DP2[i][j-1] [i] DP2 i j-1 j 1 field[i][j] [i] field i j DP3[i][j-1] [i] DP3 i j-1 j 1 DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1]) DP2[i][j] [i] DP2 i j max(DP1[i][j-1] + field[i][j], DP2[i][j-1]) max DP1[i][j-1] + field[i][j] DP1[i][j-1] [i] DP1 i j-1 j 1 field[i][j] [i] field i j DP2[i][j-1] [i] DP2 i j-1 j 1 DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j]) DP1[i][j] [i] DP1 i j max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j]) max field[i][j] [i] field i j DP1[i][j-1] [i] DP1 i j-1 j 1 max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j] max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) max DP1[i-1][j] [i-1] DP1 i-1 i 1 j DP2[i-1][j] [i-1] DP2 i-1 i 1 j DP3[i-1][j] [i-1] DP3 i-1 i 1 j field[i][j] [i] field i j ans = max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1]) ans max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1]) max DP3[R-1][C-1] [R-1] DP3 R-1 R 1 C-1 C 1 DP2[R-1][C-1] [R-1] DP2 R-1 R 1 C-1 C 1 DP1[R-1][C-1] [R-1] DP1 R-1 R 1 C-1 C 1 print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    global field
    R, C, K = map(int, input().split())
    field = []
    for _ in range(R):
        tmp = [0] * C
        field.append(tmp)
    DP1 = copy.deepcopy(field)
    DP2 = copy.deepcopy(field)
    DP3 = copy.deepcopy(field)

    for _ in range(K):
        r, c, k = map(int, input().split())
        field[r-1][c-1] = k
    
    i = 0
    DP1[0][0] = field[0][0]
    for j in range(1, C):
        DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
        DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
        DP1[i][j] = max(field[i][j], DP1[i][j-1])

    for i in range(1, R):
        for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])

    ans = max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1])
    print(ans) def main():
    global field
    R, C, K = map(int, input().split())
    field = []
    for _ in range(R):
        tmp = [0] * C
        field.append(tmp)
    DP1 = copy.deepcopy(field)
    DP2 = copy.deepcopy(field)
    DP3 = copy.deepcopy(field)

    for _ in range(K):
        r, c, k = map(int, input().split())
        field[r-1][c-1] = k
    
    i = 0
    DP1[0][0] = field[0][0]
    for j in range(1, C):
        DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
        DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
        DP1[i][j] = max(field[i][j], DP1[i][j-1])

    for i in range(1, R):
        for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])

    ans = max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1])
    print(ans) main input = sys.stdin.readline sys.stdin.readline input
import sys
import copy
from collections import deque

sys.setrecursionlimit(10**6)
input = sys.stdin.readline
#read = sys.stdin.buffer.read

def main():
    global field
    R, C, K = map(int, input().split())
    field = []
    for _ in range(R):
        tmp = [0] * C
        field.append(tmp)
    DP1 = copy.deepcopy(field)
    DP2 = copy.deepcopy(field)
    DP3 = copy.deepcopy(field)

    for _ in range(K):
        r, c, k = map(int, input().split())
        field[r-1][c-1] = k
    
    i = 0
    DP1[0][0] = field[0][0]
    for j in range(1, C):
        DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
        DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
        DP1[i][j] = max(field[i][j], DP1[i][j-1])

    for i in range(1, R):
        for j in range(C):
            DP3[i][j] = max(DP2[i][j-1] + field[i][j], DP3[i][j-1])
            DP2[i][j] = max(DP1[i][j-1] + field[i][j], DP2[i][j-1])
            DP1[i][j] = max(field[i][j], DP1[i][j-1], max(DP1[i-1][j], DP2[i-1][j], DP3[i-1][j]) + field[i][j])

    ans = max(DP3[R-1][C-1], DP2[R-1][C-1], DP1[R-1][C-1])
    print(ans)


if __name__ == "__main__":
    main()
