from collections import deque



def main():

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



    A = [[a for a in eval(input())] for _ in range(H)]

    #stack = deque([(i, j) for i in range(H) for j in range(W) if A[i][j]=="#"])

    stack = [(i, j) for i in range(H) for j in range(W) if A[i][j]=="#"]

    ans = 0

    flag = False

    if stack:

        flag = True

    while flag:

        ans += 1

        flag = False

        #new_stack = deque()

        new_stack = []

        for a,b in stack:

        #while stack:

            #a,b = stack.pop() # 取り出す

            # A[a][b]の隣接マスについて調べる

             # 上

            if a != 0 and A[a-1][b] == ".": # 白の場合

                    A[a-1][b] = "#"

                    new_stack.append((a-1,b))

             # 下

            if a != H-1 and A[a+1][b] == ".": # 白の場合

                    A[a+1][b] = "#"

                    new_stack.append((a+1,b))

            # 左

            if b != 0 and A[a][b-1] == ".": # 白の場合

                    A[a][b-1] = "#"

                    new_stack.append((a,b-1))

            # 右

            if b != W-1 and A[a][b+1] == ".": # 白の場合

                    A[a][b+1] = "#"

                    new_stack.append((a,b+1))

        stack = new_stack

        if stack:

            flag = True

        else:

            ans -= 1 



    print(ans)

            





if __name__ == "__main__":

    main()





from collections import deque





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

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



q = deque()

for i in range(H):

    for j in range(W):

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

            q.append((i,j))





ans = 0

dyx = [(1,0), (0,1), (-1,0), (0,-1)]

while q:

    flag = False

    for _ in range(len(q)):

        y,x = q.popleft()

        for dy, dx in dyx:

            ny, nx = y+dy, x+dx

            if not 0 <= ny <= H-1 or not 0 <= nx <= W-1:

                continue

            if A[ny][nx] == ".":

                q.append((ny, nx))

                A[ny][nx] = "#"

                flag = True

    if flag:

        ans += 1



print(ans)





from collections import deque


def main():
    H, W = list(map(int, input().split()))
    A = [[a for a in eval(input())] for _ in range(H)]
    # stack = deque([(i, j) for i in range(H) for j in range(W) if A[i][j]=="#"])
    stack = [(i, j) for i in range(H) for j in range(W) if A[i][j] == "#"]
    ans = 0
    flag = False
    if stack:
        flag = True
    while flag:
        ans += 1
        flag = False
        # new_stack = deque()
        new_stack = []
        for a, b in stack:
            # while stack:
            # a,b = stack.pop() # 取り出す
            # A[a][b]の隣接マスについて調べる
            # 上
            if a != 0 and A[a - 1][b] == ".":  # 白の場合
                A[a - 1][b] = "#"
                new_stack.append((a - 1, b))
            # 下
            if a != H - 1 and A[a + 1][b] == ".":  # 白の場合
                A[a + 1][b] = "#"
                new_stack.append((a + 1, b))
            # 左
            if b != 0 and A[a][b - 1] == ".":  # 白の場合
                A[a][b - 1] = "#"
                new_stack.append((a, b - 1))
            # 右
            if b != W - 1 and A[a][b + 1] == ".":  # 白の場合
                A[a][b + 1] = "#"
                new_stack.append((a, b + 1))
        stack = new_stack
        if stack:
            flag = True
        else:
            ans -= 1
    print(ans)


if __name__ == "__main__":
    main()

from collections import deque

H, W = list(map(int, input().split()))
A = [[str(i) for i in list(eval(input()))] for _ in range(H)]
q = deque()
for i in range(H):
    for j in range(W):
        if A[i][j] == "#":
            q.append((i, j))
ans = 0
dyx = [(1, 0), (0, 1), (-1, 0), (0, -1)]
while q:
    flag = False
    for _ in range(len(q)):
        y, x = q.popleft()
        for dy, dx in dyx:
            ny, nx = y + dy, x + dx
            if not 0 <= ny <= H - 1 or not 0 <= nx <= W - 1:
                continue
            if A[ny][nx] == ".":
                q.append((ny, nx))
                A[ny][nx] = "#"
                flag = True
    if flag:
        ans += 1
print(ans)

- -def main(): -    H, W = list(map(int, input().split())) -    A = [[a for a in eval(input())] for _ in range(H)] -    # stack = deque([(i, j) for i in range(H) for j in range(W) if A[i][j]=="#"]) -    stack = [(i, j) for i in range(H) for j in range(W) if A[i][j] == "#"] -    ans = 0 +H, W = list(map(int, input().split())) +A = [[str(i) for i in list(eval(input()))] for _ in range(H)] +q = deque() +for i in range(H): +    for j in range(W): +        if A[i][j] == "#": +            q.append((i, j)) +ans = 0 +dyx = [(1, 0), (0, 1), (-1, 0), (0, -1)] +while q: -    if stack: -        flag = True -    while flag: +    for _ in range(len(q)): +        y, x = q.popleft() +        for dy, dx in dyx: +            ny, nx = y + dy, x + dx +            if not 0 <= ny <= H - 1 or not 0 <= nx <= W - 1: +                continue +            if A[ny][nx] == ".": +                q.append((ny, nx)) +                A[ny][nx] = "#" +                flag = True +    if flag: -        flag = False -        # new_stack = deque() -        new_stack = [] -        for a, b in stack: -            # while stack: -            # a,b = stack.pop() # 取り出す -            # A[a][b]の隣接マスについて調べる -            # 上 -            if a != 0 and A[a - 1][b] == ".":  # 白の場合 -                A[a - 1][b] = "#" -                new_stack.append((a - 1, b)) -            # 下 -            if a != H - 1 and A[a + 1][b] == ".":  # 白の場合 -                A[a + 1][b] = "#" -                new_stack.append((a + 1, b)) -            # 左 -            if b != 0 and A[a][b - 1] == ".":  # 白の場合 -                A[a][b - 1] = "#" -                new_stack.append((a, b - 1)) -            # 右 -            if b != W - 1 and A[a][b + 1] == ".":  # 白の場合 -                A[a][b + 1] = "#" -                new_stack.append((a, b + 1)) -        stack = new_stack -        if stack: -            flag = True -        else: -            ans -= 1 -    print(ans) - - -if __name__ == "__main__": -    main() +print(ans)