Problem p03436 (python)

Fixed submission:
from collections import deque

H, W = map(int, input().split())
score = H * W
S = [[] for i in range(H)]
for i in range(H):
    for s in input():
        if s == "#":
            score -= 1
        S[i].append(s)


ls = deque([[0, 0]])
dir = [[1,0], [-1,0], [0,1], [0,-1]]
dp = [[float("inf") for i in range(W)] for j in range(H)]
dp[0][0] = 0
while len(ls) > 0:
    x, y = ls.popleft()

    if x == H-1 and y == W-1:
        print(score-dp[H-1][W-1]-1)
        break

    for dx, dy in dir:
        if (x+dx < 0) or (x+dx > H-1) or (y+dy < 0) or (y+dy > W-1):
            continue
        else:
            if S[x+dx][y+dy] == "." and dp[x][y]+1 < dp[x+dx][y+dy]:
                dp[x+dx][y+dy] = dp[x][y] + 1
                ls.append([x+dx, y+dy])

else:
    print(-1)

Buggy submission (verdict: Runtime Error):
from collections import deque

H, W = map(int, input().split())

G = []
black = 0
for i in range(H):
    line = list(input())
    G.append(line)
    black += line.count("#")
    
ls = deque([[0, 0, 0]])
D = [[float("inf")] * W for i in range(H)]
direction = [[1,0], [-1,0], [0,1], [0,-1]]
while len(ls) > 0:
    h, w, depth = ls.popleft()
    
    if h == H-1 and w == W-1:
        route = depth + 1
        break
        
    for dh, dw in direction:
        if h + dh < 0 or H-1 < h + dh: continue
        if w + dw < 0 or W-1 < w + dw: continue
        if depth + 1 < D[h+dh][w+dw] and G[h+dh][w+dw] != "#":
            ls.append([h+dh, w+dw, depth+1])
            D[h+dh][w+dw] = depth + 1
            
ans = (H * W) - black - route
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque

H, W = map(int, input().split())
score = H * W
S = [[] for i in range(W)]
for i in range(H):
    for s in input():
        if s == "#":
            score -= 1
        S[i].append(s)


ls = deque([[0, 0, 0]])
dir = [[1,0], [-1,0], [0,1], [0,-1]]
while len(ls) > 0:
    x, y, depth = ls.popleft()

    if x == H-1 and y == W-1:
        print(score-depth-1)
        break
    
    S[x][y] = "#"

    for dx, dy in dir:
        if (x+dx < 0) or (x+dx > H-1) or (y+dy < 0) or (y+dy > W-1):
            continue
        else:
            if S[x+dx][y+dy] == ".":
                ls.append([x+dx, y+dy, depth+1])

else:
    print(-1)

Buggy submission (verdict: Runtime Error):
from collections import deque

H, W = map(int, input().split())
score = H * W
S = [[] for i in range(W)]
for i in range(H):
    for s in input():
        if s == "#":
            score -= 1
        S[i].append(s)


ls = deque([[0, 0, 0]])
while len(ls) > 0:
    x, y, depth = ls.popleft()

    if x == H-1 and y == W-1:
        print(score-depth-1)
        break
    
    S[x][y] = "#"

    if 0 <= x+1 <= H-1 and 0 <= y <= W-1:
        if S[x+1][y] == ".":
            ls.append([x+1, y, depth+1])
    if 0 <= x <= H-1 and 0 <= y+1 <= W-1:
        if S[x][y+1] == ".":
            ls.append([x, y+1, depth+1])
    if 0 <= x <= H-1 and 0 <= y-1 <= W-1:
        if S[x][y-1] == ".":
            ls.append([x, y-1, depth+1])
    if 0 <= x-1 <= H-1 and 0 <= y <= W-1:
        if S[x-1][y] == ".":
            ls.append([x-1, y, depth+1])

else:
    print(-1)

Buggy submission (verdict: Runtime Error):
from collections import deque

H, W = map(int, input().split())
score = H * W
S = [[] for i in range(W)]
for i in range(H):
    for s in input():
        if s == "#":
            score -= 1
        S[i].append(s)


ls = deque([[0, 0, 0]])
while len(ls) > 0:
    x, y, depth = ls.popleft()

    if x == H-1 and y == W-1:
        print(score-depth-1)
        break
    
    S[x][y] = "#"

    if 0 <= x+1 <= H-1 and 0 <= y <= W-1:
        if S[x+1][y] == ".":
            ls.append([x+1, y, depth+1])
    if 0 <= x <= H-1 and 0 <= y+1 <= W-1:
        if S[x][y+1] == ".":
            ls.append([x, y+1, depth+1])
    if 0 <= x <= H-1 and 0 <= y-1 <= W-1:
        if S[x][y-1] == ".":
            ls.append([x, y-1, depth+1])
    if 0 <= x-1 <= H-1 and 0 <= y <= W-1:
        if S[x-1][y] == ".":
            ls.append([x-1, y, depth+1])

else:
    print(-1)