h, w = gets.split.map(&:to_i) a = [] for i in 0..(h-1) b = gets.split.map(&:to_i) a += b end a = a.sort() h.times do |y| (w-1).times do |x| print a[y * w + x].to_s + " " end puts a[y * w + w - 1] end