h, w = gets.split.map(&:to_i) numbers = readlines.map(&:split).flatten.map(&:to_i).sort.each_slice(w) { |a| puts a.join(' ') }