Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
def main():
    r,c,k = map(int,input().split())
    lst = [[0]*c for _ in range(r)]
    for i in range(k):
        a,b,v = map(int,input().split())
        lst[a-1][b-1] = v
    dp = [[[0]*4 for i in range(c)] for j in range(r)]
    dp[0][0][1] = lst[0][0]
    for i in range(r):
        for j in range(c):
            x,y = i+1,j
            s = max(dp[i][j])
            if x < r:
                dp[x][y][0] = s
                if dp[x][y][1] < lst[x][y]+s:
                    dp[x][y][1] = lst[x][y]+s
            x,y = i,j+1
            if y < c:
                if dp[i][j][2] != 0:
                    if dp[x][y][3] < dp[i][j][3]:
                        dp[x][y][3] = dp[i][j][3]
                    if dp[x][y][3] < dp[i][j][2]+lst[x][y] and lst[x][y] != 0:
                        dp[x][y][3] = dp[i][j][2]+lst[x][y]
            
                if dp[i][j][1] != 0:
                    if dp[x][y][2] < dp[i][j][2]:
                        dp[x][y][2] = dp[i][j][2]
                    if dp[x][y][2] < dp[i][j][1]+lst[x][y] and lst[x][y] != 0:
                        dp[x][y][2] = dp[i][j][1]+lst[x][y]
            
                if dp[x][y][1] < dp[i][j][1]:
                    dp[x][y][1] = dp[i][j][1]
                if dp[x][y][1] < dp[i][j][0]+lst[x][y] and lst[x][y] != 0:
                    dp[x][y][1] = dp[i][j][0]+lst[x][y]

                if dp[x][y][0] < dp[i][j][0]:
                    dp[x][y][0] = dp[i][j][0]
    print(max(dp[-1][-1]))

if __name__ == '__main__':
    main()

Fixed submission:
r,c,k = map(int,input().split())
lst = [[0]*c for _ in range(r)]
for i in range(k):
    a,b,v = map(int,input().split())
    lst[a-1][b-1] = v
dp0 = [[0]*c for _ in range(r)]
dp1 = [[0]*c for _ in range(r)]
dp2 = [[0]*c for _ in range(r)]
dp3 = [[0]*c for _ in range(r)]
dp1[0][0] = lst[0][0]

for i in range(r):
    for j in range(c):
        s = max(dp0[i][j], dp1[i][j], dp2[i][j], dp3[i][j])
        x,y = i+1,j
        if x < r:
            dp0[x][y] = max(dp0[x][y], s)
            if lst[x][y]:
                dp1[x][y] = max(dp1[x][y], s+lst[x][y])
        x,y = i,j+1
        if y < c:
            dp3[x][y] = max(dp3[i][j], dp3[x][y])
            if lst[x][y]:
                dp3[x][y] = max(dp2[i][j]+lst[x][y], dp3[x][y])
            
            dp2[x][y] = max(dp2[x][y], dp2[i][j])
            if lst[x][y]:
                dp2[x][y] = max(dp2[x][y], dp1[i][j]+lst[x][y])
            
            dp1[x][y] = max(dp1[x][y], dp1[i][j])
            if lst[x][y]:
                dp1[x][y] = max(dp1[x][y], dp0[i][j]+lst[x][y])
            
            dp0[x][y] = max(dp0[x][y], dp0[i][j])
print(max(dp0[-1][-1], dp1[-1][-1], dp2[-1][-1], dp3[-1][-1]))

Buggy python submission (verdict: Time Limit Exceeded):
r,c,k = map(int,input().split())
lst = [[0]*c for _ in range(r)]
for i in range(k):
    a,b,v = map(int,input().split())
    lst[a-1][b-1] = v
dp = [[[0]*4 for i in range(c)] for j in range(r)]
dp[0][0][1] = lst[0][0]
for i in range(r):
    for j in range(c):
        x,y = i+1,j
        s = max(dp[i][j])
        if x < r:
            dp[x][y][0] = s
            if dp[x][y][1] < lst[x][y]+s:
                dp[x][y][1] = lst[x][y]+s
        x,y = i,j+1
        if y < c:
            if dp[i][j][2] != 0:
                if dp[x][y][3] < dp[i][j][3]:
                    dp[x][y][3] = dp[i][j][3]
                if dp[x][y][3] < dp[i][j][2]+lst[x][y] and lst[x][y] != 0:
                    dp[x][y][3] = dp[i][j][2]+lst[x][y]
            
            if dp[i][j][1] != 0:
                if dp[x][y][2] < dp[i][j][2]:
                    dp[x][y][2] = dp[i][j][2]
                if dp[x][y][2] < dp[i][j][1]+lst[x][y] and lst[x][y] != 0:
                    dp[x][y][2] = dp[i][j][1]+lst[x][y]
            
            if dp[x][y][1] < dp[i][j][1]:
                dp[x][y][1] = dp[i][j][1]
            if dp[x][y][1] < dp[i][j][0]+lst[x][y] and lst[x][y] != 0:
                dp[x][y][1] = dp[i][j][0]+lst[x][y]

            if dp[x][y][0] < dp[i][j][0]:
                dp[x][y][0] = dp[i][j][0]
print(max(dp[-1][-1]))

Fixed submission:
r,c,k = map(int,input().split())
lst = [[0]*c for _ in range(r)]
for i in range(k):
    a,b,v = map(int,input().split())
    lst[a-1][b-1] = v
dp0 = [[0]*c for _ in range(r)]
dp1 = [[0]*c for _ in range(r)]
dp2 = [[0]*c for _ in range(r)]
dp3 = [[0]*c for _ in range(r)]
dp1[0][0] = lst[0][0]

for i in range(r):
    for j in range(c):
        s = max(dp0[i][j], dp1[i][j], dp2[i][j], dp3[i][j])
        x,y = i+1,j
        if x < r:
            dp0[x][y] = max(dp0[x][y], s)
            if lst[x][y]:
                dp1[x][y] = max(dp1[x][y], s+lst[x][y])
        x,y = i,j+1
        if y < c:
            dp3[x][y] = max(dp3[i][j], dp3[x][y])
            if lst[x][y]:
                dp3[x][y] = max(dp2[i][j]+lst[x][y], dp3[x][y])
            
            dp2[x][y] = max(dp2[x][y], dp2[i][j])
            if lst[x][y]:
                dp2[x][y] = max(dp2[x][y], dp1[i][j]+lst[x][y])
            
            dp1[x][y] = max(dp1[x][y], dp1[i][j])
            if lst[x][y]:
                dp1[x][y] = max(dp1[x][y], dp0[i][j]+lst[x][y])
            
            dp0[x][y] = max(dp0[x][y], dp0[i][j])
print(max(dp0[-1][-1], dp1[-1][-1], dp2[-1][-1], dp3[-1][-1]))