Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
cost_map = [[None]*W for i in range(H)]
starts = list()
for x in range(H):
    for y in range(W):
        if map[x][y]=="#":
            for nx, ny in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:
                if 0<=nx<H and 0<=ny<W:
                    if map[nx][ny]==".":
                        break
            else:
                continue
            starts.append([x, y])
for sx, sy in starts:
    queue = [[sx, sy]]
    cost_map[sx][sy] = 0
    while queue:
        x, y = queue.pop(0)
        current = cost_map[x][y]
        for nx, ny in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:
            if 0<=nx<H and 0<=ny<W:
                if cost_map[nx][ny] is None:
                    cost_map[nx][ny] = current + 1
                    queue.append([nx, ny])
                elif current+1 < cost_map[nx][ny]:
                    cost_map[nx][ny] = current + 1
                    queue.append([nx, ny])
if cost_map[0][0] is None:
    print(0)
    exit()
result = 0
for i in range(H):
    for j in range(W):
        if cost_map[i][j]>result:
            result = cost_map[i][j]
print(result)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
cost_map = [[None]*W for i in range(H)]
# 迷路を最初のマス目の数だけ解く
starts = list()
for x in range(H):
    for y in range(W):
        if map[x][y]=="#":
            starts.append([x, y])
for sx, sy in starts:
    queue = [[sx, sy]]
    cost_map[sx][sy] = 0
    while queue:
        x, y = queue.pop(0)
        current = cost_map[x][y]
        for nx, ny in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:
            if 0<=nx<H and 0<=ny<W:
                if cost_map[nx][ny] is None:
                    cost_map[nx][ny] = current + 1
                    queue.append([nx, ny])
                elif current+1 < cost_map[nx][ny]:
                    cost_map[nx][ny] = current + 1
                    queue.append([nx, ny])
result = 0
for i in range(H):
    for j in range(W):
        if cost_map[i][j]>result:
            result = cost_map[i][j]
print(result)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
queue = list()
for x in range(H):
    for y in range(W):
        if map[x][y]=="#":
            queue.append([x, y])
count = -1
while queue:
    next_queue = list()
    for x, y in queue:
        for nx, ny in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:
            if 0<=nx<H and 0<=ny<W:
                if map[nx][ny]==".":
                    map[nx][ny] = "#"
                    next_queue.append([nx, ny])
    queue = next_queue
    count += 1
print(count)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
queue = list()
for x in range(H):
    for y in range(W):
        if map[x][y]=="#":
            queue.append([x, y])
def check_all_black():
    checktext = "".join(["".join(l) for l in map])
    if "." in checktext:
        return False
    else:
        return True
count = 0
if check_all_black():
    print(count)
    exit()
while queue:
    next_queue = list()
    for x, y in queue:
        for nx, ny in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:
            if 0<=nx<H and 0<=ny<W:
                if map[nx][ny]==".":
                    map[nx][ny] = "#"
                    next_queue.append([nx, ny])
    queue = next_queue
    count += 1
    if check_all_black():
        break
print(count)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
queue = list()
for x in range(H):
    for y in range(W):
        if map[x][y]=="#":
            queue.append([x, y])
def check_all_black():
    global map
    for x in range(H):
        for y in range(W):
            if map[x][y]==".":
                break
        else:
            continue
        break
    else:
        return True
    return False
count = 0
if check_all_black():
    print(count)
    exit()
while queue:
    next_queue = list()
    for x, y in queue:
        for nx, ny in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:
            if 0<=nx<H and 0<=ny<W:
                if map[nx][ny]==".":
                    map[nx][ny] = "#"
                    next_queue.append([nx, ny])
    queue = next_queue
    count += 1
    if check_all_black():
        break
print(count)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
H, W = map(int, input().split())
map = [list(input()) for i in range(H)]
checked = list()
queue = deque()
for x in range(H):
    checked.append(list())
    for y in range(W):
        if map[x][y]=="#":
            checked[x].append(True)
            queue.append((x, y, 0))
        else:
            checked[x].append(False)
neighbors = [(1,0), (0,1), (-1,0), (0,-1)]
while queue:
    x, y, res = queue.popleft()
    for dx, dy in neighbors:
        nx, ny = x+dx, y+dy
        if 0<=nx<H and 0<=ny<W:
            if not checked[nx][ny]:
                queue.append((nx, ny, res+1))
                checked[nx][ny] = True
print(res)