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


H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

checked = [[False for _ in range(W)] for _ in range(H)]

que = deque()
flag = False
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            que.append((i, j))
            checked[i][j] = True
            flag = True

if flag is False:
    print(0)
    exit()

n_que = deque()
step = 0
while len(que):

    h, w = que[0][0], que[0][1]
    que.popleft()
    for d in range(4):
        nh, nw = h + dy[d], w + dx[d]
        if nh < 0 or nw < 0 or nh >= H or nw >= W:
            continue
        if checked[nh][nw] is True:
            continue
        n_que.append((nh, nw))
        checked[nh][nw] = True

    if len(que) == 0:
        if len(n_que) == 0:
            print(step)
            exit()
        que = n_que
        n_que = deque()
        step += 1


Fixed submission:
from collections import deque


H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

checked = [[False for _ in range(W)] for _ in range(H)]

que = deque()
flag = False
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            que.append((i, j))
            checked[i][j] = True
        else:
            flag = True

if flag is False:
    print('0')
    exit()

n_que = deque()
step = 0
while len(que):

    h, w = que[0][0], que[0][1]
    que.popleft()
    for d in range(4):
        nh, nw = h + dy[d], w + dx[d]
        if nh < 0 or nw < 0 or nh >= H or nw >= W:
            continue
        if checked[nh][nw] is True:
            continue
        n_que.append((nh, nw))
        checked[nh][nw] = True

    if len(que) == 0:
        if len(n_que) == 0:
            print(step)
            exit()
        que = n_que
        n_que = deque()
        step += 1

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


H, W = map(int, input().split())
past_map = [list(input()) for _ in range(H)]


def make_new_map(p_map):

    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]

    update_flag = False
    n_map = copy.deepcopy(p_map)
    reached = [[False for _ in range(W)] for _ in range(H)]

    for sh in range(H):
        for sw in range(W):
            if (reached[sh][sw] is False) and (p_map[sh][sw] == '#'):
                # start bfs
                togo = deque()
                togo.append((sh, sw))
                reached[sh][sw] = True
                while len(togo):
                    h, w = togo[0][0], togo[0][1]
                    togo.popleft()
                    for d in range(4):
                        nh, nw = h + dy[d], w + dx[d]
                        if nh < 0 or nw < 0 or nh >= H or nw >= W:
                            continue
                        if reached[nh][nw] is True:
                            continue
                        if n_map[nh][nw] == '.':
                            n_map[nh][nw] = '#'
                            update_flag = True
                        if p_map[nh][nw] == '.':
                            continue
                        togo.append((nh, nw))
                        reached[nh][nw] = True
    return n_map, update_flag


count = 0
past_map, flag = make_new_map(past_map)
while flag:
    count += 1
    past_map, flag = make_new_map(past_map)
print(count)


Fixed submission:
from collections import deque


H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

checked = [[False for _ in range(W)] for _ in range(H)]

que = deque()
flag = False
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            que.append((i, j))
            checked[i][j] = True
        else:
            flag = True

if flag is False:
    print('0')
    exit()

n_que = deque()
step = 0
while len(que):

    h, w = que[0][0], que[0][1]
    que.popleft()
    for d in range(4):
        nh, nw = h + dy[d], w + dx[d]
        if nh < 0 or nw < 0 or nh >= H or nw >= W:
            continue
        if checked[nh][nw] is True:
            continue
        n_que.append((nh, nw))
        checked[nh][nw] = True

    if len(que) == 0:
        if len(n_que) == 0:
            print(step)
            exit()
        que = n_que
        n_que = deque()
        step += 1

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


H, W = map(int, input().split())
past_map = [list(input()) for _ in range(H)]


def make_new_map(p_map):

    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]

    n_map = copy.deepcopy(p_map)
    reached = [[False for _ in range(W)] for _ in range(H)]

    for sh in range(H):
        for sw in range(W):
            if (reached[sh][sw] is False) and (p_map[sh][sw] == '#'):
                # start bfs
                togo = deque()
                togo.append((sh, sw))
                reached[sh][sw] = True
                while len(togo):
                    h, w = togo[0][0], togo[0][1]
                    togo.popleft()
                    for d in range(4):
                        nh, nw = h + dy[d], w + dx[d]
                        if nh < 0 or nw < 0 or nh >= H or nw >= W:
                            continue
                        if n_map[nh][nw] == '.':
                            n_map[nh][nw] = '#'
                        if reached[nh][nw] is True:
                            continue
                        if p_map[nh][nw] == '.':
                            continue
                        togo.append((nh, nw))
                        reached[nh][nw] = True
    return n_map


count = 0
next_map = make_new_map(past_map)
while past_map != next_map:
    count += 1
    past_map = copy.deepcopy(next_map)
    next_map = make_new_map(past_map)
print(count)


Fixed submission:
from collections import deque


H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

checked = [[False for _ in range(W)] for _ in range(H)]

que = deque()
flag = False
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            que.append((i, j))
            checked[i][j] = True
        else:
            flag = True

if flag is False:
    print('0')
    exit()

n_que = deque()
step = 0
while len(que):

    h, w = que[0][0], que[0][1]
    que.popleft()
    for d in range(4):
        nh, nw = h + dy[d], w + dx[d]
        if nh < 0 or nw < 0 or nh >= H or nw >= W:
            continue
        if checked[nh][nw] is True:
            continue
        n_que.append((nh, nw))
        checked[nh][nw] = True

    if len(que) == 0:
        if len(n_que) == 0:
            print(step)
            exit()
        que = n_que
        n_que = deque()
        step += 1

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


H, W = map(int, input().split())
past_map = [list(input()) for _ in range(H)]


def make_new_map(p_map):

    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]

    n_map = copy.deepcopy(p_map)
    reached = [[False for _ in range(W)] for _ in range(H)]

    for sh in range(H):
        for sw in range(W):
            if (reached[sh][sw] is False) and (p_map[sh][sw] == '#'):
                # start bfs
                togo = deque()
                togo.append((sh, sw))
                reached[sh][sw] = True
                while len(togo):
                    h, w = togo[0][0], togo[0][1]
                    togo.popleft()
                    for d in range(4):
                        nh, nw = h + dy[d], w + dx[d]
                        if nh < 0 or nw < 0 or nh >= H or nw >= W:
                            continue
                        if n_map[nh][nw] == '.':
                            n_map[nh][nw] = '#'
                        if reached[nh][nw] is True:
                            continue
                        if p_map[nh][nw] == '.':
                            continue
                        togo.append((nh, nw))
                        reached[nh][nw] = True
    return n_map


count = 0
next_map = make_new_map(past_map)
while past_map != next_map:
    count += 1
    past_map = copy.deepcopy(next_map)
    next_map = make_new_map(past_map)
print(count)


Fixed submission:
from collections import deque


H, W = map(int, input().split())
maze = [list(input()) for _ in range(H)]

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

checked = [[False for _ in range(W)] for _ in range(H)]

que = deque()
flag = False
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            que.append((i, j))
            checked[i][j] = True
        else:
            flag = True

if flag is False:
    print('0')
    exit()

n_que = deque()
step = 0
while len(que):

    h, w = que[0][0], que[0][1]
    que.popleft()
    for d in range(4):
        nh, nw = h + dy[d], w + dx[d]
        if nh < 0 or nw < 0 or nh >= H or nw >= W:
            continue
        if checked[nh][nw] is True:
            continue
        n_que.append((nh, nw))
        checked[nh][nw] = True

    if len(que) == 0:
        if len(n_que) == 0:
            print(step)
            exit()
        que = n_que
        n_que = deque()
        step += 1