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 Kotlin (1.0.0) (judge verdict: Time Limit Exceeded)

import java.util.*

fun main(args: Array<String>) {
    val (H, W) = readIntList()
    val map = Array(H) { readString() }

    val dist = BFS(map.mapSearch('#').map { it.roundList() }.flatten(), map, {false})
    println(dist.dist)
}

fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)
fun powInt(a: Int, b: Int) = Math.pow(a.toDouble(), b.toDouble()).toInt()


data class WH(val W:Int, val H:Int)

val DIR_4 = listOf(
    -1 to 0,
    1 to 0,
    0 to -1,
    0 to 1
)

fun Pair<Int,Int>.roundList(dir: List<Pair<Int, Int>> = DIR_4) = dir.map { it.first+first to it.second+second }

fun Array<String>.mapSearch(ch: Char): List<Pair<Int, Int>> {
    val res = mutableListOf<Pair<Int, Int>>()
    for ((h, line) in this.withIndex())
        for ((w, c) in line.withIndex())
            if (ch == c) res.add(h to w)
    return res
}

data class BFSResult(val dist:Int, val hitEnd:Boolean)


fun BFS(
    startList: List<Pair<Int, Int>>,
    map: Array<String>,
    isEnd: (Pair<Int, Int>) -> Boolean,
    wall:Char = '#',
    dir:List<Pair<Int, Int>> = DIR_4
):BFSResult {
    val set = mutableSetOf<Pair<Int, Int>>()
    val que = ArrayDeque<Pair<Int, Int>>()
    que.addAll(startList)
    var res = -1

    while (que.isNotEmpty()) {
        res++
        val tmpQue = ArrayDeque<Pair<Int, Int>>()
        while (que.isNotEmpty()) {
            val hw = que.pop()
            val (h, w) = hw
            if (h !in map.indices || w !in map[0].indices) continue
            if (map[h][w] == wall) continue
            if (hw in set) continue
            if (isEnd(hw)) return BFSResult(res, true)

            set.add(hw)
            tmpQue.addAll(dir.map { it.first + h to it.second + w })
        }
        que.addAll(tmpQue)
    }

    return BFSResult(res,false)
}