def main():

    from collections import deque

    import sys;input=sys.stdin.readline



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

    grid=[["#"]*(w+2)]+[list("#"+eval(input())+"#")for x in range(h)]+[["#"]*(w+2)]



    stack=deque()

    for y in range(1,h+1):

        for x in range(1,w+1):

            if grid[y][x]=="#":

                #grid[y][x]=0

                stack.append((y,x))



    for i in range(h*w):

        tmp=deque()

        while stack:

            my,mx=stack.pop()

            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):

                if grid[my+y][mx+x]==".":

                    #grid[my+y][mx+x]=i+1

                    grid[my+y][mx+x]="#"

                    tmp.append([my+y,mx+x])

        stack=tmp

        if not tmp:

            print(i)

            break

if __name__ == '__main__':

    main()
import functools

@functools.lru_cache(maxsize=None)



def main():

    from collections import deque

    import sys;input=sys.stdin.readline



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

    grid=[["#"]*(w+2)]+[list("#"+eval(input())+"#")for x in range(h)]+[["#"]*(w+2)]



    stack=deque()

    for y in range(1,h+1):

        for x in range(1,w+1):

            if grid[y][x]=="#":

                #grid[y][x]=0

                stack.append((y,x))



    for i in range(h*w):

        tmp=deque()

        while stack:

            my,mx=stack.pop()

            for y,x in ([0,1],[1,0],[-1,0],[0,-1]):

                if grid[my+y][mx+x]==".":

                    #grid[my+y][mx+x]=i+1

                    grid[my+y][mx+x]="#"

                    tmp.append([my+y,mx+x])

        stack=tmp

        if not tmp:

            print(i)

            break

if __name__ == '__main__':

    main()



def main():
    from collections import deque
    import sys

    input = sys.stdin.readline
    h, w = list(map(int, input().split()))
    grid = (
        [["#"] * (w + 2)]
        + [list("#" + eval(input()) + "#") for x in range(h)]
        + [["#"] * (w + 2)]
    )
    stack = deque()
    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if grid[y][x] == "#":
                # grid[y][x]=0
                stack.append((y, x))
    for i in range(h * w):
        tmp = deque()
        while stack:
            my, mx = stack.pop()
            for y, x in ([0, 1], [1, 0], [-1, 0], [0, -1]):
                if grid[my + y][mx + x] == ".":
                    # grid[my+y][mx+x]=i+1
                    grid[my + y][mx + x] = "#"
                    tmp.append([my + y, mx + x])
        stack = tmp
        if not tmp:
            print(i)
            break


if __name__ == "__main__":
    main()

import functools


@functools.lru_cache(maxsize=None)
def main():
    from collections import deque
    import sys

    input = sys.stdin.readline
    h, w = list(map(int, input().split()))
    grid = (
        [["#"] * (w + 2)]
        + [list("#" + eval(input()) + "#") for x in range(h)]
        + [["#"] * (w + 2)]
    )
    stack = deque()
    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if grid[y][x] == "#":
                # grid[y][x]=0
                stack.append((y, x))
    for i in range(h * w):
        tmp = deque()
        while stack:
            my, mx = stack.pop()
            for y, x in ([0, 1], [1, 0], [-1, 0], [0, -1]):
                if grid[my + y][mx + x] == ".":
                    # grid[my+y][mx+x]=i+1
                    grid[my + y][mx + x] = "#"
                    tmp.append([my + y, mx + x])
        stack = tmp
        if not tmp:
            print(i)
            break


if __name__ == "__main__":
    main()

+import functools + + +@functools.lru_cache(maxsize=None)