Problem p03436 (python)

Fixed submission:
from collections import deque
def DFS(graph,start,end):
    #  深さ優先探索ではスタックを用いたが幅優先探索ではキューを用いる
    c,r = len(graph[0]),len(graph)
    que = deque([])
    que.append(start)
    visited = [[0 for _ in range(c)] for _ in range(r)]

    while visited[end[0]][end[1]]==0:
        if len(que)== 0:
            break
        now = que.popleft()
        y,x = now
        up,down,left,right = [y-1,x],[y+1,x],[y,x-1],[y,x+1]

        #up
        if visited[up[0]][up[1]]==0:
            if graph[up[0]][up[1]]==".":
                que.append(up)
                visited[up[0]][up[1]]= visited[y][x]+1
            
        #down
        if visited[down[0]][down[1]]==0:
            if graph[down[0]][down[1]]==".":
                que.append(down)
                visited[down[0]][down[1]]= visited[y][x]+1
        #left
        if visited[left[0]][left[1]]==0:
            if graph[left[0]][left[1]]==".":
                que.append(left)
                visited[left[0]][left[1]]= visited[y][x]+1
        #right
        if visited[right[0]][right[1]]==0:
            if graph[right[0]][right[1]]==".":
                que.append(right)
                visited[right[0]][right[1]]= visited[y][x]+1
    
    return visited[end[0]][end[1]]

def main():
    h,w = map(int,input().split())
    ls = [list(input()) for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if ls[i][j]=="#":
                cnt += 1

    dummy = ["#" for _ in range(w+2)]
    for i in range(h):
        ls[i].append("#")
        ls[i].insert(0,"#")
    ls.insert(0,dummy)
    ls.append(dummy)

    step = DFS(ls,[1,1],[h,w])+1

    if step == 1:
        print(-1)
    else:
        print(h*w-step-cnt)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque
def DFS(graph,start,end):
    #  深さ優先探索ではスタックを用いたが幅優先探索ではキューを用いる
    c,r = len(graph[0]),len(graph)
    que = deque([])
    que.append(start)
    visited = [[0 for _ in range(c)] for _ in range(r)]

    while visited[end[0]][end[1]]==0:
        now = que.popleft()
        y,x = now
        up,down,left,right = [y-1,x],[y+1,x],[y,x-1],[y,x+1]

        #up
        if visited[up[0]][up[1]]==0:
            if graph[up[0]][up[1]]==".":
                que.append(up)
                visited[up[0]][up[1]]= visited[y][x]+1
            
        #down
        if visited[down[0]][down[1]]==0:
            if graph[down[0]][down[1]]==".":
                que.append(down)
                visited[down[0]][down[1]]= visited[y][x]+1
        #left
        if visited[left[0]][left[1]]==0:
            if graph[left[0]][left[1]]==".":
                que.append(left)
                visited[left[0]][left[1]]= visited[y][x]+1
        #right
        if visited[right[0]][right[1]]==0:
            if graph[right[0]][right[1]]==".":
                que.append(right)
                visited[right[0]][right[1]]= visited[y][x]+1
    
    return visited[end[0]][end[1]]

def main():
    h,w = map(int,input().split())
    ls = [list(input()) for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if ls[i][j]=="#":
                cnt += 1

    dummy = ["#" for _ in range(w+2)]
    for i in range(h):
        ls[i].append("#")
        ls[i].insert(0,"#")
    ls.insert(0,dummy)
    ls.append(dummy)

    step = DFS(ls,[1,1],[h,w])+1

    if step == 1:
        print(-1)
    else:
        print(h*w-step-cnt)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque
def DFS(graph,start,end):
    #  深さ優先探索ではスタックを用いたが幅優先探索ではキューを用いる
    c,r = len(graph[0]),len(graph)
    que = deque([])
    que.append(start)
    visited = [[0 for _ in range(c)] for _ in range(r)]

    while visited[end[0]][end[1]]==0:
        now = que.popleft()
        y,x = now
        up,down,left,right = [y-1,x],[y+1,x],[y,x-1],[y,x+1]

        #up
        if visited[up[0]][up[1]]==0:
            if graph[up[0]][up[1]]==".":
                que.append(up)
                visited[up[0]][up[1]]= visited[y][x]+1
            
        #down
        if visited[down[0]][down[1]]==0:
            if graph[down[0]][down[1]]==".":
                que.append(down)
                visited[down[0]][down[1]]= visited[y][x]+1
        #left
        if visited[left[0]][left[1]]==0:
            if graph[left[0]][left[1]]==".":
                que.append(left)
                visited[left[0]][left[1]]= visited[y][x]+1
        #right
        if visited[right[0]][right[1]]==0:
            if graph[right[0]][right[1]]==".":
                que.append(right)
                visited[right[0]][right[1]]= visited[y][x]+1
    
    return visited[end[0]][end[1]]

def main():
    h,w = map(int,input().split())
    ls = [list(input()) for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if ls[i][j]=="#":
                cnt += 1

    dummy = ["#" for _ in range(w+2)]
    for i in range(h):
        ls[i].append("#")
        ls[i].insert(0,"#")
    ls.insert(0,dummy)
    ls.append(dummy)

    step = DFS(ls,[1,1],[h,w])+1

    print(h*w-step-cnt)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque
def DFS(graph,start,end):
    #  深さ優先探索ではスタックを用いたが幅優先探索ではキューを用いる
    c,r = len(graph[0]),len(graph)
    que = deque([])
    que.append(start)
    visited = [[0 for _ in range(c)] for _ in range(r)]

    while visited[end[0]][end[1]]==0:
        now = que.popleft()
        y,x = now
        up,down,left,right = [y-1,x],[y+1,x],[y,x-1],[y,x+1]

        #up
        if visited[up[0]][up[1]]==0:
            if graph[up[0]][up[1]]==".":
                que.append(up)
                visited[up[0]][up[1]]= visited[y][x]+1
            
        #down
        if visited[down[0]][down[1]]==0:
            if graph[down[0]][down[1]]==".":
                que.append(down)
                visited[down[0]][down[1]]= visited[y][x]+1
        #left
        if visited[left[0]][left[1]]==0:
            if graph[left[0]][left[1]]==".":
                que.append(left)
                visited[left[0]][left[1]]= visited[y][x]+1
        #right
        if visited[right[0]][right[1]]==0:
            if graph[right[0]][right[1]]==".":
                que.append(right)
                visited[right[0]][right[1]]= visited[y][x]+1
    
    return visited[end[0]][end[1]]

def main():
    h,w = map(int,input().split())
    ls = [list(input()) for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if ls[i][j]=="#":
                cnt += 1

    dummy = ["#" for _ in range(w+2)]
    for i in range(h):
        ls[i].append("#")
        ls[i].insert(0,"#")
    ls.insert(0,dummy)
    ls.append(dummy)

    step = DFS(ls,[1,1],[h,w])+1

    print(h*w-step-cnt)


if __name__ == "__main__":
    main()