#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> land;
vector<vector<bool>> visited;
vector<pair<int, int>> peaks;
bool isPeak(int x, int y) {
int height = land[x][y];
if (x > 0 && land[x - 1][y] >= height) return false;
if (x < n - 1 && land[x + 1][y] >= height) return false;
if (y > 0 && land[x][y - 1] >= height) return false;
if (y < m - 1 && land[x][y + 1] >= height) return false;
return true;
}
void dfs(int x, int y) {
visited[x][y] = true;
if (isPeak(x, y)) {
peaks.push_back(make_pair(x + 1, y + 1));
}
if (x > 0 && !visited[x - 1][y]) dfs(x - 1, y);
if (x < n - 1 && !visited[x + 1][y]) dfs(x + 1, y);
if (y > 0 && !visited[x][y - 1]) dfs(x, y - 1);
if (y < m - 1 && !visited[x][y + 1]) dfs(x, y + 1);
}
int main() {
cin >> n >> m;
land.resize(n, vector<int>(m));
visited.resize(n, vector<bool>(m, false));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> land[i][j];
}
}
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
swap(land[x1 - 1][y1 - 1], land[x2 - 1][y2 - 1]);
peaks.clear();
visited.assign(n, vector<bool>(m, false));
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
if (!visited[j][k]) {
dfs(j, k);
}
}
}
cout << peaks.size() << endl;
for (auto peak : peaks) {
cout << peak.first << " " << peak.second << endl;
}
}
return 0;
}