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


H, W = map(int, input().split())

has_visited = [[True]*(W+2)]
has_visited += [[True] + [m == "#" for m in input().rstrip()] + [True] for _ in range(H)]
has_visited += [[True]*(W+2)]

search_que = deque()
for y in range(1, W+1):
    for x in range(1, H+1):
        if has_visited[y][x]:
            search_que.append((y, x))

count = -1
while search_que:
    count += 1
    next_que = deque()
    for y, x in search_que:
        if not has_visited[y+1][x]:
            has_visited[y+1][x] = True
            next_que.append((y+1, x))
        if not has_visited[y-1][x]:
            has_visited[y-1][x] = True
            next_que.append((y-1, x))
        if not has_visited[y][x+1]:
            has_visited[y][x+1] = True
            next_que.append((y, x+1))
        if not has_visited[y][x-1]:
            has_visited[y][x-1] = True
            next_que.append((y, x-1))
    search_que = next_que

print(count)

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

has_visited = [[True]*(W+2)]
has_visited += [[True] + [m == "#" for m in input().rstrip()] + [True] for _ in range(H)]
has_visited += [[True]*(W+2)]

search_que = []
for y in range(1, H+1):
    for x in range(1, W+1):
        if has_visited[y][x]:
            search_que.append((y, x))

count = -1
while search_que:
    count += 1
    next_que = []
    for y, x in search_que:
        if not has_visited[y+1][x]:
            has_visited[y+1][x] = True
            next_que.append((y+1, x))
        if not has_visited[y-1][x]:
            has_visited[y-1][x] = True
            next_que.append((y-1, x))
        if not has_visited[y][x+1]:
            has_visited[y][x+1] = True
            next_que.append((y, x+1))
        if not has_visited[y][x-1]:
            has_visited[y][x-1] = True
            next_que.append((y, x-1))
    search_que = next_que

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


dir_y = [1, 0, -1, 0]
dir_x = [0, 1, 0, -1]
def paint():
    for _ in range(len(que)):
        y, x = que.popleft()
        for i in range(4):
            next_y = y + dir_y[i]
            next_x = x + dir_x[i]
            if 0 <= next_y < H and 0 <= next_x < W and not has_visited[next_y][next_x]:
                has_visited[next_y][next_x] = True
                que.append((next_y, next_x))


H, W = map(int, input().split())
canvas = []
has_visited = [[False]*W for _ in range(H)]
que = deque()
for y in range(H):
    canvas.append(input())
    for x in range(W):
        if canvas[y][x] == "#":
            que.append((y, x))
            has_visited[y][x] = True

count = 0
while len(que) > 0:
    paint()
    count += 1

print(count-1)


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

has_visited = [[True]*(W+2)]
has_visited += [[True] + [m == "#" for m in input().rstrip()] + [True] for _ in range(H)]
has_visited += [[True]*(W+2)]

search_que = []
for y in range(1, H+1):
    for x in range(1, W+1):
        if has_visited[y][x]:
            search_que.append((y, x))

count = -1
while search_que:
    count += 1
    next_que = []
    for y, x in search_que:
        if not has_visited[y+1][x]:
            has_visited[y+1][x] = True
            next_que.append((y+1, x))
        if not has_visited[y-1][x]:
            has_visited[y-1][x] = True
            next_que.append((y-1, x))
        if not has_visited[y][x+1]:
            has_visited[y][x+1] = True
            next_que.append((y, x+1))
        if not has_visited[y][x-1]:
            has_visited[y][x-1] = True
            next_que.append((y, x-1))
    search_que = next_que

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


dir_y = [1, 0, -1, 0]
dir_x = [0, 1, 0, -1]
def paint():
    for _ in range(len(que)):
        y, x = que.popleft()
        for i in range(4):
            next_y = y + dir_y[i]
            next_x = x + dir_x[i]
            if 0 <= next_y < H and 0 <= next_x < W and not has_visited[next_y][next_x]:
                has_visited[next_y][next_x] = True
                que.append((next_y, next_x))


H, W = map(int, input().split())
canvas = []
has_visited = [[False]*W for _ in range(H)]
que = deque()
for y in range(H):
    canvas.append(list(input()))
    for x in range(W):
        if canvas[y][x] == "#":
            que.append((y, x))
            has_visited[y][x] = True

count = 0
while len(que) > 0:
    paint()
    count += 1

print(count-1)


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

has_visited = [[True]*(W+2)]
has_visited += [[True] + [m == "#" for m in input().rstrip()] + [True] for _ in range(H)]
has_visited += [[True]*(W+2)]

search_que = []
for y in range(1, H+1):
    for x in range(1, W+1):
        if has_visited[y][x]:
            search_que.append((y, x))

count = -1
while search_que:
    count += 1
    next_que = []
    for y, x in search_que:
        if not has_visited[y+1][x]:
            has_visited[y+1][x] = True
            next_que.append((y+1, x))
        if not has_visited[y-1][x]:
            has_visited[y-1][x] = True
            next_que.append((y-1, x))
        if not has_visited[y][x+1]:
            has_visited[y][x+1] = True
            next_que.append((y, x+1))
        if not has_visited[y][x-1]:
            has_visited[y][x-1] = True
            next_que.append((y, x-1))
    search_que = next_que

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def find_black():
    for y in range(H):
        for x in range(W):
            if canvas[y][x] == "#" and first_visit[y][x]:
                que.append((y, x))
                first_visit[y][x] = False


dir_y = [1, 0, -1, 0]
dir_x = [0, 1, 0, -1]
def paint_canvas_black():
    while len(que) > 0:
        y, x = que.popleft()
        for i in range(4):
            next_y = y + dir_y[i]
            next_x = x + dir_x[i]
            if 0 <= next_y < H and 0 <= next_x < W and canvas[next_y][next_x] == ".":
                canvas[next_y][next_x] = "#"


def has_painted():
    return canvas == BLACK_CANVAS


H, W = map(int, input().split())
sy, sx = -1, -1
canvas = []
BLACK_CANVAS = [["#"]*W for _ in range(H)]
first_visit = [[True]*W for _ in range(H)]
for y in range(H):
    canvas.append(list(input()))
    if sy == sx == -1:
        for x in range(W):
            if canvas[y][x] == "#":
                sy, sx = y, x
                break


que = deque()
count = 0
while not has_painted():
    find_black()
    paint_canvas_black()
    count += 1

print(count)

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

has_visited = [[True]*(W+2)]
has_visited += [[True] + [m == "#" for m in input().rstrip()] + [True] for _ in range(H)]
has_visited += [[True]*(W+2)]

search_que = []
for y in range(1, H+1):
    for x in range(1, W+1):
        if has_visited[y][x]:
            search_que.append((y, x))

count = -1
while search_que:
    count += 1
    next_que = []
    for y, x in search_que:
        if not has_visited[y+1][x]:
            has_visited[y+1][x] = True
            next_que.append((y+1, x))
        if not has_visited[y-1][x]:
            has_visited[y-1][x] = True
            next_que.append((y-1, x))
        if not has_visited[y][x+1]:
            has_visited[y][x+1] = True
            next_que.append((y, x+1))
        if not has_visited[y][x-1]:
            has_visited[y][x-1] = True
            next_que.append((y, x-1))
    search_que = next_que

print(count)