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

import scala.util.control.Breaks

object Main {
  def main(args: Array[String]): Unit = {
    val lines = io.Source.stdin.getLines().toStream
    println(solve(lines))
  }

  def solve(lines: Seq[String]): String = {
    val Array(h, w) = lines.head.split(" ").map(_.toInt)
    val tiles = lines.tail.take(h).map(_.split("").filter(_.length > 0).take(w)).toArray
    debug(h, w, tiles)

    var counter = 0
    var blacks = tiles.map(_.count(_ == "#")).sum
    while (blacks < h * w) {
      val _tiles = tiles.map(_.map(x => x).toIndexedSeq).toIndexedSeq
      val whites = _tiles.map(x => x.zipWithIndex.filter(_._1 == ".").map(_._2)).filter(_.nonEmpty).zipWithIndex.flatMap(x => x._1.map(y => (x._2, y)))
      whites.foreach { case (i, j) =>
        if (i == 0) {
          if (j == 0) {
            blacks += paint(tiles, _tiles, i, j, Seq(1, 2))
          } else if (j == w - 1) {
            blacks += paint(tiles, _tiles, i, j, Seq(2, 3))
          } else {
            blacks += paint(tiles, _tiles, i, j, Seq(1, 2, 3))
          }
        } else if (i == h - 1) {
          if (j == 0) {
            blacks += paint(tiles, _tiles, i, j, Seq(0, 1))
          } else if (j == w - 1) {
            blacks += paint(tiles, _tiles, i, j, Seq(0, 3))
          } else {
            blacks += paint(tiles, _tiles, i, j, Seq(0, 1, 3))
          }
        } else {
          if (j == 0) {
            blacks += paint(tiles, _tiles, i, j, Seq(0, 1, 2))
          } else if (j == w - 1) {
            blacks += paint(tiles, _tiles, i, j, Seq(0, 2, 3))
          } else {
            blacks += paint(tiles, _tiles, i, j, Seq(0, 1, 2, 3))
          }
        }
      }
      counter += 1
    }

    val result = counter.toString
    result
  }

  def paint(tiles: Array[Array[String]], copy: Seq[Seq[String]], i: Int, j: Int, directions: Seq[Int]): Int = {
    var result = 0
    val br = new Breaks
    br.breakable {
      directions.foreach { d =>
        if (d == 0) {
          if (copy(i - 1)(j) == "#") {
            tiles(i)(j) = "#"
            result = 1
          }
        } else if (d == 1) {
          if (copy(i)(j + 1) == "#") {
            tiles(i)(j) = "#"
            result = 1
          }
        } else if (d == 2) {
          if (copy(i + 1)(j) == "#") {
            tiles(i)(j) = "#"
            result = 1
          }
        } else {
          if (copy(i)(j - 1) == "#") {
            tiles(i)(j) = "#"
            result = 1
          }
        }
        if (result != 0) br.break()
      }
    }
    result
  }

  private def debug(x: Any): Unit = {
    if (System.getenv("LOCAL_DEBUG") != null)
      println(x)
  }
}