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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MagicHash             #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf            #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE ViewPatterns          #-}

import           Control.Applicative
import           Control.Exception
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bool
import qualified Data.ByteString             as B
import qualified Data.ByteString.Char8       as C
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import qualified Data.Foldable               as F
import           Data.Function
import qualified Data.IntMap.Strict          as IM
import qualified Data.IntSet                 as IS
import qualified Data.List                   as L
import qualified Data.Map.Strict             as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive.ByteArray
import           Data.Primitive.MutVar
import qualified Data.Primitive.Types        as P

import qualified Data.Set                    as S
import           Data.Tuple
import qualified Data.Vector                 as V
import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM
import           Debug.Trace
import           Foreign                     hiding (void)
import           GHC.Exts
import           Unsafe.Coerce

main :: IO ()
main = do
    [h, w] <- map read.words <$> getLine :: IO [Int]
    m <- U.filter (not.isSpace) . U.unfoldrN (h*w+h) C.uncons <$> C.getContents
    print $ solve h w m

convert :: Char -> Int
convert = (inf *) . fromEnum . (/= '#')

inf :: Int
inf = 0x3f3f3f3f

solve :: Int -> Int -> U.Vector Char -> Int
solve h w (U.map convert -> m) = runST $ do
    q <- newQueueM
    U.forM_ (U.findIndices (== 0) m) $ \xy -> do
        enqueueM xy q

    d <- U.unsafeThaw m

    fix $ \loop -> do
        dequeueM q >>= \case
            Just xy -> do
                forNeighborM xy $ \nxny -> do
                    dnxny <- UM.unsafeRead d nxny
                    when (dnxny == inf) $ do
                        UM.unsafeRead d xy >>= UM.unsafeWrite d nxny . (+1)
                        enqueueM nxny q
                loop
            Nothing -> do
                h <- readByteArray (queueInfo q) 0
                readByteArray (queueData q) (h - 1) >>= UM.unsafeRead d

  where
    forNeighborM :: (PrimMonad m) => Int -> (Int -> m ()) -> m ()
    forNeighborM xy f = do
        let y = rem xy w
        when (y > 0) $ f (xy - 1)
        when (y + 1 < w) $ f (xy + 1)
        when (xy >= w) $ f (xy - w)
        when (xy + w < h * w) $ f (xy + w)
    {-# INLINE forNeighborM #-}

-------------------------------------------------------------------------------
data VecQueue m a = VecQueue
    { queueInfo :: !(MutableByteArray m)
    , queueData :: !(MutableByteArray m)
    }

newQueueM :: (PrimMonad m, P.Prim a) => m (VecQueue (PrimState m) a)
newQueueM = do
    info <- newByteArray (2 * 8)
    writeByteArray info 0 (0 :: Int)
    writeByteArray info 1 (0 :: Int)
    q <- newByteArray (1024 * 1024 * 8)
    return $ VecQueue info q

dequeueM :: (PrimMonad m, P.Prim a) => VecQueue (PrimState m) a -> m (Maybe a)
dequeueM (VecQueue info q) = do
    h <- readByteArray info 0
    t <- readByteArray info 1
    if h < t
    then do
        writeByteArray info 0 (h + 1 :: Int)
        pure <$> readByteArray q h
    else return Nothing
{-# INLINE dequeueM #-}

enqueueM :: (PrimMonad m, P.Prim a) => a -> VecQueue (PrimState m) a -> m ()
enqueueM x (VecQueue info q) = do
    t <- readByteArray info 1
    writeByteArray q t x
    writeByteArray info 1 (t + 1 :: Int)
{-# INLINE enqueueM #-}

type Parser a = C.ByteString -> Maybe (a, C.ByteString)

parseInt :: Parser Int
parseInt = C.readInt . C.dropWhile isSpace

parseInt2 :: Parser (Int, Int)
parseInt2 = runStateT $
    (,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt3 :: Parser (Int, Int, Int)
parseInt3 = runStateT $
    (,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)

parseInt4 :: Parser (Int, Int, Int, Int)
parseInt4 = runStateT $
    (,,,) <$> StateT (C.readInt . C.dropWhile isSpace)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)
        <*> StateT (C.readInt . B.unsafeTail)