#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

const int MAXN = 5e4 + 5;

struct edge
{
    int v;
    int edgeInd;
};

int n, m;
int a[105][105];

int node[105][105];
pair <int, int> node2Cell[MAXN];

int S, T;
vector <int> cap;
vector <edge> graph[MAXN];

queue <int> q;
int dist[MAXN];
int startInd[MAXN];

int m1[105][105], m2[105][105];
bool used[105][105];

void addEdge(int u, int v)
{
    //cout << u << " --> " << v << "\n";

    cap.push_back(1);
    cap.push_back(0);

    graph[u].push_back({v, cap.size()-2});
    graph[v].push_back({u, cap.size()-1});

    //cout << "                           " << graph[13].size() << '\n';
}

void BFS()
{
    memset(dist, 0, sizeof(dist));

    dist[S] = 1;
    q.push(S);

    while(q.empty()==false)
    {
        int x = q.front();
        q.pop();

        //cout << x << " &&&&> " << dist[x] << '\n';

        for(edge e: graph[x])
        {
            //cout << "       " << e.v << " " << cap[e.edgeInd] << '\n';
            if(dist[e.v]==0 && cap[e.edgeInd]>0)
            {
                q.push(e.v);
                dist[e.v] = dist[x] + 1;
            }
        }
    }
}

int DFS(int x, int minVal)
{
    if(x==T) return minVal;

    for(int i = startInd[x];i<graph[x].size();i++)
    {
        edge e = graph[x][i];
        if(dist[e.v]==dist[x]+1 && cap[e.edgeInd]>0)
        {
            int flow = DFS(e.v, min(minVal, cap[e.edgeInd]));
            if(flow>0)
            {
                //cout << x << " -> " << e.v << '\n';

                cap[e.edgeInd] -= flow;
                cap[e.edgeInd^1] += flow;

                return flow;
            }
        }

        startInd[x]++;
    }

    return 0;
}

int Dinic()
{
    int maxFlow = 0;
    while(true)
    {
        BFS();
        if(dist[T]==0) break;

        memset(startInd, 0, sizeof(startInd));
        while(true)
        {
            int flow = DFS(S, 1);
            if(flow==0) break;

            //cout << "---- += " << flow << '\n';
            maxFlow += flow;
        }
    }

    return maxFlow;
}

void Init()
{
    S = 303;
    T = 304;
}

void constructGraph(int col)
{
    /*
    int nodeCnt = 1;
    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=m;j++)
        {
            node[i][j] = nodeCnt;
            node2Cell[nodeCnt] = {i, j};

            nodeCnt++;
        }
    }

    S = nodeCnt;nodeCnt++;
    T = nodeCnt;nodeCnt++;
    */

    cap.clear();
    for(int i = 1;i<=350;i++)
    {
        graph[i].clear();
    }

    /*
    bool added[105];
    memset(added, false, sizeof(added));
    for(int i = 1;i<=n;i++)
    {
        if(m1[i][col]!=0)
        {
            //added[ (m1[i][col]+m-1)/m ] = true;
        }
    }
    */
    for(int i = 1;i<=n;i++)
    {
        if(m1[i][col]!=0)
        {
            addEdge(i, (m1[i][col]+m-1)/m+n);
            continue;
        }

        for(int j = col;j<=m;j++)
        {
            if(m1[i][j]!=0)
            {
                continue;
            }
            int row = (a[i][j]+m-1)/m;

            //if(added[row]==true) continue;

            //cout << "link " << i << " " << row + n << '\n';
            addEdge(i, row+n);
        }
    }

    for(int j = 1;j<=n;j++)
    {
        addEdge(n+j, T);
    }

    for(int i = 1;i<=n;i++)
    {
        addEdge(S, i);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=m;j++)
        {
            cin >> a[i][j];
        }
    }

    Init();
    for(int j = 1;j<=m;j++)
    {
        constructGraph(j);
        Dinic();

        /*
        cout << " ---- " << '\n';
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=m;j++)
            {
                cout << m1[i][j] << " ";
            }
            cout << '\n';
        }
        */

        for(int i = 1;i<=n;i++)
        {
            //if(m1[i][j]!=0) continue;

            int row = -1;
            for(edge e: graph[i])
            {
                if(e.v!=S && e.v!=T && cap[e.edgeInd]==0)
                {
                    row = e.v - n;
                    break;
                }
            }

            //cout << i <<  " ---> " << row << '\n';

            for(int j1 = j;j1<=m;j1++)
            {
                if(m1[i][j1]==0 && (a[i][j1]+m-1)/m==row)
                {
                    //cout << i << "$$$" << j1 << " " << (a[i][j1]+m-1)/m << " $$ " << a[i][j1] << '\n';
                    m1[i][j] = a[i][j1];
                    swap(a[i][j], a[i][j1]);
                    //m1[i][j1] = a[i][j];

                    break;
                }
            }
        }
    }

    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=m;j++)
        {
            cout << m1[i][j] << " ";
        }
        cout << '\n';
    }

    for(int j = 1;j<=m;j++)
    {
        vector <int> v;
        for(int i = 1;i<=n;i++)
        {
            v.push_back(m1[i][j]);
        }

        sort(v.begin(), v.end());
        for(int i = 1;i<=n;i++)
        {
            m2[i][j] = v[i-1];
        }
    }

    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=m;j++)
        {
            cout << m2[i][j] << " ";
        }
        cout << '\n';
    }
}

/*
2 5
1 3 5 7 9
2 4 6 8 10
*/


Measured memory_bytes: 2432