Buggy python submission (verdict: Runtime Error):
def main():
    c=[[0]*3001for _ in range(3001)]
    h,w,k=map(int,input().split())
    for i in range(k):
        y,x,v=map(int,input().split())
        c[y][x]=v
    dp=[[0]*(w+1)for _ in range(h+1)]
    for i in range(1,h+1):
        x=y=z=p=0
        for j in range(1,w+1):
            v=c[i][j]
            if v>x:x,y,z=v,x,y
            elif v>y:y,z=v,y
            elif v>z:z=v
            if x+y+z+p<=dp[i-1][j]+v:
                x,y,z=v,0,0
                p=dp[i-1][j]
            dp[i][j]=x+y+z+p
    a=dp[h][w]
    dp=[[[0]*(w+1)for _ in range(h+1)]for _ in range(4)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            x=max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])
            for k in range(4):
                if k:dp[k][i][j]=max(dp[k][i][j-1],dp[k-1][i][j-1]+c[i][j])
                else:dp[k][i][j]=max(dp[k][i][j-1],x)
            dp[1][i][j]=max(dp[1][i][j],x+c[i][j])
    if a<max(dp[0][h][w],dp[1][h][w],dp[2][h][w],dp[3][h][w]):1/0
    else:print(a)
main()

Fixed submission:
c=[[0]*3001for _ in range(3001)]
dp=[[[0]*3001for _ in range(3001)]for _ in range(4)]
h,w,k=map(int,input().split())
for i in range(k):
    y,x,v=map(int,input().split())
    c[y][x]=v
for i in range(1,h+1):
    for j in range(1,w+1):
        x=max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])
        for k in range(4):
            if k:dp[k][i][j]=max(dp[k][i][j-1],dp[k-1][i][j-1]+c[i][j])
            else:dp[k][i][j]=max(dp[k][i][j-1],x)
        dp[1][i][j]=max(dp[1][i][j],x+c[i][j])
print(max(dp[0][h][w],dp[1][h][w],dp[2][h][w],dp[3][h][w]))

Buggy python submission (verdict: Runtime Error):
def main():
    c=[[0]*3001for _ in range(3001)]
    h,w,k=map(int,input().split())
    for i in range(k):
        y,x,v=map(int,input().split())
        c[y][x]=v
    dp=[[0]*(w+1)for _ in range(h+1)]
    for i in range(1,h+1):
        x=y=z=p=0
        for j in range(1,w+1):
            v=c[i][j]
            if v>x:x,y,z=v,x,y
            elif v>y:y,z=v,y
            elif v>z:z=v
            if x+y+z+p<=dp[i-1][j]+v:
                x,y,z=v,0,0
                p=dp[i-1][j]
            dp[i][j]=x+y+z+p
    a=dp[h][w]
    dp=[[[0]*(w+1)for _ in range(h+1)]for _ in range(4)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            x=max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])
            for k in range(4):
                if k:dp[k][i][j]=max(dp[k][i][j-1],dp[k-1][i][j-1]+c[i][j])
                else:dp[k][i][j]=max(dp[k][i][j-1],x)
            dp[1][i][j]=max(dp[1][i][j],x+c[i][j])
    if a<max(dp[0][h][w],dp[1][h][w],dp[2][h][w],dp[3][h][w]):1/0
main()

Fixed submission:
c=[[0]*3001for _ in range(3001)]
dp=[[[0]*3001for _ in range(3001)]for _ in range(4)]
h,w,k=map(int,input().split())
for i in range(k):
    y,x,v=map(int,input().split())
    c[y][x]=v
for i in range(1,h+1):
    for j in range(1,w+1):
        x=max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])
        for k in range(4):
            if k:dp[k][i][j]=max(dp[k][i][j-1],dp[k-1][i][j-1]+c[i][j])
            else:dp[k][i][j]=max(dp[k][i][j-1],x)
        dp[1][i][j]=max(dp[1][i][j],x+c[i][j])
print(max(dp[0][h][w],dp[1][h][w],dp[2][h][w],dp[3][h][w]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    (h,w,k),*t=[[*map(int,t.split())]for t in open(0)]
    dp=[[[0]*4for _ in range(w+1)]for _ in range(h+1)]
    c=[[0]*(w+1)for _ in range(h+1)]
    for y,x,v in t:c[y][x]=v
    for i in range(1,h+1):
        for j in range(1,w+1):
            for k in range(4):dp[i][j][k]=dp[i][j-1][k]
            for k in range(1,4):dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]+c[i][j])
            k=c[i][j]>0
            dp[i][j][k]=max(dp[i][j][k],max(dp[i-1][j])+c[i][j])
    print(max(dp[h][w]))
main()

Fixed submission:
c=[[0]*3001for _ in range(3001)]
dp=[[[0]*3001for _ in range(3001)]for _ in range(4)]
h,w,k=map(int,input().split())
for i in range(k):
    y,x,v=map(int,input().split())
    c[y][x]=v
for i in range(1,h+1):
    for j in range(1,w+1):
        x=max(dp[0][i-1][j],dp[1][i-1][j],dp[2][i-1][j],dp[3][i-1][j])
        for k in range(4):
            if k:dp[k][i][j]=max(dp[k][i][j-1],dp[k-1][i][j-1]+c[i][j])
            else:dp[k][i][j]=max(dp[k][i][j-1],x)
        dp[1][i][j]=max(dp[1][i][j],x+c[i][j])
print(max(dp[0][h][w],dp[1][h][w],dp[2][h][w],dp[3][h][w]))