Problem p03436 (python)

Fixed submission:
H,W=map(int,input().split())

A=[]
a=["#"]*(W+2)
A.append(a)
for i in range(H):
    s=input()
    s="#"+s+"#"
    A.append(list(s))
A.append(a)
#幅優先探索
#print(A)
P=[[1,1]]
A[1][1]=1
x=[1,-1,0,0]
y=[0,0,1,-1]
while P!=[]:
    #print(P)
    for j in range(4):
        if A[P[0][0]+x[j]][P[0][1]+y[j]]=="#":
            pass
        elif A[P[0][0]+x[j]][P[0][1]+y[j]]==".":
            A[P[0][0]+x[j]][P[0][1]+y[j]]=A[P[0][0]][P[0][1]]+1
            P.append([P[0][0]+x[j],P[0][1]+y[j]])
        elif A[P[0][0]+x[j]][P[0][1]+y[j]]<=A[P[0][0]][P[0][1]]+1:
            pass
        else:
            A[P[0][0]+x[j]][P[0][1]+y[j]]=A[P[0][0]][P[0][1]]+1
            P.append([P[0][0]+x[j],P[0][1]+y[j]])
    del P[0]
S=0
for i in range(H+2):
    for j in range(W+2):
        if A[i][j]=="#":
            S+=1
if A[H][W]==".":
    print(-1)
else:
    print((H+2)*(W+2)-S-A[H][W])

Buggy submission (verdict: Runtime Error):
from collections import deque
H,W=map(int,input().split())
L=[]
l=["#"]*(W+2)
L.append(l)
cnt=0
for i in range(H):
    s=input()
    s="#"+s+"#"
    l=[]
    for j in range(W+2):
        l.append(s[j])
        if s[j]==".":
            cnt+=1
    L.append(l)
l=["#"]*(W+2)
L.append(l)
#print(L)
D=deque([[1,1,1]])
for x in range(1000000):
    if D==[]:
        print("-1")
    elif H==D[0][0] and W==D[0][1]:
        print(cnt-D[0][2])
        exit()
    else:
        if L[D[0][0]+1][D[0][1]]==".":
            D.append([D[0][0]+1,D[0][1],D[0][2]+1])
        if L[D[0][0]][D[0][1]+1]==".":
            D.append([D[0][0],D[0][1]+1,D[0][2]+1])
        if L[D[0][0]-1][D[0][1]]==".":
            D.append([D[0][0]-1,D[0][1],D[0][2]+1])
        if L[D[0][0]][D[0][1]-1]==".":
            D.append([D[0][0],D[0][1]-1,D[0][2]+1])
        L[D[0][0]][D[0][1]]="#"
        D.popleft()

Buggy submission (verdict: Runtime Error):
from collections import deque
H,W=map(int,input().split())
L=[]
l=["#"]*(W+2)
L.append(l)
cnt=0
for i in range(H):
    s=input()
    s="#"+s+"#"
    l=[]
    for j in range(W+2):
        l.append(s[j])
        if s[j]==".":
            cnt+=1
    L.append(l)
l=["#"]*(W+2)
L.append(l)
#print(L)
D=deque([[1,1,1]])
for x in range(1000000):
    if H==D[0][0] and W==D[0][1]:
        print(cnt-D[0][2])
        exit()
    else:
        if L[D[0][0]+1][D[0][1]]==".":
            D.append([D[0][0]+1,D[0][1],D[0][2]+1])
        if L[D[0][0]][D[0][1]+1]==".":
            D.append([D[0][0],D[0][1]+1,D[0][2]+1])
        if L[D[0][0]-1][D[0][1]]==".":
            D.append([D[0][0]-1,D[0][1],D[0][2]+1])
        if L[D[0][0]][D[0][1]-1]==".":
            D.append([D[0][0],D[0][1]-1,D[0][2]+1])
        L[D[0][0]][D[0][1]]="#"
        D.popleft()

Buggy submission (verdict: Runtime Error):
from collections import deque
H,W=map(int,input().split())
L=[]
cnt=0
for i in range(H+2):
    if i==0 or i==H+1:
        S="#"*(W+2)
    else:
        S=input()
        S="#"+S+"#"
    L1=[]
    for j in range(W+2):
        if S[j]==".":
            cnt+=1
        L1.append(S[j])
    L.append(L1)
L[1][1]=0
D=deque()
D.append([1,1])
h=[0,1,0,-1]
w=[1,0,-1,0]
i=0
for i in range(10000):
    #print(D)
    #print(L)
    if D==[]:
        print(-1)
    elif D[0][0]==H and D[0][1]==W:
        print(cnt-L[H][W]-1)
        exit()
    for k in range(3):
        if L[D[0][0]+h[k]][D[0][1]+w[k]]==".":
            if [D[0][0]+h[k],D[0][1]+w[k]] not in D:
                D.append([D[0][0]+h[k],D[0][1]+w[k]])
            L[D[0][0]+h[k]][D[0][1]+w[k]]=L[D[0][0]][D[0][1]]+1
            
        elif L[D[0][0]+h[k]][D[0][1]+w[k]]=="#":
            pass
        else:
            if L[D[0][0]+h[k]][D[0][1]+w[k]]>L[D[0][0]][D[0][1]]+1:
                L[D[0][0]+h[k]][D[0][1]+w[k]]=L[D[0][0]][D[0][1]]+1
    D.popleft()

Buggy submission (verdict: Runtime Error):
from collections import deque
H,W=map(int,input().split())
L=[]
cnt=0
for i in range(H+2):
    if i==0 or i==H+1:
        S="#"*(W+2)
    else:
        S=input()
        S="#"+S+"#"
    L1=[]
    for j in range(W+2):
        if S[j]==".":
            cnt+=1
        L1.append(S[j])
    L.append(L1)
L[1][1]=0
D=deque()
D.append([1,1])
h=[0,1,0,-1]
w=[1,0,-1,0]
i=0
for i in range(10000):
    #print(D)
    #print(L)
    if D[0][0]==H and D[0][1]==W:
        print(cnt-L[H][W]-1)
        exit()
    for k in range(3):
        if L[D[0][0]+h[k]][D[0][1]+w[k]]==".":
            if [D[0][0]+h[k],D[0][1]+w[k]] not in D:
                D.append([D[0][0]+h[k],D[0][1]+w[k]])
            L[D[0][0]+h[k]][D[0][1]+w[k]]=L[D[0][0]][D[0][1]]+1
            
        elif L[D[0][0]+h[k]][D[0][1]+w[k]]=="#":
            pass
        else:
            if L[D[0][0]+h[k]][D[0][1]+w[k]]>L[D[0][0]][D[0][1]]+1:
                L[D[0][0]+h[k]][D[0][1]+w[k]]=L[D[0][0]][D[0][1]]+1
    D.popleft()