Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
R, C, K = map(int, input().split())
item = {}
for _ in range(K):
    r, c, v = map(int, input().split())
    item[(r, c)] = v

dp = [[[0] * 4 for _ in range(3010)] for _ in range(3010)]
for r in range(1, R+1):
    for c in range(1, C+1):
        prer = max(dp[r-1][c])
        dp[r][c][0] = max(prer, dp[r][c-1][0])
        dp[r][c][1] = max(prer, dp[r][c-1][1])
        dp[r][c][2] = max(prer, dp[r][c-1][2])
        dp[r][c][3] = max(prer, dp[r][c-1][3])
        if item.get((r, c)):
            dp[r][c][1] = max(dp[r][c][1], max(prer, dp[r][c-1][0]) + item[(r,c)])
            dp[r][c][2] = max(dp[r][c][2], max(prer, dp[r][c-1][1]) + item[(r,c)])
            dp[r][c][3] = max(dp[r][c][3], max(prer, dp[r][c-1][2]) + item[(r,c)])

print(max(dp[R][C]))


Fixed submission:
R, C, K = map(int, input().split())
item = {}
for _ in range(K):
    r, c, v = map(int, input().split())
    item[(r, c)] = v

dp = [[0] * 4 for _ in range(3010)]
ndp = [[0] * 4 for _ in range(3010)]
for r in range(1, R+1):
    for c in range(1, C+1):
        prer = max(dp[c])
        ndp[c][0] = max(prer, ndp[c-1][0])
        ndp[c][1] = max(prer, ndp[c-1][1])
        ndp[c][2] = max(prer, ndp[c-1][2])
        ndp[c][3] = max(prer, ndp[c-1][3])
        if item.get((r, c)):
            ndp[c][1] = max(ndp[c][1], max(prer, ndp[c-1][0]) + item[(r,c)])
            ndp[c][2] = max(ndp[c][2], max(prer, ndp[c-1][1]) + item[(r,c)])
            ndp[c][3] = max(ndp[c][3], max(prer, ndp[c-1][2]) + item[(r,c)])
    dp = ndp
    ndp = [[0] * 4 for _ in range(3010)]

#print(dp)
print(max(dp[C]))

Buggy python submission (verdict: Time Limit Exceeded):
R, C, K = map(int, input().split())
item = [[0] * 3010 for _ in range(3010)]
for _ in range(K):
    r, c, v = map(int, input().split())
    item[r][c] = v

dp = [[[0] * 4 for _ in range(3010)] for _ in range(3010)]
for r in range(1, R+1):
    for c in range(1, C+1):
        prer = max(dp[r-1][c])
        dp[r][c][0] = max(prer, dp[r][c-1][0])
        dp[r][c][1] = max(prer, dp[r][c-1][1])
        dp[r][c][2] = max(prer, dp[r][c-1][2])
        dp[r][c][3] = max(prer, dp[r][c-1][3])
        if item[r][c] > 0:
            dp[r][c][1] = max(dp[r][c][1], max(prer, dp[r][c-1][0]) + item[r][c])
            dp[r][c][2] = max(dp[r][c][2], max(prer, dp[r][c-1][1]) + item[r][c])
            dp[r][c][3] = max(dp[r][c][3], max(prer, dp[r][c-1][2]) + item[r][c])

print(max(dp[R][C]))


Fixed submission:
R, C, K = map(int, input().split())
item = {}
for _ in range(K):
    r, c, v = map(int, input().split())
    item[(r, c)] = v

dp = [[0] * 4 for _ in range(3010)]
ndp = [[0] * 4 for _ in range(3010)]
for r in range(1, R+1):
    for c in range(1, C+1):
        prer = max(dp[c])
        ndp[c][0] = max(prer, ndp[c-1][0])
        ndp[c][1] = max(prer, ndp[c-1][1])
        ndp[c][2] = max(prer, ndp[c-1][2])
        ndp[c][3] = max(prer, ndp[c-1][3])
        if item.get((r, c)):
            ndp[c][1] = max(ndp[c][1], max(prer, ndp[c-1][0]) + item[(r,c)])
            ndp[c][2] = max(ndp[c][2], max(prer, ndp[c-1][1]) + item[(r,c)])
            ndp[c][3] = max(ndp[c][3], max(prer, ndp[c-1][2]) + item[(r,c)])
    dp = ndp
    ndp = [[0] * 4 for _ in range(3010)]

#print(dp)
print(max(dp[C]))