#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stack>
#include <iostream>
#include <vector>
#include <string>
#include <time.h>

#define pb push_back

using namespace std;
using ll = long long;
using ull = unsigned long long;
using dl = double;

const ll N = 405 * 405;
const ll inf = 100000000000;

ifstream in("input.txt");
ofstream out("output.txt");

int n, m;
int a[N];
int b[405][405];
int c[405][405];
vector<int> g[405], h[405];
pair<int, int> op[N];
int opl = 0;

vector<int> test;
int main() {
    srand(time(0));

//    test.pb(1);
//    test.pb(2);
//    test.pb(3);
//    test.pb(4);
//
//    out << test.back() << endl;

    in >> n >> m;

    int k = 0;
    int inp;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            in >> inp;
            a[k++] = inp;
            b[i][j] = inp;
        }
    }
    int s = -1;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            ++c[i][b[i][j]];
            if (c[i][b[i][j]] >= 2) {
//                if (s == -1) {
                    s = i * m + j;
//                } else {
                    h[b[i][j]].pb(i * m + j);
//                }
            }
        }
    }
    int r = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (c[i][j] == 0) {
//                g[j].pb(i);
                g[i].pb(j);
                ++r;
            }
        }
    }

    if (s == -1) {
        out << 0 << endl;
        return 0;
    }

    for (int u = 0; u < m; ++u) {
        auto &v = h[u];
        if (v.empty()) continue;
        int s = v.back();
        v.pop_back();
//        v.erase(s);

        op[opl++] = {s + 1, n * m + 1};

        while (true) {

            int p = s / m;

            if (g[p].empty()) break;
            int ne = g[p].back();
            g[p].pop_back();

            if (h[ne].empty()) break;

            int ps = s + 1;

            s = h[ne].back();
            h[ne].pop_back();

            op[opl++] = {s + 1, ps};
        }
        op[opl++] = {n * m + 1, s + 1};

        if (opl != r + 1) continue;
        out << opl << endl;

        for (int i = 0; i < opl; ++i) {
            out << op[i].first << " " << op[i].second << endl;
        }
        v.push_back(s);
    }

    return 0;
}