import sys from collections import deque def main(): H, W = map(int, sys.stdin.readline().split()) grid = [sys.stdin.readline().strip() for _ in range(H)] INF = float('inf') distance = [[INF] * W for _ in range(H)] q = deque() # Initialize queue with all sea cells for i in range(H): for j in range(W): if grid[i][j] == '.': distance[i][j] = 0 q.append((i, j)) # Directions for 8-way movement (Chebyshev neighbors) dirs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] # Multi-source BFS to compute minimum Chebyshev distance from any sea while q: i, j = q.popleft() for di, dj in dirs: ni, nj = i + di, j + dj if 0 <= ni < H and 0 <= nj < W: if distance[ni][nj] == INF: distance[ni][nj] = distance[i][j] + 1 q.append((ni, nj)) max_dist = 0 for i in range(H): for j in range(W): if grid[i][j] == '#': # Calculate the shortest distance to the virtual sea outside the map external = min(i + 1, H - i, j + 1, W - j) current = min(distance[i][j], external) if current > max_dist: max_dist = current print(max_dist) if __name__ == "__main__": main()