Buggy python submission (verdict: Time Limit Exceeded):
from queue import deque
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

que = deque([])

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            visited.add((i, j))
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#' or (i+ud, j+lr) in visited:
                    continue
                que.append((i+ud, j+lr))

res = 0

while que:
    res += 1
    for k in range(len(que)):
        i, j = que.popleft()
        visited.add((i, j))
        for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
            if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#' or (i+ud, j+lr) in visited:
                continue
            que.append((i+ud, j+lr))
print(res)



Fixed submission:
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

visited = set()

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                visited.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'

res = 0

while visited:
    res += 1
    buf = set()
    for i, j in visited:
        if matrix[i][j] == '#' and (i, j) not in buf:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                buf.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'
    if not buf:
        break
    visited = buf

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from queue import deque
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

que = deque([])

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            visited.add((i, j))
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                que.append((i+ud, j+lr))

res = 0

while que:
    for k in range(len(que)):
        i, j = que.popleft()
        if (i, j) in visited:
            continue
        visited.add((i, j))
        for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
            if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                continue
            que.append((i+ud, j+lr))
    if que:
        res += 1
print(res)



Fixed submission:
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

visited = set()

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                visited.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'

res = 0

while visited:
    res += 1
    buf = set()
    for i, j in visited:
        if matrix[i][j] == '#' and (i, j) not in buf:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                buf.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'
    if not buf:
        break
    visited = buf

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

visited = set()

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                visited.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'

res = 0

while visited:
    res += 1
    buf = set()
    for i, j in visited:
        if matrix[i][j] == '#' and (i, j) not in buf:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                buf.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'
    if not buf:
        break
    visited = buf

print(res)



Fixed submission:
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

visited = set()

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                visited.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'

res = 0

while visited:
    res += 1
    buf = set()
    for i, j in visited:
        if matrix[i][j] == '#' and (i, j) not in buf:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                buf.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'
    if not buf:
        break
    visited = buf

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

matrix = []

res = 0

visited = set()

for i in range(H):
    matrix.append(list(input()))

while True:
    visited = set()
    count = 0
    for i in range(H):
        for j in range(W):
            if matrix[i][j] == '#' and (i, j) not in visited:
                for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                    if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                        continue
                    visited.add((i+ud, j+lr))
                    matrix[i+ud][j+lr] = '#'
                    count += 1
    if count == 0:
        break
    res += 1
print(res)



Fixed submission:
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

visited = set()

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                visited.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'

res = 0

while visited:
    res += 1
    buf = set()
    for i, j in visited:
        if matrix[i][j] == '#' and (i, j) not in buf:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                buf.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'
    if not buf:
        break
    visited = buf

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

matrix = []

res = 0

visited = set()

for i in range(H):
    matrix.append(list(input()))

while True:
    visited = set()
    count = 0
    for i in range(H):
        for j in range(W):
            if matrix[i][j] == '#' and (i, j) not in visited:
                for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                    if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                        continue
                    visited.add((i+ud, j+lr))
                    matrix[i+ud][j+lr] = '#'
                    count += 1
    if count == 0:
        break
    res += 1
print(res)



Fixed submission:
H, W = map(int, input().split())

matrix = []

visited = set()

for i in range(H):
    matrix.append(list(input()))

visited = set()

for i in range(H):
    for j in range(W):
        if matrix[i][j] == '#' and (i, j) not in visited:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                visited.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'

res = 0

while visited:
    res += 1
    buf = set()
    for i, j in visited:
        if matrix[i][j] == '#' and (i, j) not in buf:
            for ud, lr in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                if not(0 <= i+ud < H and 0 <= j+lr < W)  or matrix[i+ud][j+lr] == '#':
                    continue
                buf.add((i+ud, j+lr))
                matrix[i+ud][j+lr] = '#'
    if not buf:
        break
    visited = buf

print(res)