H, W = gets.split.map(&:to_i) P = H.times.map { gets.chomp.chars } def f(cx, cy) dist = 0 P.each.with_index(1) do |row, y| row.each.with_index(1) do |c, x| next if c == '0' dy = y - cy dx = x - cx dist += Math.sqrt(dy ** 2 + dx ** 2) end end dist end min_dist = Float::INFINITY 0.upto(W + 1) do |x| 0.upto(H + 1) do |y| next if 1 <= y && y <= H && 1 <= x && x <= W next if y == 0 && x == 0 next if y == 0 && x == W + 1 next if y == H + 1 && x == 0 next if y == H + 1 && x == W + 1 dist = f(x, y) if min_dist > dist min_dist = dist end end end puts "%.12f" % min_dist