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

A = [list(str(eval(input()))) for i in range(H)]

 

black = []

 

cnt = 0

 

for i in range(H):

    for j in range(W):

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

            black.append([i,j])

 

length = len(black)

            

while (length < H*W):

    gray = []

    for l in black:

        if l[1] != 0:

            c1 = (l[0],l[1]-1)

            if A[c1[0]][c1[1]] == ".":

                gray.append(c1)

                A[c1[0]][c1[1]] = "#"

        if l[1] != W-1:

            c2 = (l[0],l[1]+1)

            if  A[c2[0]][c2[1]] == ".":

                gray.append(c2)

                A[c2[0]][c2[1]] = "#"

        if l[0] != 0:

            c3 = (l[0]-1,l[1])

            if  A[c3[0]][c3[1]] == ".":

                gray.append(c3)

                A[c3[0]][c3[1]] = "#"

        if l[0] != H-1:

            c4 = (l[0]+1,l[1])

            if  A[c4[0]][c4[1]] == ".":

                gray.append(c4)

                A[c4[0]][c4[1]] = "#"

    

    gray = list(set(gray))

    black = [l for l in gray]

    

    cnt += 1

    length += len(black)

    

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

A = [list(str(eval(input()))) for i in range(H)]

 

black = []

 

cnt = 0

 

for i in range(H):

    for j in range(W):

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

            black.append([i,j])

 

length = len(black)

            

while (length < H*W):

    gray = []

    for l in black:

        if l[1] != 0:

            c1 = [l[0],l[1]-1]

            if A[c1[0]][c1[1]] == ".":

                gray.append(c1)

                A[c1[0]][c1[1]] = "#"

        if l[1] != W-1:

            c2 = [l[0],l[1]+1]

            if  A[c2[0]][c2[1]] == ".":

                gray.append(c2)

                A[c2[0]][c2[1]] = "#"

        if l[0] != 0:

            c3 = [l[0]-1,l[1]]

            if  A[c3[0]][c3[1]] == ".":

                gray.append(c3)

                A[c3[0]][c3[1]] = "#"

        if l[0] != H-1:

            c4 = [l[0]+1,l[1]]

            if  A[c4[0]][c4[1]] == ".":

                gray.append(c4)

                A[c4[0]][c4[1]] = "#"

    

    black = [l for l in gray]

    

    cnt += 1

    length += len(black)

    

print(cnt)
H, W = list(map(int, input().split()))
A = [list(str(eval(input()))) for i in range(H)]
black = []
cnt = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            black.append([i, j])
length = len(black)
while length < H * W:
    gray = []
    for l in black:
        if l[1] != 0:
            c1 = (l[0], l[1] - 1)
            if A[c1[0]][c1[1]] == ".":
                gray.append(c1)
                A[c1[0]][c1[1]] = "#"
        if l[1] != W - 1:
            c2 = (l[0], l[1] + 1)
            if A[c2[0]][c2[1]] == ".":
                gray.append(c2)
                A[c2[0]][c2[1]] = "#"
        if l[0] != 0:
            c3 = (l[0] - 1, l[1])
            if A[c3[0]][c3[1]] == ".":
                gray.append(c3)
                A[c3[0]][c3[1]] = "#"
        if l[0] != H - 1:
            c4 = (l[0] + 1, l[1])
            if A[c4[0]][c4[1]] == ".":
                gray.append(c4)
                A[c4[0]][c4[1]] = "#"
    gray = list(set(gray))
    black = [l for l in gray]
    cnt += 1
    length += len(black)
print(cnt)

H, W = list(map(int, input().split()))
A = [list(str(eval(input()))) for i in range(H)]
black = []
cnt = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            black.append([i, j])
length = len(black)
while length < H * W:
    gray = []
    for l in black:
        if l[1] != 0:
            c1 = [l[0], l[1] - 1]
            if A[c1[0]][c1[1]] == ".":
                gray.append(c1)
                A[c1[0]][c1[1]] = "#"
        if l[1] != W - 1:
            c2 = [l[0], l[1] + 1]
            if A[c2[0]][c2[1]] == ".":
                gray.append(c2)
                A[c2[0]][c2[1]] = "#"
        if l[0] != 0:
            c3 = [l[0] - 1, l[1]]
            if A[c3[0]][c3[1]] == ".":
                gray.append(c3)
                A[c3[0]][c3[1]] = "#"
        if l[0] != H - 1:
            c4 = [l[0] + 1, l[1]]
            if A[c4[0]][c4[1]] == ".":
                gray.append(c4)
                A[c4[0]][c4[1]] = "#"
    black = [l for l in gray]
    cnt += 1
    length += len(black)
print(cnt)

-            c1 = (l[0], l[1] - 1) +            c1 = [l[0], l[1] - 1] -            c2 = (l[0], l[1] + 1) +            c2 = [l[0], l[1] + 1] -            c3 = (l[0] - 1, l[1]) +            c3 = [l[0] - 1, l[1]] -            c4 = (l[0] + 1, l[1]) +            c4 = [l[0] + 1, l[1]] -    gray = list(set(gray))