import sys

input = sys.stdin.readline



H, W = list(map(int, input().split()))

A = [input().rstrip() for _ in range(H)]

dp = [[float('inf') if A[i][j] == '.' else 0 for j in range(W)] for i in range(H)]

for i in range(H):

    for j in range(W):

        if i > 0:

            dp[i][j] = min(dp[i][j], dp[i-1][j] + 1)

        if j > 0:

            dp[i][j] = min(dp[i][j], dp[i][j-1] + 1)

for i in range(H)[::-1]:

    for j in range(W)[::-1]:

        if i < H-1:

            dp[i][j] = min(dp[i][j], dp[i+1][j] + 1)

        if j < W-1:

            dp[i][j] = min(dp[i][j], dp[i][j+1] + 1)

for i in range(H):

    for j in range(W)[::-1]:

        if i > 0:

            dp[i][j] = min(dp[i][j], dp[i-1][j] + 1)

        if j < W-1:

            dp[i][j] = min(dp[i][j], dp[i][j+1] + 1)

for i in range(H)[::-1]:

    for j in range(W):

        if i < H-1:

            dp[i][j] = min(dp[i][j], dp[i+1][j] + 1)

        if j > 0:

            dp[i][j] = min(dp[i][j], dp[i][j-1] + 1)

print((max(max(row) for row in dp)))

from collections import deque

import sys

input = sys.stdin.readline



H, W = list(map(int, input().split()))

A = [input().rstrip() for _ in range(H)]

dist = [[-1] * W for _ in range(H)]

queue = deque()

for i in range(H):

    for j in range(W):

        if A[i][j] == '#':

            dist[i][j] = 0

            queue.append((i, j))

while queue:

    i, j = queue.popleft()

    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:

        ni, nj = i+di, j+dj

        if 0 <= ni < H and 0 <= nj < W and dist[ni][nj] == -1:

            dist[ni][nj] = dist[i][j] + 1

            queue.append((ni, nj))

print((max(max(row) for row in dist)))
import sys

input = sys.stdin.readline
H, W = list(map(int, input().split()))
A = [input().rstrip() for _ in range(H)]
dp = [[float("inf") if A[i][j] == "." else 0 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if i > 0:
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1)
        if j > 0:
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1)
for i in range(H)[::-1]:
    for j in range(W)[::-1]:
        if i < H - 1:
            dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1)
        if j < W - 1:
            dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1)
for i in range(H):
    for j in range(W)[::-1]:
        if i > 0:
            dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1)
        if j < W - 1:
            dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1)
for i in range(H)[::-1]:
    for j in range(W):
        if i < H - 1:
            dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1)
        if j > 0:
            dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1)
print((max(max(row) for row in dp)))

from collections import deque
import sys

input = sys.stdin.readline
H, W = list(map(int, input().split()))
A = [input().rstrip() for _ in range(H)]
dist = [[-1] * W for _ in range(H)]
queue = deque()
for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            dist[i][j] = 0
            queue.append((i, j))
while queue:
    i, j = queue.popleft()
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and dist[ni][nj] == -1:
            dist[ni][nj] = dist[i][j] + 1
            queue.append((ni, nj))
print((max(max(row) for row in dist)))

+from collections import deque -dp = [[float("inf") if A[i][j] == "." else 0 for j in range(W)] for i in range(H)] +dist = [[-1] * W for _ in range(H)] +queue = deque() -        if i > 0: -            dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1) -        if j > 0: -            dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1) -for i in range(H)[::-1]: -    for j in range(W)[::-1]: -        if i < H - 1: -            dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1) -        if j < W - 1: -            dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1) -for i in range(H): -    for j in range(W)[::-1]: -        if i > 0: -            dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1) -        if j < W - 1: -            dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1) -for i in range(H)[::-1]: -    for j in range(W): -        if i < H - 1: -            dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1) -        if j > 0: -            dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1) -print((max(max(row) for row in dp))) +        if A[i][j] == "#": +            dist[i][j] = 0 +            queue.append((i, j)) +while queue: +    i, j = queue.popleft() +    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]: +        ni, nj = i + di, j + dj +        if 0 <= ni < H and 0 <= nj < W and dist[ni][nj] == -1: +            dist[ni][nj] = dist[i][j] + 1 +            queue.append((ni, nj)) +print((max(max(row) for row in dist)))