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)

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

class Board(
  val H: Int,
  val W: Int
) {
  val BLACK = "#"
  val WHITE = "."
  var body: Array[Array[String]] = _
  var line: String = _

  def createBody(lines: Array[String]) {
    this.body = lines.map(line => line.split(""))
    this.createLine()
  }

  def createLine() {
    line = this.body.flatMap(r => r).foldLeft("")((p, n) => p + n)
  }

  def isPaintedAllColorAt(c: String=this.BLACK): Boolean = {
    if (line.filter(s => s.toString == this.WHITE).size == 0) true else false
  }

  def searchColor(c: String=this.BLACK): mutable.ArrayBuffer[(Int, Int)] = {
    val point = mutable.ArrayBuffer[(Int, Int)]()
    line.zipWithIndex.foreach{case (v, i) => if (v.toString == c) point += ((i / this.W, i % this.W))}
    point
  }

  def paintAround(i: Int, j: Int, c: String=this.BLACK) {
    // 左
    if (0 <= i - 1) {
      this.body(i - 1)(j) = c
    }
    // 上
    if (0 <= j - 1) {
      this.body(i)(j - 1) = c
    }
    // 右
    if (i + 1 < W) {
      this.body(i + 1)(j) = c
    }
    // 下
    if (j + 1 < H) {
      this.body(i)(j + 1) = c
    }
    this.createLine()
  }
}