import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on

void main()
{
    long H, W;
    scan(H, W);
    auto B = new bool[][][](H, W, 2);
    foreach (i; 0 .. H)
    {
        auto A = sread();
        foreach (j, c; A)
            B[i][j][0] = (c == '#');
    }
    long cnt;
    while (true)
    {
        bool all = true;
        foreach (i; 0 .. H)
            foreach (j; 0 .. W)
            {
                all = all && B[i][j][cnt & 1];
                if (!B[i][j][cnt & 1])
                {
                    bool b = false;
                    if (0 < i)
                        b = b || B[i - 1][j][cnt & 1];
                    if (0 < j)
                        b = b || B[i][j - 1][cnt & 1];
                    if (i + 1 < H)
                        b = b || B[i + 1][j][cnt & 1];
                    if (j + 1 < W)
                        b = b || B[i][j + 1][cnt & 1];
                    B[i][j][(cnt + 1) & 1] = b;
                }
                else
                {
                    B[i][j][(cnt + 1) & 1] = B[i][j][cnt & 1];
                }
            }
        cnt += !all;
        if (all)
        {
            writeln(cnt);
            return;
        }
    }
}
