Problem p03436 (python)

Fixed submission:
from collections import deque

H,W=map(int,input().split())
H-=1
W-=1
maze=[]
for i in range(H+1):
   S=input()
   S=[c for c in S]
   maze.append(S)

maze[0][0]="S"

count=0
for i in range(H+1):
   for j in range(W+1):
      if maze[i][j]=="#":
         count+=1

que=deque([[0,0]])
steps=[[-1]*(W+1) for i in range(H+1)]
steps[0][0]=0
flag=True

def bfs():
      global flag
      if len(que)>=1:
         now=que.popleft()
         now_step=steps[now[0]][now[1]]
         for dx,dy in (0,-1),(1,0),(0,1),(-1,0):
               ny=now[0]+dy
               nx=now[1]+dx
               if 0<=ny<=H and 0<=nx<=W:
                     if ny==H and nx==W:
                           print((H+1)*(W+1)-(now_step+1+1)-(count))
                           flag=False
                           break
                     elif maze[ny][nx]==".":
                           maze[ny][nx]="A"
                           que.append([ny,nx])
                           steps[ny][nx]=now_step+1
                     else:
                           pass
      else:
         print(-1)
         flag=False

while(flag):
      bfs()

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

H,W=map(int,input().split())
H-=1
W-=1
maze=[]
for i in range(H+1):
   S=input()
   S=[c for c in S]
   maze.append(S)

maze[0][0]="S"

count=0
for i in range(H+1):
   for j in range(W+1):
      if maze[i][j]=="#":
         count+=1

que=deque([[0,0]])
steps=[[-1]*(W+1) for i in range(H+1)]
steps[0][0]=0
flag=True

def bfs():
      global flag
      now=que.popleft()
      now_step=steps[now[0]][now[1]]
      for dx,dy in (0,-1),(1,0),(0,1),(-1,0):
            ny=now[0]+dy
            nx=now[1]+dx
            if 0<=ny<=H and 0<=nx<=W:
                  if ny==H and nx==W:
                        print((H+1)*(W+1)-(now_step+1+1)-(count))
                        flag=False
                        break
                  elif maze[ny][nx]==".":
                        maze[ny][nx]="A"
                        que.append([ny,nx])
                        steps[ny][nx]=now_step+1
                  else:
                        pass

while(flag):
      bfs()

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

H,W=map(int,input().split())
H-=1
W-=1
maze=[]
for i in range(H+1):
   S=input()
   S=[c for c in S]
   maze.append(S)

maze[0][0]="S"

count=0
for i in range(H+1):
   for j in range(W+1):
      if maze[i][j]=="#":
         count+=1

que=deque([[0,0]])
steps=[[-1]*(W+1) for i in range(H+1)]
steps[0][0]=0
flag=True

def bfs():
      global flag
      print(que)
      now=que.popleft()
      now_step=steps[now[0]][now[1]]
      for dx,dy in (0,-1),(1,0),(0,1),(-1,0):
            ny=now[0]+dy
            nx=now[1]+dx
            if 0<=ny<=H and 0<=nx<=W:
                  if ny==H and nx==W:
                        print((H+1)*(W+1)-(now_step+1+1)-(count))
                        flag=False
                        break
                  elif maze[ny][nx]==".":
                        maze[ny][nx]="A"
                        que.append([ny,nx])
                        steps[ny][nx]=now_step+1
                  else:
                        pass

while(flag):
      bfs()