#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx,avx2,tune=native")
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define int long long
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sqr(x) ((x)*(x))
#define PI 3.14159265358979323846
using namespace std;
using pii = pair <int, int>;
constexpr int maxn = 1e6 + 10;
constexpr int mod = 998244353;
constexpr long long inf = 1e18 + 10;
constexpr long double eps = 1e-15;
mt19937_64 rng (chrono::steady_clock::now ().time_since_epoch ().count ());

int m;

inline int get (pii a) {
    return a.X * m + a.Y + 1;
}

int a [444][444];
set <int> mas;
stack <int> add [444];
stack <pii> del [444];
int pos = -1;
vector <pii> res;

inline void solve () {
    int n;
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            cin >> a[i][j], --a[i][j];
    for (int i = 0; i < n; ++i) {
        mas.clear ();
        for (int j = 0; j < m; ++j) {
            if (mas.count (a[i][j])) del[a[i][j]].push (mp (i, j));
            mas.insert (a[i][j]);
        }
        for (int j = 0; j < m; ++j) if (!mas.count (j)) add[i].push (j);
    }
    pii v = mp (-1, -1);
    int C = 0;
    while (true) {
        //if (++C > 2) break;
        if (v.X == -1) {
            for (int i = 0; i < m; ++i) if (!del[i].empty ()) {
                pos = i;
                v = del[i].top ();
                del[i].pop ();
                break;
            }
            if (v.X == -1) break;
            res.pb (mp (get (v), n * m + 1));
            //cout << "p: " << v.X + 1 << ' ' << v.Y + 1 << '\n';
            //cout << get (v) << ' ' << n * m + 1 << '\n';
        }
        int x = add[v.X].top ();
        add[v.X].pop ();
        //cout << x + 1 << '\n';
        if (del[x].empty ()) {
            res.pb (mp (n * m + 1, get (v)));
            //cout << n * m + 1 << ' ' << get (v) << '\n';
            v = mp (-1, -1);
            continue;
        }
        pii u = del[x].top ();
        //cout << u.X  + 1 << ' ' << u.Y + 1 << '\n';
        del[x].pop ();
        res.pb (mp (get (u), get (v)));
        //cout << get (u) << ' ' << get (v) << '\n';
        v = u;
    }
    cout << (int) res.size () << '\n';
    for (pii i : res) cout << i.X << ' ' << i.Y << '\n';
}

signed main() {
    //freopen ("parallelepiped.in", "r", stdin);
    //freopen ("parallelepiped.out", "w", stdout);
    cin.tie(nullptr) -> sync_with_stdio(false);
    //cout << fixed << setprecision (12);
    int t = 1, g;
    //cin >> t;
    while (t--) solve ();
    return 0;
}