#include<bits/stdc++.h>
using namespace std;
#define el "\n"
#define ll long long
#define ull unsigned long long
#define se second
#define fi first
#define be begin()
#define en end()
#define Faster cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
vector<pair<int,int>> vt;
bool cmp(pair<int,int> a, pair<int,int> b)
{
if(a.fi + a.se != b.fi + b.se) return a.fi + a.se < b.fi + b.se;
return a.fi < b.fi;
}
void findSubMatrix(int **a, int n)
{
for(pair<int,int> x : vt) cout << "(" << x.fi << "," << x.se << ")" << el;
cout << el;
}
void sortBySum(int **a, int n)
{
sort(vt.begin(), vt.end(), cmp);
for(pair<int,int> x : vt) cout << "(" << x.fi << "," << x.se << ")" << el;
cout << el;
}
int main()
{
Faster;
int n; cin >> n;
int **a = new int*[n];
for(int i = 0; i < n; i++)
{
a[i] = new int[n];
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++) cin >> a[i][j];
}
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - 1; j++)
{
int ans = 0;
for(int x = i; x < i + 2; x++)
{
for(int y =j; y < j + 2; y++)
{
ans += a[x][y];
}
}
if(ans == 0 || ans == 4) vt.push_back({i,j});
}
}
findSubMatrix(a, n);
sortBySum(a, n);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBlbCAiXG4iCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgdWxsIHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIHNlIHNlY29uZAojZGVmaW5lIGZpIGZpcnN0CiNkZWZpbmUgYmUgYmVnaW4oKQojZGVmaW5lIGVuIGVuZCgpCiNkZWZpbmUgRmFzdGVyIGNpbi50aWUoMCk7IGNvdXQudGllKDApOyBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOwp2ZWN0b3I8cGFpcjxpbnQsaW50Pj4gdnQ7CmJvb2wgY21wKHBhaXI8aW50LGludD4gYSwgcGFpcjxpbnQsaW50PiBiKQp7CiAgICBpZihhLmZpICsgYS5zZSAhPSBiLmZpICsgYi5zZSkgcmV0dXJuIGEuZmkgKyBhLnNlIDwgYi5maSArIGIuc2U7CiAgICByZXR1cm4gYS5maSA8IGIuZmk7Cn0Kdm9pZCBmaW5kU3ViTWF0cml4KGludCAqKmEsIGludCBuKQp7CiAgICBmb3IocGFpcjxpbnQsaW50PiB4IDogdnQpIGNvdXQgPDwgIigiIDw8IHguZmkgPDwgIiwiIDw8IHguc2UgPDwgIikiIDw8IGVsOwogICAgY291dCA8PCBlbDsKfQoKdm9pZCBzb3J0QnlTdW0oaW50ICoqYSwgaW50IG4pCnsKICAgIHNvcnQodnQuYmVnaW4oKSwgdnQuZW5kKCksIGNtcCk7CiAgICBmb3IocGFpcjxpbnQsaW50PiB4IDogdnQpIGNvdXQgPDwgIigiIDw8IHguZmkgPDwgIiwiIDw8IHguc2UgPDwgIikiIDw8IGVsOwogICAgY291dCA8PCBlbDsKfQoKaW50IG1haW4oKQp7CiAgICBGYXN0ZXI7CiAgICBpbnQgbjsgY2luID4+IG47CiAgICBpbnQgKiphID0gbmV3IGludCpbbl07CiAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgewogICAgICAgIGFbaV0gPSBuZXcgaW50W25dOwogICAgfQogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgIHsKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgbjsgaisrKSBjaW4gPj4gYVtpXVtqXTsKICAgIH0KICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuIC0gMTsgaSsrKQogICAgewogICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBuIC0gMTsgaisrKQogICAgICAgIHsKICAgICAgICAgICAgaW50IGFucyAgPSAwOwogICAgICAgICAgICBmb3IoaW50IHggPSBpOyB4IDwgaSArIDI7IHgrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yKGludCB5ID1qOyB5IDwgaiArIDI7IHkrKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBhbnMgKz0gYVt4XVt5XTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZihhbnMgPT0gMCB8fCBhbnMgPT0gNCkgdnQucHVzaF9iYWNrKHtpLGp9KTsKICAgICAgICB9CiAgICB9CiAgICBmaW5kU3ViTWF0cml4KGEsIG4pOwogICAgc29ydEJ5U3VtKGEsIG4pOwogICAgcmV0dXJuIDA7Cn0KCg==
(1,1)
(1,4)
(3,0)
(4,0)
(4,4)
(1,1)
(3,0)
(4,0)
(1,4)
(4,4)