#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pii pair <int, int>
using namespace std;
const ll inf = 1e18;
constexpr ll mod = 998244353;
const int MAXN = 1e5+10;
mt19937 rnd(time(nullptr));

int a[401][401];

void solve(){
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j) cin >> a[i][j];
	
	set <int> del[401], ins[401];
	vector <pii> vec;
	for (int i = 1; i <= n; ++i){
		set <int> s;
		for (int j = 1; j <= m; ++j){
			if (s.find(a[i][j]) != s.end()){
				del[a[i][j]].insert((i - 1) * m + j);
				vec.pb({(i - 1) * m + j, a[i][j]});
			}
			else s.insert(a[i][j]);
		}
		for (int j = 1; j <= m; ++j)
			if (s.find(j) == s.end()) ins[i].insert(j);
	}
	
	int cell = n * m + 1, cnt = 0;
	//vector <pii> ans;
	for (int i = 1; i <= m; ++i) cnt += del[i].size();
	if (cnt == 0){
		cout << 0;
		return;
	}
	cout << cnt + 1 << '\n';
	for (int i = 1; i <= m; ++i)
		if (!del[i].empty()){
			cell = *del[i].begin();
			//ans.pb({cell, m * n + 1});
			cout << cell << " " << m * n + 1 << '\n';
			del[i].erase(del[i].begin());
			//if (del[i].empty()) del.erase(i);
			--cnt;
			break;
		}
	/*pii start = vec[rnd() % cnt];
	cell = start.ff;
	cout << cell << " " << m * n + 1 << '\n';
	del[start.ss].erase(cell);
	--cnt;*/
	while (cnt){
		int r = (cell + m - 1) / m;
		int color = *ins[r].begin();
		if (del[color].empty()) color = *ins[r].rbegin();
		if (del[color].empty()) break;
		int v = *del[color].begin(); del[color].erase(del[color].begin());
		//ans.pb({v, cell});
		cout << v << " " << cell << '\n';
		cell = v;
		ins[r].erase(color);
		--cnt;
	}
	//ans.pb({m * n + 1, cell});
	cout << m * n + 1 << " " << cell;
	//cout << ans.size() << '\n';
	//for (auto i : ans) cout << i.ff << " " << i.ss << '\n';
}
 
signed main(){
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    solve();
}