H,W = gets.split.map{|m|m.to_i} P = $<.read.split("\n").map{|m|m.split('').map{|n|n.to_i}} #H,W,P = 1,1,[1] p 0 if P.inject(:+)==0 #puts "#{H} #{W}" #P.each{|e|p e} #puts '---' memo = Array.new(H+2).map{Array.new(W+2, 0.0)} #memo.each{|e|p e} for y in 0..H+1 for x in 0..W+1 next if [*1..H].include?(y) and [*1..W].include?(x) #puts "x,y = #{x},#{y}" for k in 0..H-1 for j in 0..W-1 next if P[k][j] == 0 #puts "j,k = #{j+1},#{k+1}" a = Math.sqrt( (x-(j+1))**2 + (y-(k+1))**2 ) memo[y][x] += a #puts "memo[#{y}][#{x}] += Math.sqrt( (#{x}-#{j+1})**2 + (#{y}-#{k+1})**2 ) = #{a}" end end #puts '' end end #puts '---' #memo.each{|e|p e} puts memo.flatten.sort.uniq[1]