h, w = read_line.split.map &.to_i a = (1..h).reduce([] of Int32) { |acc, i| acc.concat(read_line.split.map &.to_i) }.sort puts h.times.join('\n') { |i| a[i*w...(i + 1)*w].join(' ') }