#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> a(n + 1, vector<int>(n + 1));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            cin >> a[i][j];
        }
    }
    int q;
    cin >> q;
    while (q--) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        // 模拟小球移动轨迹
        while (x1 <= x2 && y1 <= y2) {
            if (a[x1][y1] == 0) {
                ++x1;
            } else {
                ++y1;
            }
        }
        // 判断滚出边界的方向
        if (x1 > x2) {
            cout << x1 - 1 << ' ' << y1 << endl;
        }
        if (y1 > y2) {
            cout << x1 << ' ' << y1 - 1 << endl;
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")