Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
dist = [[INF] * W for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
INF = H * W
A = [list(input()) for x in range(H)]
dist = [[INF] * W for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
while counter != len(q):
    item = q[counter]

    if A[item[0]][item[1]] == '#':
        dist[item[0]][item[1]] = 0
    else:
        dist[item[0]][item[1]] = min(
            dist[item[0] - 1][item[1]] if item[0] > 0 else INF,
            dist[item[0]][item[1]-1] if item[1] > 0 else INF,
            dist[item[0] + 1][item[1]] if item[0] < H - 1 else INF,
            dist[item[0]][item[1]+1] if item[1] < W - 1 else INF
        ) + 1
        if max_dist < dist[item[0]][item[1]]: max_dist = dist[item[0]][item[1]] 


    if item[0] > 0 and not used[item[0]-1][item[1]]:
        q.append([item[0] - 1, item[1]])
        used[item[0] - 1][item[1]] = True
    if item[1] > 0 and not used[item[0]][item[1] - 1]:
        q.append([item[0], item[1] - 1])
        used[item[0]][item[1] - 1] = True
    if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
        q.append([item[0] + 1, item[1]])
        used[item[0] + 1][item[1]] = True
    if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
        q.append([item[0], item[1] + 1])
        used[item[0]][item[1] + 1] = True
    counter += 1

print(max_dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

H, W = map(int, input().split())
A = []
dist = []
black = []
for i in range(H):
    A.append(list(input()))
    dist.append([])
    for j in range(W):
        dist[i].append([1000000, False])
        if A[i][j] == '#':
            black.append([i, j])

q = queue.Queue(maxsize=-1)
for b in black:
    q.put(b)

max_dist = 0
while not q.empty():
    item = q.get()
    if i < 0 or j < 0 or i > H - 1 or j > W - 1:
        continue
    if A[item[0]][item[1]] == '#':
        dist[item[0]][item[1]][0] = 0
    else:
        a = dist[item[0] - 1][item[1]][0] if item[0] > 0 else 1000000
        b = dist[item[0]][item[1] - 1][0] if item[1] > 0 else 1000000
        c = dist[item[0] + 1][item[1]][0] if item[0] < H - 1 else 1000000
        d = dist[item[0]][item[1] + 1][0] if item[1] < W - 1 else 1000000
        dist[item[0]][item[1]][0] = min(a, b, c, d) + 1
        if dist[item[0]][item[1]][0] > max_dist:
            max_dist = dist[item[0]][item[1]][0]

    if item[0] > 0 and not dist[item[0] - 1][item[1]][1]:
        q.put([item[0] - 1, item[1]])
        dist[item[0] - 1][item[1]][1] = True
    if item[1] > 0 and not dist[item[0]][item[1] - 1][1]:
        q.put([item[0], item[1] - 1])
        dist[item[0]][item[1] - 1][1] = True
    if item[0] < H - 1 and not dist[item[0] + 1][item[1]][1]:
        q.put([item[0] + 1, item[1]])
        dist[item[0] + 1][item[1]][1] = True
    if item[1] < W - 1 and not dist[item[0]][item[1] + 1][1]:
        q.put([item[0], item[1] + 1])
        dist[item[0]][item[1] + 1][1] = True

print(max_dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

H, W = map(int, input().split())
A = []
dist = []
black = []
for i in range(H):
    A.append(list(input()))
    dist.append([])
    for j in range(W):
        dist[i].append([1000000, False])
        if A[i][j] == '#':
            black.append([i, j])

q = queue.Queue(maxsize=-1)
for b in black:
    q.put(b)

max_dist = 0
while not q.empty():
    item = q.get()
    if i < 0 or j < 0 or i > H - 1 or j > W - 1:
        continue
    if A[item[0]][item[1]] == '#':
        dist[item[0]][item[1]][0] = 0
    else:
        a = dist[item[0] - 1][item[1]][0] if item[0] > 0 else 1000000
        b = dist[item[0]][item[1] - 1][0] if item[1] > 0 else 1000000
        c = dist[item[0] + 1][item[1]][0] if item[0] < H - 1 else 1000000
        d = dist[item[0]][item[1] + 1][0] if item[1] < W - 1 else 1000000
        dist[item[0]][item[1]][0] = min(a, b, c, d) + 1
        if dist[item[0]][item[1]][0] > max_dist:
            max_dist = dist[item[0]][item[1]][0]

    if item[0] > 0 and not dist[item[0] - 1][item[1]][1]:
        q.put([item[0] - 1, item[1]])
        dist[item[0] - 1][item[1]][1] = True
    if item[1] > 0 and not dist[item[0]][item[1] - 1][1]:
        q.put([item[0], item[1] - 1])
        dist[item[0]][item[1] - 1][1] = True
    if item[0] < H - 1 and not dist[item[0] + 1][item[1]][1]:
        q.put([item[0] + 1, item[1]])
        dist[item[0] + 1][item[1]][1] = True
    if item[1] < W - 1 and not dist[item[0]][item[1] + 1][1]:
        q.put([item[0], item[1] + 1])
        dist[item[0]][item[1] + 1][1] = True

print(max_dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

H, W = map(int, input().split())
A = []
dist = []
black = []
for i in range(H):
    A.append(list(input()))
    dist.append([])
    for j in range(W):
        dist[i].append(1000000)
        if A[i][j] == '#':
            black.append([i, j])

q = queue.Queue(maxsize=-1)
for b in black:
    q.put(b)

max_dist = 0
while not q.empty():
    item = q.get()

    if i < 0 or j < 0 or i > H - 1 or j > W - 1:
        continue
    if A[item[0]][item[1]] == '#':
        dist[item[0]][item[1]] = 0
    else:
        a = dist[item[0] - 1][item[1]] if item[0] > 0 else 1000000
        b = dist[item[0]][item[1] - 1] if item[1] > 0 else 1000000
        c = dist[item[0] + 1][item[1]] if item[0] < H - 1 else 1000000
        d = dist[item[0]][item[1] + 1] if item[1] < W - 1 else 1000000
        dist[item[0]][item[1]] = min(a, b, c, d) + 1
        if dist[item[0]][item[1]] > max_dist:
            max_dist = dist[item[0]][item[1]]

    if item[0] > 0 and dist[item[0]-1][item[1]] == 1000000: q.put([item[0] - 1,item[1]])
    if item[1] > 0 and dist[item[0]][item[1]-1] == 1000000: q.put([item[0], item[1] - 1])
    if item[0] < H - 1 and dist[item[0]+1][item[1]] == 1000000: q.put([item[0] + 1, item[1]])
    if item[1] < W - 1 and dist[item[0]][item[1]+1] == 1000000: q.put([item[0], item[1] + 1])

print(max_dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = []
black = []
white = []
for i in range(H):
    A.append(list(input()))
    for j in range(W):
        if A[i][j] == '#':
            black.append([i, j])
        else:
            white.append([i, j])

max_dist = 0
for w in white:
    dist = 1000000
    for b in black:
        d = abs(b[0] - w[0]) + abs(b[1] - w[1])
        if dist > d:
            dist = d
        if dist == 1:
            break
    if max_dist < dist:
        max_dist = dist
print(max_dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = []
black = []
white = []
for i in range(H):
    A.append(list(input()))
    for j in range(W):
        if A[i][j] == '#':
            black.append([i, j])
        else:
            white.append([i, j])

max_dist = 0
for w in white:
    dist = min([abs(b[0] - w[0]) + abs(b[1] - w[1]) for b in black])
    if max_dist < dist:
        max_dist = dist
print(max_dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

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

H, W = map(int, input().split())
A = []
black = []
for i in range(H):
    A.append(list(input()))
    for j in range(W):
        if A[i][j] == '#':
            black.append([i,j])

counter = len(black)
dist = 0
cur_black = copy.deepcopy(black)
next_black = copy.deepcopy(black)
while True:
    if counter == W * H:
        break
    dist += 1
    black = copy.deepcopy(next_black)
    next_black = []
    for b in black:
        if b[0] != 0 and [b[0] - 1, b[1]] not in cur_black:
            cur_black.append([b[0] - 1, b[1]])
            next_black.append([b[0] - 1, b[1]])
            counter += 1
        if b[1] != 0 and [b[0], b[1] - 1] not in cur_black:
            cur_black.append([b[0], b[1] - 1])
            next_black.append([b[0], b[1] -1])
            counter += 1
        if b[0] != H - 1 and [b[0] + 1, b[1]] not in cur_black:
            cur_black.append([b[0] + 1, b[1]])
            next_black.append([b[0] + 1, b[1]])
            counter += 1
        if b[1] != W - 1 and [b[0], b[1] + 1] not in cur_black:
            cur_black.append([b[0], b[1] + 1])
            next_black.append([b[0], b[1] + 1])
            counter += 1            
print(dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

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

H, W = map(int, input().split())
A = []
black = []
for i in range(H):
    A.append(list(input()))
    for j in range(W):
        if A[i][j] == '#':
            black.append([i,j])

counter = len(black)
dist = 0
while True:
    if counter == W * H:
        break
    dist += 1
    next_black = []
    for b in black:
        cur_black = black + next_black
        if b[0] != 0 and [b[0] - 1, b[1]] not in cur_black:
            next_black.append([b[0] - 1, b[1]])
            counter += 1
        if b[1] != 0 and [b[0], b[1] - 1] not in cur_black:
            next_black.append([b[0], b[1] - 1])
            counter += 1
        if b[0] != H - 1 and [b[0] + 1, b[1]] not in cur_black:
            next_black.append([b[0] + 1, b[1]])
            counter += 1
        if b[1] != W - 1 and [b[0], b[1] + 1] not in cur_black:
            next_black.append([b[0], b[1] + 1])
            counter += 1            
    black += next_black
print(dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = []
black = []
for i in range(H):
    A.append(list(input()))
    for j in range(W):
        if A[i][j] == '#':
            black.append([i,j])

max_dist = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            continue
        else:
            min_dist = 10000000
            for b in black:
                cur_dist = abs(b[0] - i) + abs(b[1] - j)
                if cur_dist < min_dist:
                    min_dist = cur_dist
                if min_dist == 1:
                    break
            if max_dist < min_dist:
                max_dist = min_dist

print(max_dist)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = []
black = []
for i in range(H):
    A.append(list(input()))
    for j in range(W):
        if A[i][j] == '#':
            black.append([i,j])

dist = []
for i in range(H):
    dist.append([])
    for j in range(W):
        if A[i][j] == '#':
            dist[i].append(0)
        else:
            min_dist = 10000000
            for b in black:
                cur_dist = abs(b[0] - i) + abs(b[1] - j)
                if cur_dist < min_dist:
                    min_dist = cur_dist
                if min_dist == 1:
                    break
            dist[i].append(min_dist)

print(max([max(r) for r in dist]))

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

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

H, W = map(int, input().split())
A = []
black = []
for i in range(H):
    A.append(list(input()))
    for j in range(W):
        if A[i][j] == '#':
            black.append([i,j])

dist = []
for i in range(H):
    dist.append([])
    for j in range(W):
        dist[i].append(min([abs(b[0] - i) + abs(b[1] - j) for b in black]))

print(max([max(r) for r in dist]))

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)

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

H, W = map(int, input().split())
A = []
for i in range(H):
    A.append(list(input()))

def become_black(A, i, j):
    return (i != 0 and A[i - 1][j] == '#') \
    or (j != 0 and A[i][j - 1] == '#') \
    or (i != H-1 and A[i + 1][j] == '#') \
    or (j != W-1 and A[i][j+1] == '#')


white_exist = True
counter = 0
while True:
    prev_A = copy.deepcopy(A)
    white_exist = False
    for i in range(H):
        if '.' in A[i]:
            white_exist = True
    if not white_exist:
        break

    for i in range(H):
        for j in range(W):
            if prev_A[i][j] == '.' and become_black(prev_A, i, j):
                A[i][j] = '#'
    counter += 1
print(counter)

Fixed submission:
H, W = map(int, input().split())
INF = H * W
A = [input() for x in range(H)]
used = [[False] * W for x in range(H)]

q = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            q.append([i, j])
            used[i][j] = True

max_dist = 0
counter = 0
depth = -1
while counter != INF:
    # print(q)
    # print(counter)
    for i in range(len(q) - counter):
        item = q[counter]

        if item[0] > 0 and not used[item[0]-1][item[1]]:
            q.append([item[0] - 1, item[1]])
            used[item[0] - 1][item[1]] = True
        if item[1] > 0 and not used[item[0]][item[1] - 1]:
            q.append([item[0], item[1] - 1])
            used[item[0]][item[1] - 1] = True
        if item[0] < H - 1 and not used[item[0] + 1][item[1]]:
            q.append([item[0] + 1, item[1]])
            used[item[0] + 1][item[1]] = True
        if item[1] < W - 1 and not used[item[0]][item[1] + 1]:
            q.append([item[0], item[1] + 1])
            used[item[0]][item[1] + 1] = True
        counter += 1
    depth += 1

print(depth)