def lscan; gets.split.map(&:to_i); end n, h = lscan div = (1..(n-1)).filter{|x| n/x*x == n}.to_a dd = div + [n] dd.each do |w| h2 = 0 stk = 0 ok = true div.each do |x| if x/w*w != x stk += x if stk > w # p [w, stk] ok = false break end if stk == w h2 += 1 stk = 0 end next end h2 += x/w end next unless ok # p h2 if h2 == h || w == h mat = [] stk = [] div.each do |x| if x/w*w != x stk += [x]*x if stk.size == w mat << stk stk = [] end next end (x/w).times {mat << [x]*w} end mat = mat.transpose if w == h puts mat.map{|l| l*' '} exit end end p -1