H, W = gets.split.map &:to_i $board = $<.map{|s|s.to_i(2)} def f(a, b) (a**2 + b**2) ** 0.5 end def g(yy, xx) sum = 0 H.times{|y| W.times{|x| next if $board[y][x] != 1 sum += f(xx - x, yy - y) } } sum end ans = 1.0/0 (0...H).each{|yy| [-1, W].each{|xx| ans = [ans, g(yy, xx)].min } } (0...W).each{|xx| [-1, H].each{|yy| ans = [ans, g(yy, xx)].min } } p ans