#include <bits/stdc++.h>

using namespace std;

#define DIM 407

long long n, m, a[DIM][DIM], id[DIM][DIM], it;

set<long long> need[DIM];

long long vis[DIM][DIM];

deque<long long> have[DIM][DIM];

vector<pair<long long, long long> > ans;

void dfs(long long v, long long c, long long h){
    bool flag=false;
    for(auto col : need[v]){
        for(int i=1; i<=n; i++){
            if(i==v) continue;
            if(have[i][col].size()==0 || need[i].count(h)!=0) continue;
            ans.push_back({have[i][col].back(), c});
            long long it=have[i][col].back();
            have[i][col].pop_back();
            flag=true;
            dfs(i, it, h);
        }
        if(flag){
            need[v].erase(col);
            break;
        }
    }

    for(auto col : need[v]){
        for(int i=1; i<=n; i++){
            if(i==v) continue;
            if(have[i][col].size()==0) continue;
            ans.push_back({have[i][col].back(), c});
            long long it=have[i][col].back();
            have[i][col].pop_back();
            flag=true;
            dfs(i, it, h);
        }
        if(flag){
            need[v].erase(col);
            break;
        }
    }
    if(flag) return;
    ans.push_back({n*m+1, c});
    need[v].erase(h);
    return;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    it=1;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cin>>a[i][j];
            id[i][j]=it;
            it++;
            if(vis[i][a[i][j]]){
                have[i][a[i][j]].push_back(id[i][j]);
            }
            vis[i][a[i][j]]=1;
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(!vis[i][j]) need[i].insert(j);
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(have[i][j].size()==0) continue;
            ans.push_back({have[i][j].back(), n*m+1});
            it=have[i][j].back();
            have[i][j].pop_back();
            dfs(i, it, j);
        }
    }
    cout<<ans.size()<<"\n";
    for(int i=0; i<ans.size(); i++) cout<<ans[i].first<<' '<<ans[i].second<<"\n";
    return 0;
}
