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

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

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


ans = 0

que = deque()

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            que.append((i, j))

def check(field):
    flag = False
    for i in range(H):
        if "." in field[i]:
            flag = True

    return flag

def one_bsf():
    global que
    global ans

    for _ in range(len(que)):
        n = que.popleft()
        ny = n[0]
        nx = n[1]

        for i in range(4):
            nny = ny + dy[i]
            nnx = nx + dx[i]

            if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                continue

            elif field[nny][nnx] == ".":
                field[nny][nnx] = "#"
                que.append((nny, nnx))

    ans += 1

while check(field):
    one_bsf()
    
print(ans)

Fixed submission:
from collections import deque

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

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


ans = 0

que = deque()

num_white = H * W

paint = 0

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            que.append((i, j))
            num_white -= 1

def check(field):
    flag = False
    for i in range(H):
        if "." in field[i]:
            flag = True

    return flag

def one_bsf():
    global paint
    global que
    global ans

    for _ in range(len(que)):
        n = que.popleft()
        ny = n[0]
        nx = n[1]

        for i in range(4):
            nny = ny + dy[i]
            nnx = nx + dx[i]

            if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                continue

            elif field[nny][nnx] == ".":
                field[nny][nnx] = "#"
                paint += 1
                que.append((nny, nnx))

    ans += 1

while num_white != paint:
    one_bsf()
    
print(ans)

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

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

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


ans = 0

que = deque()

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            que.append((i, j))

def check(field):
    flag = False
    for i in range(H):
        if "." in field[i]:
            flag = True

    return flag

def one_bsf():
    global que
    global ans

    for _ in range(len(que)):
        n = que.popleft()
        ny = n[0]
        nx = n[1]

        for i in range(4):
            nny = ny + dy[i]
            nnx = nx + dx[i]

            if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                continue

            elif field[nny][nnx] == ".":
                field[nny][nnx] = "#"
                que.append((nny, nnx))

    ans += 1

while check(field):
    one_bsf()
    
print(ans)

Fixed submission:
from collections import deque

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

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


ans = 0

que = deque()

num_white = H * W

paint = 0

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            que.append((i, j))
            num_white -= 1

def check(field):
    flag = False
    for i in range(H):
        if "." in field[i]:
            flag = True

    return flag

def one_bsf():
    global paint
    global que
    global ans

    for _ in range(len(que)):
        n = que.popleft()
        ny = n[0]
        nx = n[1]

        for i in range(4):
            nny = ny + dy[i]
            nnx = nx + dx[i]

            if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                continue

            elif field[nny][nnx] == ".":
                field[nny][nnx] = "#"
                paint += 1
                que.append((nny, nnx))

    ans += 1

while num_white != paint:
    one_bsf()
    
print(ans)

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

H, W = map(int, input().split())
field = tuple([input() for _ in range(H)])

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


#(sy, sx)に一番近いところを探す
def bsf(sy, sx):

    if field[sy][sx] == "#":
        return 0
    
    else:
        dist = [[-1 for x in range(W)] for y in range(H)]
        que = deque()
        que.append((sy, sx))
        dist[sy][sx] = 0

        while len(que):
            n = que.popleft()
            ny = n[0]
            nx = n[1]

            for i in range(4):
                nny = ny + dy[i]
                nnx = nx + dx[i]

                if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                    continue

                elif field[nny][nnx] == "." and dist[nny][nnx] == -1:
                    dist[nny][nnx] = dist[ny][nx] + 1
                    que.append((nny, nnx))

                elif field[nny][nnx] == "#":
                    return dist[ny][nx] + 1

l = []

for i in range(H):
    for j in range(W):
        l.append(bsf(i, j))

print(max(l))

Fixed submission:
from collections import deque

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

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


ans = 0

que = deque()

num_white = H * W

paint = 0

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            que.append((i, j))
            num_white -= 1

def check(field):
    flag = False
    for i in range(H):
        if "." in field[i]:
            flag = True

    return flag

def one_bsf():
    global paint
    global que
    global ans

    for _ in range(len(que)):
        n = que.popleft()
        ny = n[0]
        nx = n[1]

        for i in range(4):
            nny = ny + dy[i]
            nnx = nx + dx[i]

            if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                continue

            elif field[nny][nnx] == ".":
                field[nny][nnx] = "#"
                paint += 1
                que.append((nny, nnx))

    ans += 1

while num_white != paint:
    one_bsf()
    
print(ans)

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

H, W = map(int, input().split())
field = tuple([input() for _ in range(H)])

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


#(sy, sx)に一番近いところを探す
def bsf(sy, sx):

    if field[sy][sx] == "#":
        return 0
    
    else:
        dist = [[-1 for x in range(W)] for y in range(H)]
        que = deque()
        que.append((sy, sx))
        dist[sy][sx] = 0

        while len(que):
            n = que.popleft()
            ny = n[0]
            nx = n[1]

            for i in range(4):
                nny = ny + dy[i]
                nnx = nx + dx[i]

                if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                    continue

                elif field[nny][nnx] == "." and dist[nny][nnx] == -1:
                    dist[nny][nnx] = dist[ny][nx] + 1
                    que.append((nny, nnx))

                elif field[nny][nnx] == "#":
                    return dist[ny][nx] + 1

l = []

for i in range(H):
    for j in range(W):
        l.append(bsf(i, j))

print(max(l))

Fixed submission:
from collections import deque

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

#右上左下
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]


ans = 0

que = deque()

num_white = H * W

paint = 0

for i in range(H):
    for j in range(W):
        if field[i][j] == "#":
            que.append((i, j))
            num_white -= 1

def check(field):
    flag = False
    for i in range(H):
        if "." in field[i]:
            flag = True

    return flag

def one_bsf():
    global paint
    global que
    global ans

    for _ in range(len(que)):
        n = que.popleft()
        ny = n[0]
        nx = n[1]

        for i in range(4):
            nny = ny + dy[i]
            nnx = nx + dx[i]

            if nny < 0 or nny >= H or nnx < 0 or nnx >= W:
                continue

            elif field[nny][nnx] == ".":
                field[nny][nnx] = "#"
                paint += 1
                que.append((nny, nnx))

    ans += 1

while num_white != paint:
    one_bsf()
    
print(ans)