Problem p02803 (python)

Fixed submission:
from collections import deque

def bfs(grid, start, wall, h, w):
    visited = [[False for _ in range(w+2)] for _ in range(h+2)]
    visited[start[0]][start[1]] = True

    queue = deque([])
    queue.append([start[0], start[1], 0])
    dxy = [(1,0), (-1,0), (0,1), (0,-1)]
    cost = 0
    while queue:
        y,x,cnt = queue.popleft()
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall or visited[y_][x_]:
                continue
            queue.append([y_,x_,cnt+1])
            if cnt+1 > cost:
                cost = cnt+1
            visited[y_][x_] = True
    return cost

h,w = map(int, input().split())
wall = '#'
edges = [wall] * (w+2)
grid = [edges]
for i in range(h):
    s = input()
    grid.append([wall] + list(s) + [wall])
grid.append(edges)


cand = {2:[], 3:[], 4:[]}
dxy = [(1,0), (-1,0), (0,1), (0,-1)]
for y in range(1,h+1):
    for x in range(1,w+1):
        if grid[y][x] == wall:
            continue
        wl = 0
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall:
                wl += 1
        if wl > 1:
            cand[wl].append((y,x))

ans = 0
starts = []
for i in [4,3,2]:
    if cand[i] != []:
        starts = cand[i]
        break
for start in starts:
    cnt = bfs(grid, start, wall, h, w)
    if cnt > ans:
        ans = cnt
print(ans)

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

def bfs(grid, sy, sx, wall):
    queue = deque([])
    queue.append([sy, sx, 0])
    visited = {}
    visited[(sy,sx)] = 0
    dxy = [(1,0), (-1,0), (0,1), (0,-1)]
    while queue:
        y,x,cnt = queue.popleft()
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall or (y_,x_) in visited:
                continue
            queue.append([y_,x_,cnt+1])
            visited[(y_,x_)] = cnt+1
    return visited

h,w = map(int, input().split())
wall = '#'
edges = [wall] * (w+2)
grid = [edges]
start = []
for i in range(h):
    s = input()
    grid.append([wall] + list(s) + [wall])
    for j in range(1,w+1):
        if start == [] and grid[i][j] == '.':
            start = [i,j]
grid.append(edges)

visited = bfs(grid, start[0], start[1], wall)
mx = 0
pos = []
for k,v in visited.items():
    if v > mx:
        mx = v
        pos = k
# print(start, pos)
visited = bfs(grid, pos[0], pos[1], wall)
print(max(visited.values()))

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

def bfs(grid, start, wall, h, w):
    visited = [[False for _ in range(w+2)] for _ in range(h+2)]
    visited[start[0]][start[1]] = True
    queue = deque([])
    queue.append([start[0], start[1], 0])
    dxy = [(1,0), (-1,0), (0,1), (0,-1)]
    cost = 0
    while queue:
        y,x,cnt = queue.popleft()
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall or visited[y_][x_]:
                continue
            queue.append([y_,x_,cnt+1])
            if cnt+1 > cost:
                cost = cnt + 1
            visited[y_][x_] = True
    return cost

h,w = map(int, input().split())
wall = '#'
edges = [wall] * (w+2)
grid = [edges]
for i in range(h):
    s = input()
    grid.append([wall] + list(s) + [wall])
grid.append(edges)


cand = {2:[], 3:[]}
dxy = [(1,0), (-1,0), (0,1), (0,-1)]
for y in range(1,h+1):
    for x in range(1,w+1):
        wl = 0
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            if grid[y_][x_] == wall:
                wl += 1
        if wl > 1:
            cand[wl].append((y,x))

ans = 0
starts = cand[3] if cand[3] != [] else cand[2]
for start in starts:
    cnt = bfs(grid, start, wall, h, w)
    if cnt > ans:
        ans = cnt
print(ans)