private data class Cell(val row:Int, val col:Int)

fun main(args: Array<String>) {
    val (h, w) = readLine()!!.split(" ").map { it.toInt() }

    // # を起点にした幅優先探索実装

    var mat = Array<MutableList<Char>>(h) { mutableListOf() }
    for ( i in 0 until h) {
        mat[i] = readLine()!!.toCharArray().toMutableList()
    }

    var count = 0
    var queue = mutableListOf<MutableList<Cell>>()

    // 最初からすべて黒場合は 0 を出力
    var allblack = true
    var target = mutableListOf<Cell>()
    for (i in 0 until h) {
        for (j in 0 until w){
            if(mat[i][j] == "#".single()) {
                target.add(Cell(row = i, col = j))
            } else {
                allblack = false
            }
        }
    }

    if (allblack){
        print(0)
        return
    }

    // 幅優先探索で順次取り出し色を塗る, 濡れなかったところを次にenqueue
    queue.add(target)
    while(queue.size != 0) {
        target = queue.first()
        queue.removeAt(0)

        var painted_total = mutableListOf<Cell>()

        for ( c in target) {
            painted_total.addAll(paint(mat, c.row, c.col, h, w))
        }

        if (painted_total.size != 0){
            queue.add(painted_total)
            count += 1
        }
    }
    print(count)
}

private fun paint(mat: Array<MutableList<Char>>, row: Int, col: Int, h:Int, w:Int): MutableList<Cell> {
    var painted = mutableListOf<Cell>()

    if (row - 1 >= 0 && mat[row-1][col] == ".".single()){
        mat[row-1][col] = "#".single()
        painted.add(Cell(row = row-1, col = col))
    }
    if (row + 1 < h && mat[row+1][col] == ".".single()){
        mat[row+1][col] = "#".single()
        painted.add(Cell(row = row+1, col = col))
    }
    if (col - 1 >= 0 && mat[row][col-1] == ".".single()){
        mat[row][col-1] = "#".single()
        painted.add(Cell(row = row, col = col-1))
    }
    if (col + 1 < w && mat[row][col+1] == ".".single()){
        mat[row][col+1] = "#".single()
        painted.add(Cell(row = row, col = col+1))
    }

    return painted
}