local function str2tbl(s)
    local t = {string.byte(s, 1, #s)}
    for i=1, #t do
        t[i] = string.char(t[i])
    end
    return t
end

------
local H, W = io.read("*n", "*n", "*l")
local grid = {}
for i=1, H do
    grid[i] = str2tbl(io.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