h, w = list(map(int, input().split()))



A = [str(eval(input())) for _ in range(h)]



from collections import deque

q = deque()

visit = [[-1]*w for _ in range(h)]



for i in range(h):

    for j in range(w):

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

            visit[i][j] = 0

            q.append((i, j))



while q:

    y, x = q.popleft()

    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):

        ny, nx = y+dy, x+dx

        if 0 <= ny <= h-1 and 0 <= nx <= w-1 and visit[ny][nx] == -1:

            visit[ny][nx] = visit[y][x]+1

            q.append((ny, nx))

#print(max(visit))

max_ = -1

for i in range(h):

    for j in range(w):

        max_ = max(visit[i][j], max_)

print(max_)
h, w = list(map(int, input().split()))

A = [list(str(eval(input()))) for _ in range(h)]



from collections import deque

q = deque([])

visit = [[-1]*w for _ in range(h)]

for i in range(h):

    for j in range(w):

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

            q.append((i, j))

            visit[i][j] = 0



while q:

    y, x = q.popleft()

    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):

        ny, nx = y+dy, x+dx

        if 0 <= ny < h and 0 <= nx < w:

            if visit[ny][nx] == -1:

                visit[ny][nx] = visit[y][x]+1

                q.append((ny, nx))

ans = 0

for i in range(h):

    for j in range(w):

        ans = max(ans, visit[i][j])

print(ans)

h, w = list(map(int, input().split()))
A = [str(eval(input())) for _ in range(h)]
from collections import deque

q = deque()
visit = [[-1] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if A[i][j] == "#":
            visit[i][j] = 0
            q.append((i, j))
while q:
    y, x = q.popleft()
    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
        ny, nx = y + dy, x + dx
        if 0 <= ny <= h - 1 and 0 <= nx <= w - 1 and visit[ny][nx] == -1:
            visit[ny][nx] = visit[y][x] + 1
            q.append((ny, nx))
# print(max(visit))
max_ = -1
for i in range(h):
    for j in range(w):
        max_ = max(visit[i][j], max_)
print(max_)

h, w = list(map(int, input().split()))
A = [list(str(eval(input()))) for _ in range(h)]
from collections import deque

q = deque([])
visit = [[-1] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if A[i][j] == "#":
            q.append((i, j))
            visit[i][j] = 0
while q:
    y, x = q.popleft()
    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
        ny, nx = y + dy, x + dx
        if 0 <= ny < h and 0 <= nx < w:
            if visit[ny][nx] == -1:
                visit[ny][nx] = visit[y][x] + 1
                q.append((ny, nx))
ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, visit[i][j])
print(ans)

-A = [str(eval(input())) for _ in range(h)] +A = [list(str(eval(input()))) for _ in range(h)] -q = deque() +q = deque([]) +            q.append((i, j)) -            q.append((i, j)) -        if 0 <= ny <= h - 1 and 0 <= nx <= w - 1 and visit[ny][nx] == -1: -            visit[ny][nx] = visit[y][x] + 1 -            q.append((ny, nx)) -# print(max(visit)) -max_ = -1 +        if 0 <= ny < h and 0 <= nx < w: +            if visit[ny][nx] == -1: +                visit[ny][nx] = visit[y][x] + 1 +                q.append((ny, nx)) +ans = 0 -        max_ = max(visit[i][j], max_) -print(max_) +        ans = max(ans, visit[i][j]) +print(ans)