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


H, W = [int(_) for _ in input().split()]

queues = []


def f(x, i):
    for j in range(W):
        if x[j] == '#':
            queues.append(deque([[j, i, 0]]))
    return list(x)


M = [f(input(), i) for i in range(H)]


depth_now = 0
depth = 0
while True:
    for q in queues:
        while len(q) != 0 and q[0][2] == depth_now:
            p = q.popleft()
            x = p[0]
            y = p[1]
            depth = p[2]
            if x - 1 >= 0 and M[y][x-1] == '.':
                q.append([x-1, y, depth+1])
                M[y][x-1] = '#'
            if x + 1 < W and M[y][x+1] == '.':
                q.append([x+1, y, depth+1])
                M[y][x+1] = '#'
            if y - 1 >= 0 and M[y-1][x] == '.':
                q.append([x, y-1, depth+1])
                M[y-1][x] = '#'
            if y + 1 < H and M[y+1][x] == '.':
                q.append([x, y+1, depth+1])
                M[y+1][x] = '#'
    queues = [q for q in queues if len(q) != 0]
    if len(queues) == 0:
        print(depth_now)
        break
    depth_now += 1


Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

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


H, W = [int(_) for _ in input().split()]

M = []
queues = []
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            queues.append(deque([[j, i, 0]]))

depth_now = 0
depth = 0
while True:
    for q in queues:
        while len(q) != 0 and q[0][2] == depth_now:
            p = q.popleft()
            x = p[0]
            y = p[1]
            depth = p[2]
            if x - 1 >= 0 and M[y][x-1] == '.':
                q.append([x-1, y, depth+1])
                M[y][x-1] = '#'
            if x + 1 < W and M[y][x+1] == '.':
                q.append([x+1, y, depth+1])
                M[y][x+1] = '#'
            if y - 1 >= 0 and M[y-1][x] == '.':
                q.append([x, y-1, depth+1])
                M[y-1][x] = '#'
            if y + 1 < H and M[y+1][x] == '.':
                q.append([x, y+1, depth+1])
                M[y+1][x] = '#'
    queues = [q for q in queues if len(q) != 0]
    if len(queues) == 0:
        print(depth_now)
        break
    depth_now += 1


Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

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


H, W = [int(_) for _ in input().split()]

M = []
queues = []
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            queues.append(deque([[j, i, 0]]))

depth_now = 0
depth = 0
while True:
    for q in queues:
        while len(q) != 0 and q[0][2] == depth_now:
            p = q.popleft()
            x = p[0]
            y = p[1]
            depth = p[2]
            cands = []
            if x - 1 >= 0:
                cands.append([x-1, y])
            if x + 1 < W:
                cands.append([x+1, y])
            if y - 1 >= 0:
                cands.append([x, y-1])
            if y + 1 < H:
                cands.append([x, y+1])
            for cx, cy in cands:
                if M[cy][cx] == '.':
                    q.append([cx, cy, depth+1])
                    M[cy][cx] = '#'
    queues = [q for q in queues if len(q) != 0]
    if len(queues) == 0:
        print(depth_now)
        break
    depth_now += 1


Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

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


H, W = [int(_) for _ in input().split()]

M = []
queues = []
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            queues.append(deque([[j, i, 0]]))

depth_now = 0
depth = 0
while True:
    for q in queues:
        while len(q) != 0 and q[0][2] == depth_now:
            p = q.popleft()
            x = p[0]
            y = p[1]
            depth = p[2]
            cands = []
            if x - 1 >= 0:
                cands.append([x-1, y])
            if x + 1 < W:
                cands.append([x+1, y])
            if y - 1 >= 0:
                cands.append([x, y-1])
            if y + 1 < H:
                cands.append([x, y+1])
            for cx, cy in cands:
                if M[cy][cx] == '.':
                    q.append([cx, cy, depth+1])
                    M[cy][cx] = '#'
    queues = [q for q in queues if len(q) != 0]
    if len(queues) == 0:
        print(depth_now)
        break
    depth_now += 1


Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

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


H, W = [int(_) for _ in input().split()]

M = []
queues = []
for i in range(H):
    A = input()
    M.append(A)
    for j in range(W):
        if A[j] == '#':
            queues.append(deque([[j, i, 0]]))

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

max_count = 0
while True:
    for q in queues:
        if len(q) == 0:
            continue
        p = q.popleft()
        x = p[0]
        y = p[1]
        count = p[2]
        max_count = max(max_count, count)
        cands = []
        if x - 1 >= 0:
            cands.append([x-1, y])
        if x + 1 < W:
            cands.append([x+1, y])
        if y - 1 >= 0:
            cands.append([x, y-1])
        if y + 1 < H:
            cands.append([x, y+1])
        for cx, cy in cands:
            if M[cy][cx] == '.' and not visited[cy][cx]:
                q.append([cx, cy, count+1])
                visited[cy][cx] = True
    if len([q for q in queues if len(q) == 0]) == len(queues):
        print(max_count)
        break


Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = [int(i) for i in input().split()]
a = []
b = []
for i in range(h):
    a.append(list(input()))
    for j in range(w):
        if a[i][j] == "#":
            b.append([i, j])

