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: Time Limit Exceeded)

-- AGC033 A problem
import Control.Monad

-- conversion from String to Int
toInt :: String -> Int
toInt x = read x :: Int

-- obtain [Int] from I/O
getInts :: IO [Int]
getInts = do
  x <- getLine
  let y = (map $ toInt) $ words x
  return y

-- mapping
sw :: Char -> Int
sw '.' = 1
sw '#' = 0

rowmap :: [Char] -> [Int]
rowmap [] = []
rowmap (x:xs) = [(sw x)] ++ (rowmap xs)

brdmap :: [[Char]] -> [[Int]]
brdmap [] = []
brdmap (x:xs) = [(rowmap x)] ++ (brdmap xs)

transpose :: [[a]] -> [[a]]
transpose ([]:_) = []
transpose xs = map head xs : transpose (map tail xs)

pilerow :: [Int] -> [Int] -> [Int]
pilerow [] [] = []
pilerow (x:xs) (y:ys) = [x*y] ++ (pilerow xs ys)

pilebrd :: [[Int]] -> [[Int]] -> [[Int]]
pilebrd [] [] = []
pilebrd (x:xs) (y:ys) = [(pilerow x y)] ++ (pilebrd xs ys)

dotrow :: Int -> [Int]
dotrow 0 = []
dotrow n = [1] ++ (dotrow (n-1))

shiftU :: [[Int]] -> Int -> [[Int]]
shiftU x n = (tail x) ++ [(dotrow n)]

shiftD :: [[Int]] -> Int -> [[Int]]
shiftD x n  = [(dotrow n)] ++ (init x)

shiftL :: [[Int]] -> Int -> [[Int]]
shiftL x n = transpose $ shiftU (transpose x) n

shiftR :: [[Int]] -> Int -> [[Int]]
shiftR x n = transpose $ shiftD (transpose x) n

turnbrd :: [[Int]] -> Int -> Int -> [[Int]]
turnbrd brd h w =
    pilebrd brd (pilebrd (shiftU brd w) (pilebrd (shiftD brd w) (pilebrd (shiftL brd h) (shiftR brd h))))

checkrow :: [Int] -> Int
checkrow [] = 0
checkrow (x:xs) = x + (checkrow xs)

checkbrd :: [[Int]] -> Int
checkbrd [] = 0
checkbrd (x:xs) = (checkrow x) + (checkbrd xs)

agc033a :: [[Int]] -> Int -> Int -> Int
agc033a brd h w =
    if checkbrd brd > 0 then 1 + (agc033a (turnbrd brd h w) h w) else 0
    
-- main function
main :: IO ()
main = do
  hnw <- getInts
  let h = head hnw :: Int
  let w = last hnw :: Int
  rawbrd <- replicateM h getLine
  let brd = brdmap rawbrd
  print (agc033a brd h w)