import sys

from collections import defaultdict

range = xrange

input = raw_input



inp = [int(x) for x in sys.stdin.read().split()]; ii = 0



n = inp[ii]; ii += 1

m = inp[ii]; ii += 1

k = inp[ii]; ii += 1



R = inp[ii + 0: ii + 3 * k: 3]

C = inp[ii + 1: ii + 3 * k: 3]

V = inp[ii + 2: ii + 3 * k: 3]



items = [defaultdict(list) for _ in range(n)]

for i in range(k):

    r = R[i] - 1

    c = C[i] - 1

    v = V[i]

    items[r][c].append(v)



DP0 = [0] * m

DP1 = [0] * m

DP2 = [0] * m

DP3 = [0] * m



for r in range(n):

    for c in range(m):

        if c:

            DP0[c] = max(DP0[c], DP0[c - 1])

            DP1[c] = max(DP1[c], DP1[c - 1])

            DP2[c] = max(DP2[c], DP2[c - 1])

            DP3[c] = max(DP3[c], DP3[c - 1])

        if c in items[r]:

            for v in items[r][c]:

                DP3[c] = max(DP3[c], DP2[c] + v)

                DP2[c] = max(DP2[c], DP1[c] + v)

                DP1[c] = max(DP1[c], DP0[c] + v)

    for c in range(m):

        DP0[c] = max(DP0[c], DP1[c], DP2[c], DP3[c])

print(DP0[-1]) 

import sys

from collections import defaultdict

range = xrange

input = raw_input



inp = [int(x) for x in sys.stdin.read().split()]; ii = 0



n = inp[ii]; ii += 1

m = inp[ii]; ii += 1

k = inp[ii]; ii += 1



R = inp[ii + 0: ii + 3 * k: 3]

C = inp[ii + 1: ii + 3 * k: 3]

V = inp[ii + 2: ii + 3 * k: 3]



items = [defaultdict(list) for _ in range(n)]

for i in range(k):

    r = R[i] - 1

    c = C[i] - 1

    v = V[i]

    items[r][c].append(v)



DP0 = [0] * m

DP1 = [0] * m

DP2 = [0] * m

DP3 = [0] * m



for r in range(n):

    itemsr = items[r]

    for c in range(m):

        if c:

            DP0[c] = max(DP0[c], DP0[c - 1])

            DP1[c] = max(DP1[c], DP1[c - 1])

            DP2[c] = max(DP2[c], DP2[c - 1])

            DP3[c] = max(DP3[c], DP3[c - 1])

        if c in itemsr:

            for v in itemsr[c]:

                DP3[c] = max(DP3[c], DP2[c] + v)

                DP2[c] = max(DP2[c], DP1[c] + v)

                DP1[c] = max(DP1[c], DP0[c] + v)

    for c in range(m):

        DP0[c] = DP1[c] = DP2[c] = DP3[c]

print(DP0[-1]) 

import sys
from collections import defaultdict

range = xrange
input = raw_input
inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
n = inp[ii]
ii += 1
m = inp[ii]
ii += 1
k = inp[ii]
ii += 1
R = inp[ii + 0 : ii + 3 * k : 3]
C = inp[ii + 1 : ii + 3 * k : 3]
V = inp[ii + 2 : ii + 3 * k : 3]
items = [defaultdict(list) for _ in range(n)]
for i in range(k):
    r = R[i] - 1
    c = C[i] - 1
    v = V[i]
    items[r][c].append(v)
DP0 = [0] * m
DP1 = [0] * m
DP2 = [0] * m
DP3 = [0] * m
for r in range(n):
    for c in range(m):
        if c:
            DP0[c] = max(DP0[c], DP0[c - 1])
            DP1[c] = max(DP1[c], DP1[c - 1])
            DP2[c] = max(DP2[c], DP2[c - 1])
            DP3[c] = max(DP3[c], DP3[c - 1])
        if c in items[r]:
            for v in items[r][c]:
                DP3[c] = max(DP3[c], DP2[c] + v)
                DP2[c] = max(DP2[c], DP1[c] + v)
                DP1[c] = max(DP1[c], DP0[c] + v)
    for c in range(m):
        DP0[c] = max(DP0[c], DP1[c], DP2[c], DP3[c])
print(DP0[-1])

import sys
from collections import defaultdict

range = xrange
input = raw_input
inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
n = inp[ii]
ii += 1
m = inp[ii]
ii += 1
k = inp[ii]
ii += 1
R = inp[ii + 0 : ii + 3 * k : 3]
C = inp[ii + 1 : ii + 3 * k : 3]
V = inp[ii + 2 : ii + 3 * k : 3]
items = [defaultdict(list) for _ in range(n)]
for i in range(k):
    r = R[i] - 1
    c = C[i] - 1
    v = V[i]
    items[r][c].append(v)
DP0 = [0] * m
DP1 = [0] * m
DP2 = [0] * m
DP3 = [0] * m
for r in range(n):
    itemsr = items[r]
    for c in range(m):
        if c:
            DP0[c] = max(DP0[c], DP0[c - 1])
            DP1[c] = max(DP1[c], DP1[c - 1])
            DP2[c] = max(DP2[c], DP2[c - 1])
            DP3[c] = max(DP3[c], DP3[c - 1])
        if c in itemsr:
            for v in itemsr[c]:
                DP3[c] = max(DP3[c], DP2[c] + v)
                DP2[c] = max(DP2[c], DP1[c] + v)
                DP1[c] = max(DP1[c], DP0[c] + v)
    for c in range(m):
        DP0[c] = DP1[c] = DP2[c] = DP3[c]
print(DP0[-1])

+    itemsr = items[r] -        if c in items[r]: -            for v in items[r][c]: +        if c in itemsr: +            for v in itemsr[c]: -        DP0[c] = max(DP0[c], DP1[c], DP2[c], DP3[c]) +        DP0[c] = DP1[c] = DP2[c] = DP3[c]