Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
input = stdin.readline
(R,C,K) = map(int, input().split())
rcv = [list(map(int, input().split())) for _ in range(K)]

grids = [[0]*C for _ in range(R)]

for r,c,v in rcv:
    grids[r-1][c-1] = v

from collections import defaultdict
dp = defaultdict(int)

for r in range(R):
    for c in range(C):
        tmp = value = grids[r][c]
        for i in range(1,4):
            if c:
                tmp = max(tmp, dp[r,c-1,i])
                if i > 1:
                    tmp = max(tmp, dp[r,c-1,i-1] + value)
            if r and i == 1:
                tmp = max(tmp, dp[r-1,c,3] + value)
            dp[r,c,i] = tmp

print(dp[R-1,C-1,3])

Fixed submission:
# from sys import stdin
# input = stdin.readline
(R,C,K) = map(int, input().split())
rcv = [list(map(int, input().split())) for _ in range(K)]
 
grids = [[0]*C for _ in range(R)]
 
for r,c,v in rcv:
    grids[r-1][c-1] = v
 
dp = [0] * C

for r in range(R):
#     print(dp)
    ndp = [0] * C
    elm = [0] * 4
    for c in range(C):
        elm[0] = max(elm[0], dp[c])
        value = grids[r][c]
        if value:
            nelm = [0] * 4
            nelm[0] = elm[0]
#             print(f'{value=}')
#             print(f'{ elm=}')
#             print(f'{nelm=}')
            for i in range(3)[::-1]:
                nelm[i + 1] = max(elm[i + 1], elm[i] + value)
#             print(f'{value=}')
#             print(f'{ elm=}')
#             print(f'{nelm=}')
            elm = nelm
        ndp[c] = max(elm)
    dp = ndp
 

print(dp[-1])

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

(R,C,K) = map(int, input().split())
rcv = [list(map(int, input().split())) for _ in range(K)]

grids = [[0]*C for _ in range(R)]

for r,c,v in rcv:
    grids[r-1][c-1] = v

from collections import defaultdict
dp = defaultdict(int)

for r in range(R):
    for c in range(C):
        value = grids[r][c]
        for i in range(1,4):
            dp[r,c,i] = dp[r,c-1,i]
            if i == 1:
                dp[r,c,i] = max(dp[r,c,i], max(dp[r-1,c,j] for j in range(1,4)) + value)
            dp[r,c,i] = max(dp[r,c,i], dp[r,c-1,i-1] + value)

print(max(dp[R-1,C-1,j] for j in range(4)))

Fixed submission:
# from sys import stdin
# input = stdin.readline
(R,C,K) = map(int, input().split())
rcv = [list(map(int, input().split())) for _ in range(K)]
 
grids = [[0]*C for _ in range(R)]
 
for r,c,v in rcv:
    grids[r-1][c-1] = v
 
dp = [0] * C

for r in range(R):
#     print(dp)
    ndp = [0] * C
    elm = [0] * 4
    for c in range(C):
        elm[0] = max(elm[0], dp[c])
        value = grids[r][c]
        if value:
            nelm = [0] * 4
            nelm[0] = elm[0]
#             print(f'{value=}')
#             print(f'{ elm=}')
#             print(f'{nelm=}')
            for i in range(3)[::-1]:
                nelm[i + 1] = max(elm[i + 1], elm[i] + value)
#             print(f'{value=}')
#             print(f'{ elm=}')
#             print(f'{nelm=}')
            elm = nelm
        ndp[c] = max(elm)
    dp = ndp
 

print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
(R,C,K),*rcv = [list(map(int, s.split())) for s in open(0)]

grids = [[0]*C for _ in range(R)]

for r,c,v in rcv:
    grids[r-1][c-1] = v

from collections import defaultdict
dp = defaultdict(int)

for r in range(R):
    for c in range(C):
        value = grids[r][c]
        for i in range(1,4):
            dp[r,c,i] = dp[r,c-1,i]
            if i == 1:
                dp[r,c,i] = max(dp[r,c,i], max(dp[r-1,c,j] for j in range(1,4)) + value)
            dp[r,c,i] = max(dp[r,c,i], dp[r,c-1,i-1] + value)

print(max(dp[R-1,C-1,j] for j in range(4)))

Fixed submission:
# from sys import stdin
# input = stdin.readline
(R,C,K) = map(int, input().split())
rcv = [list(map(int, input().split())) for _ in range(K)]
 
grids = [[0]*C for _ in range(R)]
 
for r,c,v in rcv:
    grids[r-1][c-1] = v
 
dp = [0] * C

for r in range(R):
#     print(dp)
    ndp = [0] * C
    elm = [0] * 4
    for c in range(C):
        elm[0] = max(elm[0], dp[c])
        value = grids[r][c]
        if value:
            nelm = [0] * 4
            nelm[0] = elm[0]
#             print(f'{value=}')
#             print(f'{ elm=}')
#             print(f'{nelm=}')
            for i in range(3)[::-1]:
                nelm[i + 1] = max(elm[i + 1], elm[i] + value)
#             print(f'{value=}')
#             print(f'{ elm=}')
#             print(f'{nelm=}')
            elm = nelm
        ndp[c] = max(elm)
    dp = ndp
 

print(dp[-1])