Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n, m, q = map(int, input().split())
g = [[0 for _ in range(m)] for _ in range(n)]
for _ in range(q):
	r, c, v = list(map(int, input().split()))
	g[r-1][c-1] = v

dp = [[[0 for _ in range(4)] for _ in range(m)] for _ in range(n)]
dp[0][0][1] = g[0][0]
for i in range(n):
	for j in range(m):
		if i > 0:
			dp[i][j][0] = max(dp[i][j][0], max(dp[i-1][j]))
		if j > 0:
			dp[i][j][0] = max(dp[i][j][0], dp[i][j-1][0])
			for k in range(1, 4):
				dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
				dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + g[i][j])
			if i > 0:
				dp[i][j][1] = max(dp[i][j][1], max(dp[i-1][j]) + g[i][j])
		if i > 0 and j == 0:
			dp[i][0][1] = max(dp[i][0][1], max(dp[i-1][0]) + g[i][j])

print(max(dp[n-1][m-1]))

Fixed submission:
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n, m, q = map(int, input().split())
g = [[0 for _ in range(m)] for _ in range(n)]
for _ in range(q):
	r, c, v = list(map(int, input().split()))
	g[r-1][c-1] = v

pre = [0 for _ in range(m)]
dp = [[0 for _ in range(4)] for _ in range(m)]
dp[0][1] = g[0][0]
for i in range(n):
	for j in range(m):
		dp[j][0] = max(dp[j][0], pre[j])
		if j > 0:
			dp[j][0] = max(dp[j][0], dp[j-1][0])
			for k in range(1, 4):
				dp[j][k] = max(dp[j][k], dp[j-1][k])
				dp[j][k] = max(dp[j][k], dp[j-1][k-1] + g[i][j])
			if i > 0:
				dp[j][1] = max(dp[j][1], pre[j] + g[i][j])
		else:
			dp[0][1] = max(dp[0][1], pre[0] + g[i][j])
	pre = [max(dp[j]) for j in range(m)]
	dp = [[0 for _ in range(4)] for _ in range(m)]

print(pre[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n, m, q = map(int, input().split())
g = [[0 for _ in range(m)] for _ in range(n)]
for _ in range(q):
	r, c, v = list(map(int, input().split()))
	g[r-1][c-1] = v

dp = [[[0 for _ in range(4)] for _ in range(m)] for _ in range(n)]
dp[0][0][1] = g[0][0]
for i in range(n):
	for j in range(m):
		if i > 0:
			for k in range(4):
				dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][k])
		if j > 0:
			dp[i][j][0] = max(dp[i][j][0], dp[i][j-1][0])
			for k in range(1, 4):
				dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k])
		if g[i][j] > 0:
			if i > 0 and j == 0:
				dp[i][0][1] = max(dp[i][0][1], max(dp[i-1][0]) + g[i][j])
			elif j > 0:
				for k in range(1, 4):
					dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + g[i][j])
				if i > 0:
					dp[i][j][1] = max(dp[i][j][1], max(dp[i-1][j]) + g[i][j])

print(max(dp[n-1][m-1]))

Fixed submission:
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n, m, q = map(int, input().split())
g = [[0 for _ in range(m)] for _ in range(n)]
for _ in range(q):
	r, c, v = list(map(int, input().split()))
	g[r-1][c-1] = v

pre = [0 for _ in range(m)]
dp = [[0 for _ in range(4)] for _ in range(m)]
dp[0][1] = g[0][0]
for i in range(n):
	for j in range(m):
		dp[j][0] = max(dp[j][0], pre[j])
		if j > 0:
			dp[j][0] = max(dp[j][0], dp[j-1][0])
			for k in range(1, 4):
				dp[j][k] = max(dp[j][k], dp[j-1][k])
				dp[j][k] = max(dp[j][k], dp[j-1][k-1] + g[i][j])
			if i > 0:
				dp[j][1] = max(dp[j][1], pre[j] + g[i][j])
		else:
			dp[0][1] = max(dp[0][1], pre[0] + g[i][j])
	pre = [max(dp[j]) for j in range(m)]
	dp = [[0 for _ in range(4)] for _ in range(m)]

print(pre[-1])