import scala.io.StdIn._
import scala.collection.mutable

object Main extends App {
  var line = readLine.split(" ").map(s => s.toInt)
  val H = line(0)
  val W = line(1)
  val A = (1 to H).map(_ => readLine).toArray
  val board = new Board(H, W)
  board.createBody(A)
  board.isPaintedAllColorAt()

  var cnt = 0
  while (!board.isPaintedAllColorAt()) {
    val points = board.searchColor()
    points.foreach(i => board.paintAround(i))
    cnt += 1
  }
  println(cnt)
}

class Board(
  val H: Int,
  val W: Int
) {
  val BLACK = '#'
  val WHITE = '.'
  var body: mutable.StringBuilder = _

  def createBody(lines: Array[String]) {
    this.body = new mutable.StringBuilder(lines.flatMap(r => r).foldLeft("")((p, n) => p + n))
  }

  def isPaintedAllColorAt(c: Char=this.WHITE): Boolean = {
    if (this.body.filter(s => s == c).size == 0) true else false
  }

  def searchColor(c: Char=this.BLACK) = {
    val point = mutable.ArrayBuffer[Int]()
    this.body.zipWithIndex.foreach{case (v, i) => if (v == c) point += i}
    point
  }

  def paintAround(i: Int, c: Char=this.BLACK) {
    val h_i = i / this.W // 行
    val w_i = i % this.W // 列
    // 左
    if (0 <= w_i - 1) {
      this.body(i - 1) = c
    }
    // 上
    if (0 <= h_i - 1) {
      this.body(i - this.W) = c
    }
    // 右
    if (w_i + 1 < this.W) {
      this.body(i + 1) = c
    }
    // 下
    if (h_i + 1 < this.H) {
      this.body(i + this.W) = c
    }
  }
}