for cnt in range(h+w):
    c = []
    for i in range(len(b)):
        y = b[i][0]
        x = b[i][1]
        if y != 0:
            if a[y-1][x] != "#":
                a[y-1][x] = "#"
                c.append([y-1, x])
        if y != h-1:
            if a[y+1][x] != "#":
                a[y+1][x] = "#"
                c.append([y+1, x])
        if x != 0:
            if a[y][x-1] != "#":
                a[y][x-1] = "#"
                c.append([y, x-1])
        if x != w-1:
            if a[y][x+1] != "#":
                a[y][x+1] = "#"
                c.append([y, x+1])
    if len(c) == 0:
        print(cnt)
        break
    b = c

Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = [int(i) for i in input().split()]
a = []
b = []
for i in range(h):
    a.append(list(input()))
    for j in range(w):
        if a[i][j] == "#":
            b.append([i, j])

for cnt in range(h+w):
    c = []
    for i in range(len(b)):
        y = b[i][0]
        x = b[i][1]
        if y > 0:
            if a[y-1][x] != "#":
                a[y-1][x] = "#"
                c.append([y-1, x])
        if y < h-1:
            if a[y+1][x] != "#":
                a[y+1][x] = "#"
                c.append([y+1, x])
        if x > 0:
            if a[y][x-1] != "#":
                a[y][x-1] = "#"
                c.append([y, x-1])
        if x < w-1:
            if a[y][x+1] != "#":
                a[y][x+1] = "#"
                c.append([y, x+1])
    if len(c) == 0:
        print(cnt)
        break
    b = c

Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = [int(i) for i in input().split()]
a = []
b = []
for i in range(h):
    a.append(list(input()))
    for j in range(w):
        if a[i][j] == "#":
            b.append([i, j])

cnt = 0
while True:
    c = []
    for i in range(len(b)):
        y = b[i][0]
        x = b[i][1]
        if y > 0:
            if a[y-1][x] != "#":
                a[y-1][x] = "#"
                c.append([y-1, x])
        if y < h-1:
            if a[y+1][x] != "#":
                a[y+1][x] = "#"
                c.append([y+1, x])
        if x > 0:
            if a[y][x-1] != "#":
                a[y][x-1] = "#"
                c.append([y, x-1])
        if x < w-1:
            if a[y][x+1] != "#":
                a[y][x+1] = "#"
                c.append([y, x+1])
    if len(c) == 0:
        break
    cnt += 1
    b = c
print(cnt)

Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = [int(i) for i in input().split()]
a = []
b = []
for i in range(h):
    a.append(list(input()))
    for j in range(w):
        if a[i][j] == "#":
            b.append([i, j])

cnt = 0
ans = [["#" for i in range(w)] for j in range(h)]
while True:
    c = []
    for i in range(len(b)):
        y = b[i][0]
        x = b[i][1]
        if y > 0:
            if a[y-1][x] != "#":
                a[y-1][x] = "#"
                c.append([y-1, x])
        if y < h-1:
            if a[y+1][x] != "#":
                a[y+1][x] = "#"
                c.append([y+1, x])
        if x > 0:
            if a[y][x-1] != "#":
                a[y][x-1] = "#"
                c.append([y, x-1])
        if x < w-1:
            if a[y][x+1] != "#":
                a[y][x+1] = "#"
                c.append([y, x+1])
    if c == []:
        break
    cnt += 1
    b = c
print(cnt)

Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = [int(i) for i in input().split()]
a = []
b = []
for i in range(h):
    a.append(list(input()))
    for j in range(w):
        if a[i][j] == "#":
            b.append([i, j])

cnt = 0
ans = [["#" for i in range(w)] for j in range(h)]
while True:
    c = []
    if a == ans:
        break
    cnt += 1
    for i in range(len(b)):
        y = b[i][0]
        x = b[i][1]
        if y > 0:
            if a[y-1][x] != "#":
                a[y-1][x] = "#"
                c.append([y-1, x])
        if y < h-1:
            if a[y+1][x] != "#":
                a[y+1][x] = "#"
                c.append([y+1, x])
        if x > 0:
            if a[y][x-1] != "#":
                a[y][x-1] = "#"
                c.append([y, x-1])
        if x < w-1:
            if a[y][x+1] != "#":
                a[y][x+1] = "#"
                c.append([y, x+1])
    b = c
print(cnt)

Fixed submission:
from collections import deque
import sys


H, W = [int(_) for _ in input().split()]

M = []
q = deque([])
for i in range(H):
    A = input()
    M.append(list(A))
    for j in range(W):
        if A[j] == '#':
            q.append([j, i, 0])

depth_now = 0
depth = 0
count = 0
while len(q) > 0:
    p = q.popleft()
    x = p[0]
    y = p[1]
    depth = p[2]
    if x - 1 >= 0 and M[y][x-1] == '.':
        q.append([x-1, y, depth+1])
        M[y][x-1] = '#'
    if x + 1 < W and M[y][x+1] == '.':
        q.append([x+1, y, depth+1])
        M[y][x+1] = '#'
    if y - 1 >= 0 and M[y-1][x] == '.':
        q.append([x, y-1, depth+1])
        M[y-1][x] = '#'
    if y + 1 < H and M[y+1][x] == '.':
        q.append([x, y+1, depth+1])
        M[y+1][x] = '#'
print(depth)