h, w = gets.split.map(&:to_i) lines = h.times.map { gets.split.map(&:to_i) } puts lines.flatten.sort.each_slice(w).map { _1.join(" ") }