Problem p03436 (python)

Fixed submission:
import sys

H, W = [int(_) for _ in input().split()]
s = ["" for _ in range(H)]
for i in range(H):
    s[i] = input()
A = [[0, 0]]
d = [[] for _ in range(H)]
for i in range(0, H):
    for j in range(0, W):
        d[i].append(float('inf'))
d[0][0] = 0
while [H-1,W-1] not in A:
    
    if len(A) == 0:
        print(-1)
        sys.exit()


    res = float('inf')
    t_i = 0
    t_j = 0
    for i in range(len(A)):
        if d[A[i][0]][A[i][1]] < res:
            res = d[A[i][0]][A[i][1]]
            t_i = A[i][0]
            t_j = A[i][1]
    


    A.remove([t_i,t_j])

    if t_i - 1 >= 0 and s[t_i-1][t_j] == '.':
        if d[t_i-1][t_j] == float('inf'):
            A.append([t_i-1,t_j])
            d[t_i-1][t_j] = res + 1
        elif d[t_i-1][t_j] > res + 1:
            d[t_i-1][t_j] = res + 1



    if t_i + 1 <= H-1 and s[t_i+1][t_j] == '.':
        if d[t_i+1][t_j] == float('inf'):
            A.append([t_i+1,t_j])
            d[t_i+1][t_j] = res + 1
        elif d[t_i+1][t_j] > res + 1:
            d[t_i+1][t_j] = res + 1

   

    if t_j - 1 >= 0 and s[t_i][t_j-1] == '.':
        if d[t_i][t_j-1] == float('inf'):
            A.append([t_i,t_j-1])
            d[t_i][t_j-1] = res + 1
        elif d[t_i][t_j-1] > res + 1:
            d[t_i][t_j-1] = res + 1



    if t_j + 1 <= W-1 and s[t_i][t_j+1] == '.':
        if d[t_i][t_j+1] == float('inf'):
            A.append([t_i,t_j+1])
            d[t_i][t_j+1] = res + 1
        elif d[t_i][t_j+1] > res + 1:
            d[t_i][t_j+1] = res + 1

ans = d[H-1][W-1]

c = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            c += 1

print(c-ans-1)

Buggy submission (verdict: Runtime Error):
H, W = [int(_) for _ in input().split()]
s = ["" for _ in range(H)]
for i in range(H):
    s[i] = input()
A = [[0, 0]]
d = [[] for _ in range(H)]
for i in range(0, H):
    for j in range(0, W):
        d[i].append(float('inf'))
d[0][0] = 0
while [H-1,W-1] not in A:


    res = float('inf')
    t_i = 0
    t_j = 0
    for i in range(len(A)):
        if d[A[i][0]][A[i][1]] < res:
            res = d[A[i][0]][A[i][1]]
            t_i = A[i][0]
            t_j = A[i][1]
    

    A.remove([t_i,t_j])

    if t_i - 1 >= 0 and s[t_i-1][t_j] == '.':
        if d[t_i-1][t_j] == float('inf'):
            A.append([t_i-1,t_j])
            d[t_i-1][t_j] = res + 1
        elif d[t_i-1][t_j] > res + 1:
            d[t_i-1][t_j] = res + 1



    if t_i + 1 <= H-1 and s[t_i+1][t_j] == '.':
        if d[t_i+1][t_j] == float('inf'):
            A.append([t_i+1,t_j])
            d[t_i+1][t_j] = res + 1
        elif d[t_i+1][t_j] > res + 1:
            d[t_i+1][t_j] = res + 1

   

    if t_j - 1 >= 0 and s[t_i][t_j-1] == '.':
        if d[t_i][t_j-1] == float('inf'):
            A.append([t_i,t_j-1])
            d[t_i][t_j-1] = res + 1
        elif d[t_i][t_j-1] > res + 1:
            d[t_i][t_j-1] = res + 1



    if t_j + 1 <= W-1 and s[t_i][t_j+1] == '.':
        if d[t_i][t_j+1] == float('inf'):
            A.append([t_i,t_j+1])
            d[t_i][t_j+1] = res + 1
        elif d[t_i][t_j+1] > res + 1:
            d[t_i][t_j+1] = res + 1

ans = d[H-1][W-1]

c = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            c += 1

print(c-ans-1)