Problem p03053 (python)

Fixed submission:
H, W = map(int, input().split())
masu = []
for i in range(H):
    masu.append(list(input()))
black = []
for i in range(H):
    for j in range(W):
        if masu[i][j] == "#":
            black.append([i,j])
ans = -1
while len(black) != 0:
    ans += 1
    blacktemp = black[:]
    black = []
    for k in blacktemp:
        for i, j in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
            if 0 <= k[0] + i <= H-1 and 0 <= k[1] + j <= W-1:
                if masu[k[0]+i][k[1]+j] == ".":
                    masu[k[0]+i][k[1]+j] = "#"
                    black.append([k[0]+i, k[1]+j])
print(ans)

Buggy submission (verdict: Runtime Error):
H, W, N = map(int, input().split())
r, c = map(int, input().split())
S = input()
T = input()




S_power = [[0, 0, 0, 0] for i in range(N+1)] #L, R, U, D
for i in range(N):
    if S[i] == "L":
        S_power[i][0] += 1
    elif S[i] == "R":
        S_power[i][1] += 1
    elif S[i] == "U":
        S_power[i][2] += 1
    else:
        S_power[i][3] += 1

T_power = [[0, 0, 0, 0] for i in range(N+1)]
for i in range(N):
    if T[i] == "L":
        T_power[i][0] += 1
    elif T[i] == "R":
        T_power[i][1] += 1
    elif T[i] == "U":
        T_power[i][2] += 1
    else:
        T_power[i][3] += 1

for j in range(4):
    for i in range(N-1):
        S_power[i+1][j] += S_power[i][j]
for j in range(4):
    for i in range(N-1):
        T_power[i+1][j] += T_power[i][j]
T_power.append([0, 0, 0, 0])



flag = 1
power_diff = []
for i in range(N):
    power_diff.append(S_power[i][1]-T_power[i-1][0])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "L":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "R" and power_diff_max[i] + sr + 1 <= W:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][0]-T_power[i-1][1])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "R":
        sr += 1
    if sr > W:
        flag = 0
        break
    if T[i] == "L" and power_diff_max[i] + 1 <= sr:
        sr -= 1







power_diff = []
for i in range(N):
    power_diff.append(S_power[i][2]-T_power[i-1][3])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "D" and power_diff_max[i] + sr + 1 <= H:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][3]-T_power[i-1][2])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "D":
        sr += 1
    if sr > H:
        flag = 0
        break
    if T[i] == "U" and power_diff_max[i] + 1 <= sr:
        sr -= 1


if flag == 1:
    print("YES")
else:
    print("NO")