from collections import deque import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) DX = (-1, 0, 1, 0, -1, -1, 1, 1) DY = (0, 1, 0, -1, -1, 1, -1, 1) R = 1500 H, W = map(int, input().split()) G = [input().rstrip() for _ in range(H)] dist = [[R]*W for _ in range(H)] X = deque() Y = deque() D = deque() for i in range(H): for j in range(W): if G[i][j] == ".": dist[i][j] = 0 X.appendleft(i) Y.appendleft(j) D.appendleft(0) elif i == 0 or i == H - 1 or j == 0 or j == W - 1: dist[i][j] = 1 X.append(i) Y.append(j) D.append(1) while X: x = X.popleft() y = Y.popleft() now = D.popleft() for dx, dy in zip(DX, DY): nx = x + dx ny = y + dy if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] > now + 1: dist[nx][ny] = now + 1 X.append(nx) Y.append(ny) D.append(now + 1) print(now)