ior = io.read local h, w = ior("*n", "*n", "*l") local xs, ys, cnt = {}, {}, 0 local str ="" for i_h = 1, h do str = ior() for i_w = 1, w do if(str:sub(i_w, i_w) == "1") then table.insert(xs, i_w) table.insert(ys, i_h) cnt = cnt + 1 end end end local msq, mmi = math.sqrt, math.min local function getlen(x, y) local len = 0 for i = 1, cnt do len = len + msq((x - xs[i]) * (x - xs[i]) + (y - ys[i]) * (y - ys[i])) end return len end local minlen = getlen(1, 0) for i_w = 2, w do minlen = mmi(minlen, getlen(i_w, 0)) end for i_w = 1, w do minlen = mmi(minlen, getlen(i_w, h + 1)) end for i_h = 1, h do minlen = mmi(minlen, getlen(0, i_h)) end for i_h = 1, h do minlen = mmi(minlen, getlen(w + 1, i_h)) end print(string.format("%.8f", minlen))