var lines = [];
var ans_s = []; var ans_w = [];
var readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

rl.on('line', function (x) {
    lines.push(x);
});

rl.on('close', function () {
    var H = Number(lines[0].split(" ")[0]);
    var W = Number(lines[0].split(" ")[1]);

    lines.shift();

    var A = lines.map(i => i.split(""))
    var black = []

    for (var i = 0; i < H; i++) {
        for (var j = 0; j < W; j++) {
            if (A[i][j] === "#") { black.push([i, j]); }
        }
    }

    //console.log(black)

    for (var i = 0; i < black.length; i++) {
        var y = black[i][0], x = black[i][1]

        var a = Math.abs(x - 0) + Math.abs(y - 0)
        var b = Math.abs(x - 0) + Math.abs(y - (H - 1))
        var c = Math.abs(x - (W - 1)) + Math.abs(y - 0)
        var d = Math.abs(x - (W - 1)) + Math.abs(y - (H - 1))

        ans_w[i] = Math.max(a, b, c, d);
    }


    for (var i = 0; i < black.length; i++) {
        var y = black[i][0], x = black[i][1]
        ans_s[i] = Infinity

        var x_max = Infinity, y_max = Infinity;

        for (var j = i + 1; j < black.length; j++) {
            var y_next = black[j][0]; x_next = black[j][1];

            // #までの距離
            var result = Math.abs(y - y_next) + Math.abs(x - x_next);

            //console.log(Math.abs(x - x_next), Math.abs(y - y_next))

            if (x_max >= Math.abs(x - x_next) && y_max >= Math.abs(y - y_next)) {
                ans_s[i] = result
                x_max = Math.abs(x - x_next); y_max = Math.abs(y - y_next)
            }
            else if (x_max < Math.abs(x - x_next) && y_max < Math.abs(y - y_next)) { }
            else if (x_max < Math.abs(x - x_next) || y_max < Math.abs(y - y_next)) {
                ans_s[i] = result
            }
            //console.log(ans_s[i], result, x, y, x_next, y_next, x_max, y_max)
        }
    }
    ans_w.sort((a, b) => a - b)
    ans_s.sort((a, b) => a - b)
    console.log(Math.min(ans_w[0], ans_s[0]))
});