local function str2tbl(s)
    local t = {string.unpack(string.rep("c1", #s), s)}
    table.remove(t)
    return t
end

local function setdefault(t, v)
    setmetatable(t, {__index = function() return v end})
end

local function read_array(N)
    local t, insert, read = {}, table.insert, io.read
    for i=1, N do insert(t, read("n")) end
    return t
end

local function read(ptn)
    local t = {}
    local ptn = str2tbl(ptn)
    for i=1, #ptn do
        c = ptn[i]
        if c == "n" then
            table.insert(t, io.read("n"))
        elseif c == "L" then
            io.read("l")
        elseif c == "l" then
            table.insert(t, io.read("l"))
        end
    end
    return table.unpack(t)
end

------
local H, W = read("nnL")
local grid = {}
for i=1, H do
    grid[i] = str2tbl(read("l"))
end
--[[
for i=1, H do
    for j=1, W do
        print(i,j, grid[i][j])
    end
end
]]
local count = 0
while true do
    local all_black = true
    for i=1, H do
        for j=1, W do
            local c = grid[i][j]
            if c == '.' then
                all_black = false
                break
            end
        end
    end
    if all_black then
        print(count)
        return
    end
    local blacks = {}
    for i=1, H do
        for j=1, W do
            local c = grid[i][j]
            if c == '#' then
                table.insert(blacks, {i, j})
            end
        end
    end
    for _, t in ipairs(blacks) do
        local i, j = t[1], t[2]
        if i >= 2 then
            grid[i-1][j] = '#'
        end
        if i <= H - 1 then
            grid[i+1][j] = '#'
        end
        if j >= 2 then
            grid[i][j-1] = '#'
        end
        if j <= W - 1 then
            grid[i][j+1] = '#'
        end
    end
    --[[
    print(count)
    for i=1, H do
        print(table.concat(grid[i], ''))
    end
    ]]
    count = count + 1
end