#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pi 3.14159265358979323846
#define sqr(a) (a)*(a)
#define v vector
#define ll long long
#define ld long double
#define f first
#define sec second
#define pb push_back
#define pf push_front
#define umap unordered_map
#define sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define mod 1000000007
#define inf 2*1e18

using namespace std;
using namespace __gnu_pbds;

typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>uset;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    srand(time(NULL));
    ll n,m,k,i,j,x=-1,ans=0,y;
    cin>>n>>m;
    k=n*m;
    v<ll>a(k),cnt(m),d(k);
    v<deque<ll>>mas(m);
    set<ll>st;
    for (i=0;i<k;i++){
        cin>>a[i];
        a[i]--;
    }
    for (i=0;i<n;i++){
        st.clear();
        for (j=0;j<m;j++){
            st.insert(j);
            cnt[j]=0;
        }
        for (j=0;j<m;j++){
            if (st.find(a[i*m+j])!=st.end()){
                st.erase(a[i*m+j]);
            }
        }
        for (j=0;j<m;j++){
            if (cnt[a[i*m+j]]){
                d[i*m+j]=*st.begin();
                st.erase(*st.begin());
                ans++;
                if (x==-1){
                    x=i*m+j;
                }else{
                    mas[a[i*m+j]].pb(i*m+j);
                }
            }else{ 
                cnt[a[i*m+j]]++;
            }
        }
    }
    if (x==-1){
        cout<<0;
    }else{
        cout<<ans+1<<endl;
        cout<<x+1<<' '<<31<<endl;
        for (i=0;i<ans-1;i++){
            cout<<x+1<<' '<<mas[d[x]][0]+1<<endl;
            y=x;
            x=mas[d[x]][0];
            mas[d[y]].pop_front();
        }
        cout<<x+1<<' '<<31<<endl;
    }
    return 0;
}
