E. Matrix Traversal

0

0 votes
Hard
Problem

Your friend has a matrix A of order n×m. And you want his matrix to play. But he will give you his matrix only when you return his matrix in a sorted form and printed in the format as shown. Smallest element must be at index (1,1)

Input

First line contain two integer denoting n and m respectively.

Next n lines contains m space seperated integers denoting Aij

Output

Print n lines containg m sapce seperated integers where each Aij must be in the format specified.

Constraint

1<=n,m<=500

0<Aij<=106

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

(15395612716181310211414)

After full matrix sorting we can get all numbers as

(12345678910111213141516)

If we start bending the sorted matrix as shown the fig above then we get the matrix as

(12341211105131496161587)

Editor Image

?