local ior = io.input() local h, w = ior:read("*n", "*n", "*l") local function getindex(i_h, i_w) return i_w + (i_h - 1) * w end local taskstate = {} for i = 1, h * w do taskstate[i] = false end local tasks = {} local tasknum = 0 local done = 0 local tasklim = h * w local function addtask(idx) if(not taskstate[idx]) then taskstate[idx] = true tasknum = tasknum + 1 local taskidx = tasknum % tasklim if taskidx == 0 then taskidx = tasklim end tasks[taskidx] = idx end end local function createMap() local str = "" local map = {} local inf = h + w for i_h = 1, h do str = ior:read() for i_w = 1, w do local index = getindex(i_h, i_w) if str:sub(i_w, i_w) == "#" then if i_h == 1 or i_h == h or i_w == 1 or i_w == w then map[index] = 1 addtask(index) else map[index] = inf end else map[index] = 0 addtask(index) end end end return map end local map = createMap() local function walk(src, dst) if map[src] + 1 < map[dst] then map[dst] = map[src] + 1 addtask(dst) end end while(done < tasknum) do done = done + 1 local taskidx = done % tasklim if(taskidx == 0) then taskidx = tasklim end local idx = tasks[taskidx] taskstate[idx] = false if w < idx then walk(idx, idx - w) end if idx <= (h - 1) * w then walk(idx, idx + w) end if 1 < w then if idx % w ~= 0 then walk(idx, idx + 1) if w < idx then walk(idx, idx + 1 - w) end if idx <= (h - 1) * w then walk(idx, idx + 1 + w) end end if idx % w ~= 1 then walk(idx, idx - 1) if w < idx then walk(idx, idx - 1 - w) end if idx <= (h - 1) * w then walk(idx, idx - 1 + w) end end end end local mma = math.max local ret = 1 for i = 1, h * w do ret = mma(ret, map[i]) end print(ret)