h,w=gets.split.map &:to_i a=gets(p).split.map(&:to_i).sort h.times{|i|puts a[i*w,w]*?\s}