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
    val br = new Breaks
    br.breakable {
      while (true) {
        val _tiles = tiles.map(_.map(x => x).toSeq).toSeq
        (0 until h).foreach { i =>
          (0 until w).foreach { j =>
            if (i == 0) {
              if (j == 0) {
                paint(tiles, _tiles, i, j, Seq(1, 2))
              } else if (j == w - 1) {
                paint(tiles, _tiles, i, j, Seq(2, 3))
              } else {
                paint(tiles, _tiles, i, j, Seq(1, 2, 3))
              }
            } else if (i == h - 1) {
              if (j == 0) {
                paint(tiles, _tiles, i, j, Seq(0, 1))
              } else if (j == w - 1) {
                paint(tiles, _tiles, i, j, Seq(0, 3))
              } else {
                paint(tiles, _tiles, i, j, Seq(0, 1, 3))
              }
            } else {
              if (j == 0) {
                paint(tiles, _tiles, i, j, Seq(0, 1, 2))
              } else if (j == w - 1) {
                paint(tiles, _tiles, i, j, Seq(0, 2, 3))
              } else {
                paint(tiles, _tiles, i, j, Seq(0, 1, 2, 3))
              }
            }
          }
        }
        counter += 1
        if (tiles.map(_.count(_ == "#")).sum == h * w) br.break()
      }
    }

    val result = counter.toString
    result
  }

  def paint(tiles: Array[Array[String]], copy: Seq[Seq[String]], i: Int, j: Int, directions: Seq[Int]): Unit = {
    directions.foreach { d =>
      if (tiles(i)(j) != "#") {
        if (d == 0) {
          if (copy(i - 1)(j) == "#") tiles(i)(j) = "#"
        } else if (d == 1) {
          if (copy(i)(j + 1) == "#") tiles(i)(j) = "#"
        } else if (d == 2) {
          if (copy(i + 1)(j) == "#") tiles(i)(j) = "#"
        } else {
          if (copy(i)(j - 1) == "#") tiles(i)(j) = "#"
        }
      }
    }
  }

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