def min(a,b); a < b ? a : b; end H,W = gets.split.map(&:to_i) P = H.times.map{ gets.chomp } def cost(x0, y0) s = 0.0 (1 .. W).each do |x| (1 .. H).each do |y| x2 = (x - x0).abs ** 2 y2 = (y - y0).abs ** 2 s += Math.sqrt(x2 + y2) if P[y-1][x-1] == '1' end end return s end cost_min = Float::INFINITY [0, W + 1].each do |x0| (1 .. H).each do |y0| cost_min = min(cost_min, cost(x0, y0)) end end [0, H + 1].each do |y0| (1 .. W).each do |x0| cost_min = min(cost_min, cost(x0, y0)) end end puts "%.8f" % cost_min