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

dp = [[[0] * 4 for j in range(C)] for i in range(R)] 
#print(dp, item)


for i in range(R):
  for j in range(C):
    for k in range(4):
      if (i == 0) and (j == 0):
        continue
      else:
        if i == 0:
          dp[i][j][k] = dp[i][j - 1][k]
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])  
        elif j == 0:
          dp[i][j][k] = dp[i - 1][j][3]
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])           
        else:
          dp[i][j][k] = dp[i][j - 1][k]
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][3])
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])       
    #for k in range(3):
    #  dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k])
    
    for k in range(3):
          if (item[i][j] != 0):
            dp[i][j][3 - k] = max(dp[i][j][3 - k], dp[i][j][2 - k] + item[i][j])          

#print(item)            
#print(dp)
print(dp[-1][-1][-1])  



Fixed submission:
R,C,K = map(int,input().split())
J=[0] * K
for i in range(K):
  J[i] = list(map(int,input().split()))

#DP0 = [[0]*(C+2) for i in range(R+2)]
DP1 = [[0]*(C+2) for i in range(R+2)]
DP2 = [[0]*(C+2) for i in range(R+2)]
DP3 = [[0]*(C+2) for i in range(R+2)]

MAP = [[0]*C for i in range(R)]

for x,y,v in J:
    MAP[x-1][y-1] = v
#print(MAP)
for i in range(R):
    for j in range(C):
        DP1[i][j] = max(DP1[i][j-1],DP1[i-1][j]+MAP[i][j],DP2[i-1][j]+MAP[i][j],DP3[i-1][j]+MAP[i][j])
        DP2[i][j] = max(DP2[i][j-1],DP1[i][j-1]+MAP[i][j])
        DP3[i][j] = max(DP3[i][j-1],DP2[i][j-1]+MAP[i][j])

#print(DP1)
#print(DP2)
#print(DP3)
ANS=0
for i in range(R):
    for j in range(C):
        ANS=max(ANS,DP1[i][j],DP2[i][j],DP3[i][j])
print(ANS)

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

dp = [[[0] * 4 for j in range(C)] for i in range(R)] 
#print(dp, item)


for i in range(R):
  for j in range(C):
    for k in range(4):
      if (i == 0) and (j == 0):
        #if item[0][0] != 0:
        #  dp[0][0][1] = item[0][0]
        continue
      else:
        if i == 0:
          dp[i][j][k] = dp[i][j - 1][k]
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])  
        elif j == 0:
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k])
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])           
        else:
          dp[i][j][k] = dp[i][j - 1][k]
          dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k])
          #if (item[i][j] != 0) and (k != 0):
          #  dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + item[i][j])       
    for k in range(3):
      dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k])
    
    for k in range(3):
          if (item[i][j] != 0):
            dp[i][j][3 - k] = max(dp[i][j][3 - k], dp[i][j][2 - k] + item[i][j])          

#print(item)            
#print(dp)
print(dp[-1][-1][-1])  



Fixed submission:
R,C,K = map(int,input().split())
J=[0] * K
for i in range(K):
  J[i] = list(map(int,input().split()))

#DP0 = [[0]*(C+2) for i in range(R+2)]
DP1 = [[0]*(C+2) for i in range(R+2)]
DP2 = [[0]*(C+2) for i in range(R+2)]
DP3 = [[0]*(C+2) for i in range(R+2)]

MAP = [[0]*C for i in range(R)]

for x,y,v in J:
    MAP[x-1][y-1] = v
#print(MAP)
for i in range(R):
    for j in range(C):
        DP1[i][j] = max(DP1[i][j-1],DP1[i-1][j]+MAP[i][j],DP2[i-1][j]+MAP[i][j],DP3[i-1][j]+MAP[i][j])
        DP2[i][j] = max(DP2[i][j-1],DP1[i][j-1]+MAP[i][j])
        DP3[i][j] = max(DP3[i][j-1],DP2[i][j-1]+MAP[i][j])

#print(DP1)
#print(DP2)
#print(DP3)
ANS=0
for i in range(R):
    for j in range(C):
        ANS=max(ANS,DP1[i][j],DP2[i][j],DP3[i][j])
print(ANS)