from collections import deque def solve(H, W, M): dmap = [[-1] * H for _ in range(0, W)] candidates = deque([]) for h in range(0, H): for w in range(0, W): if M[h][w] == ".": dmap[h][w] = 0 candidates.append((0, h, w)) elif (h == 0 or h == H - 1 or w == 0 or w == W - 1) and M[h][w] == "#": dmap[h][w] = 1 candidates.appendleft((1, h, w)) max = 1 count = 0 while(len(candidates)): count += 1 can = candidates.pop() d = can[0] h = can[1] w = can[2] for (xi, yi) in [(h-1, w-1),(h-1, w), (h-1, w+1), (h , w - 1), (h, w + 1), (h + 1, w - 1), (h + 1, w), (h + 1, w + 1)]: if xi > 0 and yi > 0 and xi < H and yi < W: if dmap[xi][yi] == -1: dmap[xi][yi] = d + 1 max = d + 1 if (d + 1, xi, yi) not in candidates: candidates.appendleft((d + 1, xi, yi)) print(f"looped {count} times") return max if __name__ == "__main__": H, W = tuple([int(c) for c in input().split(" ")]) print(solve(H, W, [input() for _ in range(0, H)]))