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

def main():
    R, C, K = map(int, input().split())

    dp = [[[0, None, None, None] for j in range(C + 1)] for i in range(R + 1)]
    dp[0][0][0] = 0

    G = [[None for j in range(C)] for i in range(R)]

    for _ in range(K):
        r, c, v = map(int, input().split())
        G[r - 1][c - 1] = v

    for i in range(R):
        for j in range(C):
            if i == 0:
                break
            if G[i][j] is not None:
                if dp[i][j + 1][0] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][0]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][0])
                if dp[i][j + 1][1] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][1]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][1])
                if dp[i][j + 1][2] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][2]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][2])
                if dp[i][j + 1][3] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][3]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][3])
            else:

                if dp[i][j + 1][0] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][0])
                if dp[i][j + 1][1] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][1])
                if dp[i][j + 1][2] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][2])
                if dp[i][j + 1][3] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][3])

        for j in range(C):
            if G[i][j] is not None:

                if dp[i + 1][j + 1][0] is not None and dp[i + 1][j][0] is not None:
                    if dp[i + 1][j][1] is not None:
                        if dp[i + 1][j + 1][1] is not None:
                            dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][0] + G[i][j], dp[i + 1][j][1])
                        else:
                            dp[i + 1][j + 1][1] = max(dp[i + 1][j][1], dp[i + 1][j][0] + G[i][j])
                    else:
                        if dp[i + 1][j + 1][1] is not None:
                            dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][0] + G[i][j])
                        else:
                            dp[i + 1][j + 1][1] = dp[i + 1][j][0] + G[i][j]

                if dp[i + 1][j + 1][1] is not None and dp[i + 1][j][1] is not None:
                    if dp[i + 1][j][2] is not None:
                        if dp[i + 1][j + 1][2] is not None:
                            dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], dp[i + 1][j][1] + G[i][j], dp[i + 1][j][2])
                        else:
                            dp[i + 1][j + 1][2] = max(dp[i + 1][j][2], dp[i + 1][j][1] + G[i][j])
                    else:
                        if dp[i + 1][j + 1][2] is not None:
                            dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], dp[i + 1][j][1] + G[i][j])
                        else:
                            dp[i + 1][j + 1][2] = dp[i + 1][j][1] + G[i][j]

                if dp[i + 1][j + 1][2] is not None and dp[i + 1][j][2] is not None:
                    if dp[i + 1][j][3] is not None:
                        if dp[i + 1][j + 1][3] is not None:
                            dp[i + 1][j + 1][3] = max(dp[i + 1][j + 1][3], dp[i + 1][j][2] + G[i][j], dp[i + 1][j][3])
                        else:
                            dp[i + 1][j + 1][3] = max(dp[i + 1][j][3], dp[i + 1][j][2] + G[i][j])
                    else:
                        if dp[i + 1][j + 1][3] is not None:
                            dp[i + 1][j + 1][3] = max(dp[i + 1][j + 1][3], dp[i + 1][j][2] + G[i][j])
                        else:
                            dp[i + 1][j + 1][3] = dp[i + 1][j][2] + G[i][j]


            else:
                if dp[i + 1][j][0] is not None:
                    if dp[i + 1][j + 1][0] is not None:
                        dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i + 1][j][0])
                    else:
                        dp[i + 1][j + 1][0] = dp[i + 1][j][0]

                if dp[i + 1][j][1] is not None:
                    if dp[i + 1][j + 1][1] is not None:
                        dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][1])
                    else:
                        dp[i + 1][j + 1][1] = dp[i + 1][j][1]

                if dp[i + 1][j][2] is not None:
                    if dp[i + 1][j + 1][2] is not None:
                        dp[i + 1][j + 1][2] = max(dp[i + 1][j + 1][2], dp[i + 1][j][2])
                    else:
                        dp[i + 1][j + 1][2] = dp[i + 1][j][2]

                if dp[i + 1][j][3] is not None:
                    if dp[i + 1][j + 1][3] is not None:
                        dp[i + 1][j + 1][3] = max(dp[i + 1][j + 1][3], dp[i + 1][j][3])
                    else:
                        dp[i + 1][j + 1][3] = dp[i + 1][j][3]

    res = 0
    for d in dp[R][C]:
        if d is not None:
            res = max(res, d)

    print(res)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.buffer.readline

R, C, K = map(int, input().split())
G = [[None for j in range(C)] for i in range(R)]

for _ in range(K):
    r, c, v = map(int, input().split())
    G[r - 1][c - 1] = v

dp = [[0, 0, 0, 0] for _ in range(C)]

