Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

state = []
# 初期状態
for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            state.append([w, h])

tmp = []
ret = 0
while len(state) > 0:
    tmp = list()
    ret += 1
    for x, y in state:
        for nx, ny in ((1,0), (-1,0), (0,1), (0,-1)):
            nx += x
            ny += y
            if nx < 0 or ny < 0 or nx >= W or ny >= H:
                continue

            if field[ny][nx] == ".":
                field[ny][nx] = "#"
                tmp.append([nx, ny])
    state = tmp
    


print(ret-1)

Fixed submission:
H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

state = []
# 初期状態
for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            state.append([w, h])

tmp = []
ret = 0
while len(state) > 0:
    tmp = list()
    ret += 1
    for x, y in state:
        for nx, ny in ((1,0), (-1,0), (0,1), (0,-1)):
            nx += x
            ny += y
            if nx < 0 or ny < 0 or nx >= W or ny >= H:
                continue

            if field[ny][nx] == ".":
                field[ny][nx] = "#"
                tmp.append([nx, ny])
    state = tmp

print(ret-1)

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]
tmp_field = deepcopy(field)
def fill(x, y):
    ret = 0
    tmp = list()
    for nx, ny in ((1,0), (-1,0), (0,1), (0,-1)):
        nx += x
        ny += y
        if nx < 0 or ny < 0 or nx >= W or ny >= H:
            continue

        if "." == field[ny][nx]:
            tmp_field[ny][nx] = "#"
            ret = 1
    return ret

ret = -1
tmp = 1
while tmp > 0:
    tmp = 0
    ret += 1
    for h in range(H):
        for w in range(W):
            if field[h][w] == "#":
                tmp_field[h][w] = "$"
                tmp += fill(w, h)
    field = deepcopy(tmp_field)
print(ret)

Fixed submission:
H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

state = []
# 初期状態
for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            state.append([w, h])

tmp = []
ret = 0
while len(state) > 0:
    tmp = list()
    ret += 1
    for x, y in state:
        for nx, ny in ((1,0), (-1,0), (0,1), (0,-1)):
            nx += x
            ny += y
            if nx < 0 or ny < 0 or nx >= W or ny >= H:
                continue

            if field[ny][nx] == ".":
                field[ny][nx] = "#"
                tmp.append([nx, ny])
    state = tmp

print(ret-1)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
C = [list(input()) for _ in range(H)]

pre_list = []
# set start point
for iidx, i in enumerate(C):
    for tidx, t in enumerate(i):
        if t == '#':
            pre_list.append((iidx, tidx))
            C[iidx][tidx] = 0

# bfs 
count = 0
 
while len(pre_list) > 0:
    count += 1
    next_list = []
    for y,x in pre_list:
        for i,j in ((1,0),(0,-1),(0,1),(-1,0)):
            if y+i < 0 or x+j < 0 or y+i >= H or x+j >= W:
                continue
            if C[y+i][x+j] == '.':
                C[y+i][x+j] = count
                next_list.append((y+i,x+j))
    pre_list = next_list
print(count-1)


Fixed submission:
H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

state = []
# 初期状態
for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            state.append([w, h])

tmp = []
ret = 0
while len(state) > 0:
    tmp = list()
    ret += 1
    for x, y in state:
        for nx, ny in ((1,0), (-1,0), (0,1), (0,-1)):
            nx += x
            ny += y
            if nx < 0 or ny < 0 or nx >= W or ny >= H:
                continue

            if field[ny][nx] == ".":
                field[ny][nx] = "#"
                tmp.append([nx, ny])
    state = tmp

print(ret-1)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
C = [list(input()) for _ in range(H)]

pre_list = []
# set start point
for iidx, i in enumerate(C):
    for tidx, t in enumerate(i):
        if t == '#':
            pre_list.append((iidx, tidx))
            C[iidx][tidx] = 0

# bfs 
count = 0
 
while len(pre_list) > 0:
    count += 1
    next_list = []
    for y,x in pre_list:
        for i,j in ((1,0),(0,-1),(0,1),(-1,0)):
            if y+i < 0 or x+j < 0 or y+i >= H or x+j >= W:
                continue
            if C[y+i][x+j] == '.':
                C[y+i][x+j] = "count"
                next_list.append((y+i,x+j))
    pre_list = next_list
print(count-1)


Fixed submission:
H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

state = []
# 初期状態
for h in range(H):
    for w in range(W):
        if field[h][w] == "#":
            state.append([w, h])

tmp = []
ret = 0
while len(state) > 0:
    tmp = list()
    ret += 1
    for x, y in state:
        for nx, ny in ((1,0), (-1,0), (0,1), (0,-1)):
            nx += x
            ny += y
            if nx < 0 or ny < 0 or nx >= W or ny >= H:
                continue

            if field[ny][nx] == ".":
                field[ny][nx] = "#"
                tmp.append([nx, ny])
    state = tmp

print(ret-1)