def ascan; gets.split.map(&:to_i); end H,W = ascan F = H.times.map{gets.chomp} best = 1e18 check = lambda do |cy, cx| d = 0 H.times do |y| W.times do |x| d += Math.sqrt((y-cy)**2+(x-cx)**2) if F[y][x] == '1' end end best = [d, best].min end H.times do |cy| check.call(cy, -1) check.call(cy, W) end W.times do |cx| check.call(-1, cx) check.call(H, cx) end p best