Problem:
Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3

Solution in Fortran (gfortran v4.8.4) (judge verdict: Accepted)

module mod_queue
  implicit none

  type t_node
    private
    integer :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type t_node

  type t_queue
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: enqueue => enqueue
    procedure :: dequeue => dequeue
    procedure :: peek => peek
    procedure :: clear => clear
    procedure :: size => size_of
  end type t_queue

contains


  function new_node(item) result(node)
    integer, intent(in) :: item
    type(t_node), pointer :: node

    allocate(node)
    node%item = item
  end

  subroutine enqueue(this,item)
    class(t_queue), intent(inout) :: this
    integer, intent(in) :: item
    type(t_node), pointer :: node

    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end

  function dequeue(this) result(item)
    class(t_queue), intent(inout) :: this
    integer :: item
    type(t_node), pointer :: node

    item = this%head%item
    node => this%head%next
    deallocate(this%head)
    this%head => node
    if (associated(node)) then
      node%prev => null()
    else
      this%tail => null()
    end if
    this%num = this%num-1
  end

  function peek(this) result(item)
    class(t_queue), intent(in) :: this
    integer :: item

    item = this%head%item
  end

  subroutine clear(this)
    class(t_queue), intent(inout) :: this
    type(t_node), pointer :: node, next

    if (.not.associated(this%head)) return
    node => this%head
    do while (associated(node%next))
      next => node%next
      deallocate(node)
      node => next
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end

  integer function size_of(this)
    class(t_queue), intent(in) :: this

    size_of = this%num
  end

end module mod_queue

module maze
integer,allocatable,dimension(:,:)::mazemap
integer::dirx(4)=[1,0,-1,0]
integer::diry(4)=[0,1,0,-1]

integer ans

contains

subroutine getmap(mazemap,R,C)
  integer,allocatable,dimension(:,:)::mazemap
  integer R,C
  character(1000) linereader
  allocate(mazemap(0:R+1,0:C+1))
  mazemap=0
  do i=1,R
    read*,linereader
    do j=1,C
      if(linereader(j:j)==".")mazemap(i,j)=-1
    end do
  end do
end subroutine
end module maze
use mod_queue
use maze
integer H,W
type(t_queue) :: pq(2)
read*,H,W
call getmap(mazemap,H,W)
do i=1,H
  do j=1,W
    if(mazemap(i,j)==0)then
      do k=1,4
        if(mazemap(i+dirx(k),j+diry(k) )==-1)then
          mazemap(i+dirx(k),j+diry(k) )=1
          call enqueue(pq(1),i+dirx(k))
          call enqueue(pq(2),j+diry(k))
        endif
      end do
    endif
  end do
end do

ans=0
do while(size_of(pq(1))/=0)
  itemsize=size_of(pq(1))
  do ii=1,itemsize
    i=dequeue(pq(1))
    j=dequeue(pq(2))
    do k=1,4
      if(mazemap(i+dirx(k),j+diry(k) )==-1)then
        mazemap(i+dirx(k),j+diry(k) )=mazemap(i,j)+1
        ans=max(ans,mazemap(i,j)+1)
        call enqueue(pq(1),i+dirx(k))
        call enqueue(pq(2),j+diry(k))
      endif
    end do
  end do
end do

print"(I0)",ans

end