RT,WA10,https://codeforces.com/gym/103698/problem/D
#include <bits/stdc++.h>
using namespace std;
char c;
struct no {int x, y;};
queue < no > que;
vector < int > P, Q;
map < int, int > V1, V2;
inline void OP () {for (int i = 0;i < P.size (); ++ i) cout << P[i] << ' '; cout << endl;}
inline void OQ () {for (int i = 0;i < Q.size (); ++ i) cout << Q[i] << ' '; cout << endl;}
inline void solve () {
while (!que.empty ()) {
no frt = que.front (); que.pop ();
int ans = 2;
if (frt.x == frt.y) {
cout << "-1\n"; exit (0);
}
int ans1, ans2; ans1 = ans2 = 1;
if (V1[frt.y] == 1 || V2[frt.x] == 1) {
ans1 = 0;
}
if (V1[frt.x] == 1 || V2[frt.y] == 1) {
ans2 = 0;
}
if (ans1 + ans2 == 0) {
cout << "-1\n"; exit (0);
}
if (V1[frt.y] == 1 || V2[frt.x] == 1) {
swap (frt.x, frt.y);
}
if (V1[frt.x] == 0) P.push_back (frt.x);
if (V2[frt.y] == 0) Q.push_back (frt.y);
V1[frt.x] = 1;
V2[frt.y] = 1;
}
cout << min (P.size (), Q.size ()) << endl;
sort (P.begin (), P.end ());
sort (Q.begin (), Q.end ());
if (P.size () != Q.size ()) {
if (P.size () < Q.size ()) OP ();
else OQ ();
}
else {
if (P[0] < Q[0]) OP ();
else OQ ();
}
}
int n;
signed main () {
cin >> n;
for (int i = 1;i <= n; ++ i) {
for (int j = 1;j <= n; ++ j) {
cin >> c;
if (c == '1') {
que.push ({min (i, j), max (i, j)});
// cout << min (i, j) << ' ' << max (i, j) << endl;
}
}
}
if (que.empty ()) cout << "0\n";
else solve ();
}