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)
  }
}
