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

V = [[0]*(C+1) for _ in range(R+1)]
for _ in range(K):
    r, c, v = map(int, input().split())
    V[r][c] = v

dp0 = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0]*(C+1) for _ in range(R+1)]
dp2 = [[0]*(C+1) for _ in range(R+1)]
dp3 = [[0]*(C+1) for _ in range(R+1)]
for i in range(R+1):
    for j in range(C+1):
        v = V[i][j]

        a = dp0[i][j]
        b = dp0[i][j]
        c = dp0[i][j]
        d = dp0[i][j]

        pa = pb = pc = pd = 0
        if i > 0:
            pa = dp0[i-1][j]
            pb = dp1[i-1][j]
            pc = dp2[i-1][j]
            pd = dp3[i-1][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

        a = max(a, pa)
        b = max(b, pa+v)
        a = max(a, la)

        a = max(a, pb)
        b = max(b, pb+v)
        b = max(b, lb, la+v)

        a = max(a, pc)
        b = max(b, pc+v)
        c = max(c, lc, lb+v)

        a = max(a, pd)
        b = max(b, pd+v)
        d = max(d, ld, lc+v)

        dp0[i][j] = a
        dp1[i][j] = b
        dp2[i][j] = c
        dp3[i][j] = d

print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])) R, C, K = map(int, input().split()) R map(int, input().split()) map int input().split() ().split () input split 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 V = [[0]*(C+1) for _ in range(R+1)] V [[0]*(C+1) for _ in range(R+1)] for _ in range(K):
    r, c, v = map(int, input().split())
    V[r][c] = v _ range(K) range K r, c, v = map(int, input().split()) r map(int, input().split()) map int input().split() ().split () input split c v V[r][c] = v V[r][c] [r] V r c v [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 dp0 = [[0]*(C+1) for _ in range(R+1)] dp0 [[0]*(C+1) for _ in range(R+1)] [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 dp1 = [[0]*(C+1) for _ in range(R+1)] dp1 [[0]*(C+1) for _ in range(R+1)] [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 dp2 = [[0]*(C+1) for _ in range(R+1)] dp2 [[0]*(C+1) for _ in range(R+1)] [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 dp3 = [[0]*(C+1) for _ in range(R+1)] dp3 [[0]*(C+1) for _ in range(R+1)] for i in range(R+1):
    for j in range(C+1):
        v = V[i][j]

        a = dp0[i][j]
        b = dp0[i][j]
        c = dp0[i][j]
        d = dp0[i][j]

        pa = pb = pc = pd = 0
        if i > 0:
            pa = dp0[i-1][j]
            pb = dp1[i-1][j]
            pc = dp2[i-1][j]
            pd = dp3[i-1][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

        a = max(a, pa)
        b = max(b, pa+v)
        a = max(a, la)

        a = max(a, pb)
        b = max(b, pb+v)
        b = max(b, lb, la+v)

        a = max(a, pc)
        b = max(b, pc+v)
        c = max(c, lc, lb+v)

        a = max(a, pd)
        b = max(b, pd+v)
        d = max(d, ld, lc+v)

        dp0[i][j] = a
        dp1[i][j] = b
        dp2[i][j] = c
        dp3[i][j] = d i range(R+1) range R+1 R 1 for j in range(C+1):
        v = V[i][j]

        a = dp0[i][j]
        b = dp0[i][j]
        c = dp0[i][j]
        d = dp0[i][j]

        pa = pb = pc = pd = 0
        if i > 0:
            pa = dp0[i-1][j]
            pb = dp1[i-1][j]
            pc = dp2[i-1][j]
            pd = dp3[i-1][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

        a = max(a, pa)
        b = max(b, pa+v)
        a = max(a, la)

        a = max(a, pb)
        b = max(b, pb+v)
        b = max(b, lb, la+v)

        a = max(a, pc)
        b = max(b, pc+v)
        c = max(c, lc, lb+v)

        a = max(a, pd)
        b = max(b, pd+v)
        d = max(d, ld, lc+v)

        dp0[i][j] = a
        dp1[i][j] = b
        dp2[i][j] = c
        dp3[i][j] = d j range(C+1) range C+1 C 1 v = V[i][j] v V[i][j] [i] V i j a = dp0[i][j] a dp0[i][j] [i] dp0 i j b = dp0[i][j] b dp0[i][j] [i] dp0 i j c = dp0[i][j] c dp0[i][j] [i] dp0 i j d = dp0[i][j] d dp0[i][j] [i] dp0 i j pa = pb = pc = pd = 0 pa 0 = pb = pc = pd = 0 pb 0 pc pd if i > 0:
            pa = dp0[i-1][j]
            pb = dp1[i-1][j]
            pc = dp2[i-1][j]
            pd = dp3[i-1][j]

         i > 0 i 0 pa = dp0[i-1][j] pa dp0[i-1][j] [i-1] dp0 i-1 i 1 j pb = dp1[i-1][j] pb dp1[i-1][j] [i-1] dp1 i-1 i 1 j pc = dp2[i-1][j] pc dp2[i-1][j] [i-1] dp2 i-1 i 1 j pd = dp3[i-1][j] pd dp3[i-1][j] [i-1] dp3 i-1 i 1 j la = lb = lc = ld = 0 la 0 = lb = lc = ld = 0 lb 0 lc ld if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

         j > 0 j 0 la = dp0[i][j-1] la dp0[i][j-1] [i] dp0 i j-1 j 1 lb = dp1[i][j-1] lb dp1[i][j-1] [i] dp1 i j-1 j 1 lc = dp2[i][j-1] lc dp2[i][j-1] [i] dp2 i j-1 j 1 ld = dp3[i][j-1] ld dp3[i][j-1] [i] dp3 i j-1 j 1 a = max(a, pa) a max(a, pa) max a pa b = max(b, pa+v) b max(b, pa+v) max b pa+v pa v a = max(a, la) a max(a, la) max a la a = max(a, pb) a max(a, pb) max a pb b = max(b, pb+v) b max(b, pb+v) max b pb+v pb v b = max(b, lb, la+v) b max(b, lb, la+v) max b lb la+v la v a = max(a, pc) a max(a, pc) max a pc b = max(b, pc+v) b max(b, pc+v) max b pc+v pc v c = max(c, lc, lb+v) c max(c, lc, lb+v) max c lc lb+v lb v a = max(a, pd) a max(a, pd) max a pd b = max(b, pd+v) b max(b, pd+v) max b pd+v pd v d = max(d, ld, lc+v) d max(d, ld, lc+v) max d ld lc+v lc v dp0[i][j] = a dp0[i][j] [i] dp0 i j a dp1[i][j] = b dp1[i][j] [i] dp1 i j b dp2[i][j] = c dp2[i][j] [i] dp2 i j c dp3[i][j] = d dp3[i][j] [i] dp3 i j d print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C])) print max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]) max dp0[R][C] [R] dp0 R C dp1[R][C] [R] dp1 R C dp2[R][C] [R] dp2 R C dp3[R][C] [R] dp3 R C pa = pb = pc = pd = 0 0 pa dp2 = [[0]*(C+1) for _ in range(R+1)] [[0]*(C+1) for _ in range(R+1)] dp2 pc = dp2[i-1][j] dp2[i-1][j] pc d = max(d, ld, lc+v) max(d, ld, lc+v) d a = max(a, pb) max(a, pb) a a = max(a, pa) max(a, pa) a b = max(b, lb, la+v) max(b, lb, la+v) b a = max(a, pc) max(a, pc) a b = max(b, pd+v) max(b, pd+v) b la = lb = lc = ld = 0 0 la la = dp0[i][j-1] dp0[i][j-1] la R, C, K = map(int, input().split()) map(int, input().split()) R pb = pc = pd = 0 0 pb pb = dp1[i-1][j] dp1[i-1][j] pb lc = dp2[i][j-1] dp2[i][j-1] lc b = max(b, pc+v) max(b, pc+v) b a = max(a, la) max(a, la) a dp3 = [[0]*(C+1) for _ in range(R+1)] [[0]*(C+1) for _ in range(R+1)] dp3 a = max(a, pd) max(a, pd) a lc = ld = 0 0 lc dp0 = [[0]*(C+1) for _ in range(R+1)] [[0]*(C+1) for _ in range(R+1)] dp0 c = max(c, lc, lb+v) max(c, lc, lb+v) c lb = lc = ld = 0 0 lb a = dp0[i][j] dp0[i][j] a pd = dp3[i-1][j] dp3[i-1][j] pd pc = pd = 0 0 pc ld = 0 0 ld c, v = map(int, input().split()) map(int, input().split()) c pa = dp0[i-1][j] dp0[i-1][j] pa ld = dp3[i][j-1] dp3[i][j-1] ld r, c, v = map(int, input().split()) map(int, input().split()) r v = map(int, input().split()) map(int, input().split()) v d = dp0[i][j] dp0[i][j] d dp1 = [[0]*(C+1) for _ in range(R+1)] [[0]*(C+1) for _ in range(R+1)] dp1 c = dp0[i][j] dp0[i][j] c v = V[i][j] V[i][j] v b = dp0[i][j] dp0[i][j] b pd = 0 0 pd lb = dp1[i][j-1] dp1[i][j-1] lb V = [[0]*(C+1) for _ in range(R+1)] [[0]*(C+1) for _ in range(R+1)] V K = map(int, input().split()) map(int, input().split()) K b = max(b, pa+v) max(b, pa+v) b b = max(b, pb+v) max(b, pb+v) b C, K = map(int, input().split()) map(int, input().split()) C
R, C, K = map(int, input().split())

V = [[0]*(C+1) for _ in range(R+1)]
for _ in range(K):
    r, c, v = map(int, input().split())
    V[r][c] = v

dp0 = [[0]*(C+1) for _ in range(R+1)]
dp1 = [[0]*(C+1) for _ in range(R+1)]
dp2 = [[0]*(C+1) for _ in range(R+1)]
dp3 = [[0]*(C+1) for _ in range(R+1)]
for i in range(R+1):
    for j in range(C+1):
        v = V[i][j]

        a = dp0[i][j]
        b = dp0[i][j]
        c = dp0[i][j]
        d = dp0[i][j]

        pa = pb = pc = pd = 0
        if i > 0:
            pa = dp0[i-1][j]
            pb = dp1[i-1][j]
            pc = dp2[i-1][j]
            pd = dp3[i-1][j]

        la = lb = lc = ld = 0
        if j > 0:
            la = dp0[i][j-1]
            lb = dp1[i][j-1]
            lc = dp2[i][j-1]
            ld = dp3[i][j-1]

        a = max(a, pa)
        b = max(b, pa+v)
        a = max(a, la)

        a = max(a, pb)
        b = max(b, pb+v)
        b = max(b, lb, la+v)

        a = max(a, pc)
        b = max(b, pc+v)
        c = max(c, lc, lb+v)

        a = max(a, pd)
        b = max(b, pd+v)
        d = max(d, ld, lc+v)

        dp0[i][j] = a
        dp1[i][j] = b
        dp2[i][j] = c
        dp3[i][j] = d

print(max(dp0[R][C], dp1[R][C], dp2[R][C], dp3[R][C]))
