Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in read() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def solve(H, W, grid):

    ans = 0
    while np.sum(grid) != 0:
        next_grid = grid.copy()
        for y in range(H):
            for x in range(W):
                if not grid[x + y * W]:
                    continue
                for i in range(4):
                    if i == 0:
                        nx, ny = x, y + 1
                    elif i == 1:
                        nx, ny = x, y - 1
                    elif i == 2:
                        nx, ny = x + 1, y
                    elif i == 3:
                        nx, ny = x - 1, y
                    if 0 <= nx < W and 0 <= ny < H:
                        if not grid[nx + ny * W]:
                            next_grid[x + y * W] = False
                            break
        ans += 1
        grid = next_grid
    return ans


def main():

    H, W = in_nn()
    grid = np.array(in_map(), np.int8)

    print(solve(H, W, grid))


# AOTコンパイル
def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', '(i4,i4,i1[:])')(solve)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        exit(0)
    from my_module import solve
    main()


Fixed submission:
import sys
from collections import deque
from copy import copy

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def bfs_grid(H, W, s, grid):

    q = deque(s)
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    ans = 0

    while True:

        nq = deque()
        f = False
        while q:
            now = q.popleft()
            x, y = now
            for m in move:
                nx, ny = x + m[0], y + m[1]
                if 0 <= nx < W and 0 <= ny < H:
                    if grid[ny][nx]:
                        f = True
                        grid[ny][nx] = False
                        nq.append((nx, ny))

        if f:
            ans += 1
        if nq:
            q = nq
        else:
            break

    return ans


def main():

    H, W = in_nn()
    grid = in_map2(H)

    s = []
    for y in range(H):
        for x in range(W):
            if not grid[y][x]:
                s.append((x, y))

    if len(s) == H * W:
        print(0)
        exit()

    print(bfs_grid(H, W, s, grid))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in read() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    H, W = in_nn()
    grid = in_map()

    ans = 0
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while sum(grid) != 0:

        next_grid = copy.copy(grid)
        for y in range(H):
            for x in range(W):
                if not grid[x + y * W]:
                    continue
                for (px, py) in move:
                    nx, ny = x + px, y + py
                    if 0 <= nx < W and 0 <= ny < H:
                        if not grid[nx + ny * W]:
                            next_grid[x + y * W] = False
                            break
        ans += 1
        grid = next_grid

    # print(grid)
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
from copy import copy

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def bfs_grid(H, W, s, grid):

    q = deque(s)
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    ans = 0

    while True:

        nq = deque()
        f = False
        while q:
            now = q.popleft()
            x, y = now
            for m in move:
                nx, ny = x + m[0], y + m[1]
                if 0 <= nx < W and 0 <= ny < H:
                    if grid[ny][nx]:
                        f = True
                        grid[ny][nx] = False
                        nq.append((nx, ny))

        if f:
            ans += 1
        if nq:
            q = nq
        else:
            break

    return ans


def main():

    H, W = in_nn()
    grid = in_map2(H)

    s = []
    for y in range(H):
        for x in range(W):
            if not grid[y][x]:
                s.append((x, y))

    if len(s) == H * W:
        print(0)
        exit()

    print(bfs_grid(H, W, s, grid))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in read() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    H, W = in_nn()
    grid = in_map()

    ans = 0
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while sum(grid) != 0:

        next_grid = copy.copy(grid)
        for y in range(H):
            for x in range(W):
                if not grid[x + y * W]:
                    continue
                for (px, py) in move:
                    nx, ny = x + px, y + py
                    if 0 <= nx < W and 0 <= ny < H:
                        if not grid[nx + ny * W]:
                            next_grid[x + y * W] = False
                            break
        ans += 1
        grid = next_grid

    # print(grid)
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
from copy import copy

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def bfs_grid(H, W, s, grid):

    q = deque(s)
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    ans = 0

    while True:

        nq = deque()
        f = False
        while q:
            now = q.popleft()
            x, y = now
            for m in move:
                nx, ny = x + m[0], y + m[1]
                if 0 <= nx < W and 0 <= ny < H:
                    if grid[ny][nx]:
                        f = True
                        grid[ny][nx] = False
                        nq.append((nx, ny))

        if f:
            ans += 1
        if nq:
            q = nq
        else:
            break

    return ans


def main():

    H, W = in_nn()
    grid = in_map2(H)

    s = []
    for y in range(H):
        for x in range(W):
            if not grid[y][x]:
                s.append((x, y))

    if len(s) == H * W:
        print(0)
        exit()

    print(bfs_grid(H, W, s, grid))


if __name__ == '__main__':
    main()