for i in range(R):
    for j in range(C):
        if G[i][j] is not None:
            dp[j][0], dp[j][1] = max(dp[j]), max(dp[j]) + G[i][j]
            dp[j][2], dp[j][3] = 0, 0
        else:
            dp[j][0] = max(dp[j])
            dp[j][1], dp[j][2], dp[j][3] = 0, 0, 0
    for j in range(1, C):
        if G[i][j] is not None:
            dp[j][0] = max(dp[j - 1][0], dp[j][0])
            dp[j][1] = max(dp[j - 1][1], dp[j - 1][0] + G[i][j], dp[j][1])
            if dp[j - 1][1] != 0:
                dp[j][2] = max(dp[j - 1][2], dp[j - 1][1] + G[i][j], dp[j][2])
            if dp[j - 1][2] != 0:
                dp[j][3] = max(dp[j - 1][3], dp[j - 1][2] + G[i][j], dp[j][3])
        else:
            dp[j][0] = max(dp[j - 1][0], dp[j][0])
            dp[j][1] = max(dp[j - 1][1], dp[j][1])
            dp[j][2] = max(dp[j - 1][2], dp[j][2])
            dp[j][3] = max(dp[j - 1][3], dp[j][3])

print(max(dp[-1]))

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

R, C, K = map(int, input().split())

dp = [[[0, None, None, None] for j in range(C + 1)] for i in range(R + 1)]
dp[0][0][0] = 0
dp[0][0][1] = 0

G = [[None for j in range(C)] for i in range(R)]

for _ in range(K):
    r, c, v = map(int, input().split())
    G[r - 1][c - 1] = v

for i in range(R):
    for j in range(C):
        if i == 0:
            break
        if G[i][j] is not None:
            for k in range(4):
                if dp[i][j + 1][k] is not None:
                    dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][0], dp[i][j + 1][k]) + G[i][j]
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][k])
        else:
            for k in range(4):
                if dp[i][j + 1][k] is not None:
                    dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][k])

    for j in range(C):
        if G[i][j] is not None:
            for k in range(3):
                if dp[i + 1][j + 1][k] is not None and dp[i + 1][j][k] is not None:
                    if dp[i + 1][j][k + 1] is not None:
                        if dp[i + 1][j + 1][k + 1] is not None:
                            dp[i + 1][j + 1][k + 1] = max(dp[i + 1][j + 1][k + 1], dp[i + 1][j][k] + G[i][j], dp[i + 1][j][k + 1])
                        else:
                            dp[i + 1][j + 1][k + 1] = max(dp[i + 1][j][k + 1], dp[i + 1][j][k] + G[i][j])
                    else:
                        if dp[i + 1][j + 1][k + 1] is not None:
                            dp[i + 1][j + 1][k + 1] = max(dp[i + 1][j + 1][k + 1], dp[i + 1][j][k] + G[i][j])
                        else:
                            dp[i + 1][j + 1][k + 1] = dp[i + 1][j][k] + G[i][j]
        else:
            for k in range(4):
                if dp[i + 1][j][k] is not None:
                    if dp[i + 1][j + 1][k] is not None:
                        dp[i + 1][j + 1][k] = max(dp[i + 1][j + 1][k], dp[i + 1][j][k])
                    else:
                        dp[i + 1][j + 1][k] = dp[i + 1][j][k]

res = 0
for d in dp[R][C]:
    if d is not None:
        res = max(res, d)

print(res)

Fixed submission:
import sys
input = sys.stdin.buffer.readline

R, C, K = map(int, input().split())
G = [[None for j in range(C)] for i in range(R)]

for _ in range(K):
    r, c, v = map(int, input().split())
    G[r - 1][c - 1] = v

dp = [[0, 0, 0, 0] for _ in range(C)]

for i in range(R):
    for j in range(C):
        if G[i][j] is not None:
            dp[j][0], dp[j][1] = max(dp[j]), max(dp[j]) + G[i][j]
            dp[j][2], dp[j][3] = 0, 0
        else:
            dp[j][0] = max(dp[j])
            dp[j][1], dp[j][2], dp[j][3] = 0, 0, 0
    for j in range(1, C):
        if G[i][j] is not None:
            dp[j][0] = max(dp[j - 1][0], dp[j][0])
            dp[j][1] = max(dp[j - 1][1], dp[j - 1][0] + G[i][j], dp[j][1])
            if dp[j - 1][1] != 0:
                dp[j][2] = max(dp[j - 1][2], dp[j - 1][1] + G[i][j], dp[j][2])
            if dp[j - 1][2] != 0:
                dp[j][3] = max(dp[j - 1][3], dp[j - 1][2] + G[i][j], dp[j][3])
        else:
            dp[j][0] = max(dp[j - 1][0], dp[j][0])
            dp[j][1] = max(dp[j - 1][1], dp[j][1])
            dp[j][2] = max(dp[j - 1][2], dp[j][2])
            dp[j][3] = max(dp[j - 1][3], dp[j][3])

print(max(dp[-1]))