Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline


r, c, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(k)]
INF = 10 ** 18

item = [[0] * c for i in range(r)]
for i, j, v in info:
    i -= 1
    j -= 1
    item[i][j] = v

OFFSET = (r + 1)
dp = [[-INF] * 4 for i in range((c + 1) * (r + 1))]
for i in range(r + 1):
    dp[i][0] = 0
for j in range(c + 1):
    dp[OFFSET * j][0] = 0
    
for i in range(r):
    for j in range(c):
        val = item[i][j]
        tmp11 = i + 1 + OFFSET * (j + 1)
        tmp = i + 1 + OFFSET * j
        tmp01 = i + OFFSET * (j + 1)

        dp[tmp11][0] = max(dp[tmp][0], dp[tmp01][0], dp[tmp01][1], dp[tmp01][2], dp[tmp01][3])
        dp[tmp11][1] = max(dp[tmp][1], dp[tmp][0] + val,  dp[tmp01][0] + val, dp[tmp01][1] + val, dp[tmp01][2] + val, dp[tmp01][3] + val)
        dp[tmp11][2] = max(dp[tmp][2], dp[tmp][1] + val)
        dp[tmp11][3] = max(dp[tmp][3], dp[tmp][2] + val)
          
print(max(dp[-1]))
                                   

Fixed submission:
import sys
input = sys.stdin.buffer.readline


r, c, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(k)]
INF = 10 ** 18

item = [[0] * c for i in range(r)]
for i, j, v in info:
    i -= 1
    j -= 1
    item[i][j] = v

OFFSET = (r + 1)
dp = [[-INF] * ((c + 1) * (r + 1)) for i in range(4)]
for i in range(r + 1):
    dp[0][i] = 0
for j in range(c + 1):
    dp[0][OFFSET * j] = 0
    
for i in range(r):
    for j in range(c):
        val = item[i][j]
        tmp11 = i + 1 + OFFSET * (j + 1)
        tmp = i + 1 + OFFSET * j
        tmp01 = i + OFFSET * (j + 1)

        dp[0][tmp11] = max(dp[0][tmp], dp[0][tmp01], dp[1][tmp01], dp[2][tmp01], dp[3][tmp01])
        dp[1][tmp11] = max(dp[1][tmp], dp[0][tmp] + val,  dp[0][tmp01] + val, dp[1][tmp01] + val, dp[2][tmp01] + val, dp[3][tmp01] + val)
        dp[2][tmp11] = max(dp[2][tmp], dp[1][tmp] + val)
        dp[3][tmp11] = max(dp[3][tmp], dp[2][tmp] + val)


print(max(dp[0][(c + 1) * (r + 1) - 1], dp[1][(c + 1) * (r + 1) - 1], dp[2][(c + 1) * (r + 1) - 1], dp[3][(c + 1) * (r + 1) - 1]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline


r, c, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(k)]
INF = 10 ** 18

item = [[0] * c for i in range(r)]
for i, j, v in info:
    i -= 1
    j -= 1
    item[i][j] = v

dp = [[[-INF] * 4 for i in range(c + 1)] for i in range(r + 1)]
for i in range(r + 1):
    dp[i][0][0] = 0
for j in range(c + 1):
    dp[0][j][0] = 0
    
for i in range(r):
    for j in range(c):
        val = item[i][j]
        dp[i + 1][j + 1][0] = max(dp[i + 1][j][0], dp[i][j + 1][0], dp[i][j + 1][1], dp[i][j + 1][2], dp[i][j + 1][3])
        dp[i + 1][j + 1][1] = max(dp[i + 1][j][1], dp[i + 1][j][0] + val,  dp[i][j + 1][0] + val, dp[i][j + 1][1] + val, dp[i][j + 1][2] + val, dp[i][j + 1][3] + val)
        dp[i + 1][j + 1][2] = max(dp[i + 1][j][2], dp[i + 1][j][1] + val)
        dp[i + 1][j + 1][3] = max(dp[i + 1][j][3], dp[i + 1][j][2] + val)
                                   
print(max(dp[-1][-1]))
                                   

Fixed submission:
import sys
input = sys.stdin.buffer.readline


r, c, k = map(int, input().split())
info = [list(map(int, input().split())) for i in range(k)]
INF = 10 ** 18

item = [[0] * c for i in range(r)]
for i, j, v in info:
    i -= 1
    j -= 1
    item[i][j] = v

OFFSET = (r + 1)
dp = [[-INF] * ((c + 1) * (r + 1)) for i in range(4)]
for i in range(r + 1):
    dp[0][i] = 0
for j in range(c + 1):
    dp[0][OFFSET * j] = 0
    
for i in range(r):
    for j in range(c):
        val = item[i][j]
        tmp11 = i + 1 + OFFSET * (j + 1)
        tmp = i + 1 + OFFSET * j
        tmp01 = i + OFFSET * (j + 1)

        dp[0][tmp11] = max(dp[0][tmp], dp[0][tmp01], dp[1][tmp01], dp[2][tmp01], dp[3][tmp01])
        dp[1][tmp11] = max(dp[1][tmp], dp[0][tmp] + val,  dp[0][tmp01] + val, dp[1][tmp01] + val, dp[2][tmp01] + val, dp[3][tmp01] + val)
        dp[2][tmp11] = max(dp[2][tmp], dp[1][tmp] + val)
        dp[3][tmp11] = max(dp[3][tmp], dp[2][tmp] + val)


print(max(dp[0][(c + 1) * (r + 1) - 1], dp[1][(c + 1) * (r + 1) - 1], dp[2][(c + 1) * (r + 1) - 1], dp[3][(c + 1) * (r + 1) - 1]))