Problem:
Score : 1100 points

Problem Statement

We have a grid with N rows and M columns of squares.
Each integer from 1 to NM is written in this grid once.
The number written in the square at the i-th row from the top and the j-th column from the left is A_{ij}.

You need to rearrange these numbers as follows:

First, for each of the N rows, rearrange the numbers written in it as you like.

Second, for each of the M columns, rearrange the numbers written in it as you like.

Finally, for each of the N rows, rearrange the numbers written in it as you like.

After rearranging the numbers, you want the number written in the square at the i-th row from the top and the j-th column from the left to be M\times (i-1)+j.
Construct one such way to rearrange the numbers. The constraints guarantee that it is always possible to achieve the objective.

Constraints

1 \leq N,M \leq 100

1 \leq A_{ij} \leq NM

A_{ij} are distinct.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print one way to rearrange the numbers in the following format:

B_{11} B_{12} ... B_{1M}
:
B_{N1} B_{N2} ... B_{NM}
C_{11} C_{12} ... C_{1M}
:
C_{N1} C_{N2} ... C_{NM}

Here B_{ij} is the number written in the square at the i-th row from the top and the j-th column from the left after Step 1, and C_{ij} is the number written in that square after Step 2.

Sample Input 1

3 2
2 6
4 3
1 5

Sample Output 1

2 6
4 3
5 1
2 1
4 3
5 6

Sample Input 2

3 4
1 4 7 10
2 5 8 11
3 6 9 12

Sample Output 2

1 4 7 10
5 8 11 2
9 12 3 6
1 4 3 2
5 8 7 6
9 12 11 10

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=10305,INF=1<<30;

struct edge{int to;ll cap;int rev;};

vector<edge> G[MAX];
int level[MAX];
int iter[MAX];

void add_edge(int from,int to,ll cap){
    G[from].push_back((edge){to,cap,int(G[to].size())});
    G[to].push_back((edge){from,0LL,int(G[from].size())-1});
}

void BFS(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s] =0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=0;i<G[v].size();i++){
            edge &e =G[v][i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

ll DFS(int v,int t,ll f){
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++){
        edge &e =G[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            ll d= DFS(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

ll max_flow(int s,int t){
    ll flow=0;
    for(;;){
        BFS(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        ll f;
        while((f=DFS(s,t,INF))>0){
            flow+=f;
        }
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int H,W;cin>>H>>W;
    vector<vector<int>> S(H,vector<int>(W));
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin>>S[i][j];
        }
    }
    
    for(int i=10001;i<=10000+H;i++){
        add_edge(10300,i,1);
    }
    for(int i=10101;i<=10100+H;i++){
        add_edge(i,10301,1);
    }
    
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            add_edge(10001+i,S[i][j],1);
            add_edge(S[i][j],10100+(S[i][j]-1)/W+1,1);
        }
    }
    
    vector<vector<int>> ans(H,vector<int>(W));
    vector<int> used(H*W+1);
    
    for(int t=0;t<W;t++){
        int a=max_flow(10300,10301);
        
        for(int i=10001;i<=10000+H;i++){
            for(auto it:G[i]){
                if(it.cap==0&&it.to<=H*W&&used[it.to]==0){
                    ans[i-10001][t]=it.to;
                    used[it.to]=1;
                    G[it.to][it.rev].cap=0;
                }
            }
        }
        
        for(int j=0;j<G[10300].size();j++){
            auto &e=G[10300][j];
            //cout<<e.cap<<endl;
            e.cap=1;
        }
        
        for(int j=0;j<G[10301].size();j++){
            auto &e=G[10301][j];
            //cout<<e.cap<<endl;
            e.cap=0;
        }
        
        for(int i=10001;i<=10000+H;i++){
            for(int j=0;j<G[i].size();j++){
                auto &e=G[i][j];
                if(e.to==10300){
                    //cout<<e.cap<<endl;
                    e.cap=0;
                }
            }
        }
        
        for(int i=10101;i<=10100+H;i++){
            for(int j=0;j<G[i].size();j++){
                auto &e=G[i][j];
                if(e.to==10301){
                    //cout<<e.cap<<endl;
                    e.cap=1;
                }
            }
        }
        
    }
    
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cout<<ans[i][j]<<" ";
        }
        cout<<endl;
    }
    
    for(int j=0;j<W;j++){
        vector<int> A(H);
        for(int i=0;i<H;i++) A[i]=ans[i][j];
        sort(all(A));
        for(int i=0;i<H;i++) ans[i][j]=A[i];
    }
    
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cout<<ans[i][j]<<" ";
        }
        cout<<endl;
    }
    
}