from collections import deque

import copy



def main():

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

  field = [[i=='#' for i in eval(input())] for _ in range(H)]

  count = 0

  q = deque([(i, j) for i in range(H) for j in range(W) if field[i][j]])

  while True:

    nq = deque()

    while q:

      y, x = q.popleft()

      if x > 0 and not field[y][x-1]:

        field[y][x-1] = True

        nq.append((y,x-1))

      if y > 0 and not field[y-1][x]:

        field[y-1][x] = True

        nq.append((y-1,x))

      if x<W-1 and not field[y][x+1]:

        field[y][x+1] = True

        nq.append((y,x+1))

      if y<H-1 and not field[y+1][x]:

        field[y+1][x] = True

        nq.append((y+1, x))

    if nq:

      q = copy.copy(nq)

    else:

      break

    count += 1

  print(count)

      

if __name__ == "__main__":

  main()
from collections import deque



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

field = [[i=='#' for i in eval(input())] for _ in range(H)]

count = 0



def bfs():

  global count

  q = deque()

  for y in range(H):

    for x in range(W):

      if field[y][x]:

        q.append((y, x))

  while True:

    nq = deque()

    while len(q) > 0:

      y, x = q.popleft()

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

        ny, nx = y + dy, x + dx

        if ny < 0 or ny >= H or nx < 0 or nx >= W:

          continue

        if field[ny][nx]:

          continue

        field[ny][nx] = True

        nq.append((ny, nx))

    if len(nq) > 0:

      q = nq

    else:

      break

    count += 1

      

bfs()

print(count)
from collections import deque
import copy


def main():
    H, W = list(map(int, input().split()))
    field = [[i == "#" for i in eval(input())] for _ in range(H)]
    count = 0
    q = deque([(i, j) for i in range(H) for j in range(W) if field[i][j]])
    while True:
        nq = deque()
        while q:
            y, x = q.popleft()
            if x > 0 and not field[y][x - 1]:
                field[y][x - 1] = True
                nq.append((y, x - 1))
            if y > 0 and not field[y - 1][x]:
                field[y - 1][x] = True
                nq.append((y - 1, x))
            if x < W - 1 and not field[y][x + 1]:
                field[y][x + 1] = True
                nq.append((y, x + 1))
            if y < H - 1 and not field[y + 1][x]:
                field[y + 1][x] = True
                nq.append((y + 1, x))
        if nq:
            q = copy.copy(nq)
        else:
            break
        count += 1
    print(count)


if __name__ == "__main__":
    main()

from collections import deque

H, W = list(map(int, input().split()))
field = [[i == "#" for i in eval(input())] for _ in range(H)]
count = 0


def bfs():
    global count
    q = deque()
    for y in range(H):
        for x in range(W):
            if field[y][x]:
                q.append((y, x))
    while True:
        nq = deque()
        while len(q) > 0:
            y, x = q.popleft()
            for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
                ny, nx = y + dy, x + dx
                if ny < 0 or ny >= H or nx < 0 or nx >= W:
                    continue
                if field[ny][nx]:
                    continue
                field[ny][nx] = True
                nq.append((ny, nx))
        if len(nq) > 0:
            q = nq
        else:
            break
        count += 1


bfs()
print(count)

-import copy + +H, W = list(map(int, input().split())) +field = [[i == "#" for i in eval(input())] for _ in range(H)] +count = 0 -def main(): -    H, W = list(map(int, input().split())) -    field = [[i == "#" for i in eval(input())] for _ in range(H)] -    count = 0 -    q = deque([(i, j) for i in range(H) for j in range(W) if field[i][j]]) +def bfs(): +    global count +    q = deque() +    for y in range(H): +        for x in range(W): +            if field[y][x]: +                q.append((y, x)) -        while q: +        while len(q) > 0: -            if x > 0 and not field[y][x - 1]: -                field[y][x - 1] = True -                nq.append((y, x - 1)) -            if y > 0 and not field[y - 1][x]: -                field[y - 1][x] = True -                nq.append((y - 1, x)) -            if x < W - 1 and not field[y][x + 1]: -                field[y][x + 1] = True -                nq.append((y, x + 1)) -            if y < H - 1 and not field[y + 1][x]: -                field[y + 1][x] = True -                nq.append((y + 1, x)) -        if nq: -            q = copy.copy(nq) +            for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)): +                ny, nx = y + dy, x + dx +                if ny < 0 or ny >= H or nx < 0 or nx >= W: +                    continue +                if field[ny][nx]: +                    continue +                field[ny][nx] = True +                nq.append((ny, nx)) +        if len(nq) > 0: +            q = nq -    print(count) -if __name__ == "__main__": -    main() +bfs() +print(count)