#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <functional>
using namespace std;
char mat[111][111];
int n,deg[111],uk[111][111],now[111],tot;
int pa[111],num[111];
int dist[111];
int adj[111][111];
int vec[111][111],num_vec[111];
int vs[111],times,u;
int spfa()
{
int id,ip,i,j,s,p,q;
for(i=0;i<=n;i++)
num_vec[i]=0;
for(i=0;i<n;i++)
{
dist[i]=now[i]+deg[i];
pa[i]=-1;
vec[now[i]+deg[i]][num_vec[now[i]+deg[i]]++]=i;
}
times++;
while(true)
{
for(;u>=0;u--)
{
while(num_vec[u]>0)
{
id=vec[u][num_vec[u]-1];
if(vs[id]!=times)
break;
num_vec[u]--;
}
if(num_vec[u]>0)
break;
}
if(u<0)
return -1;
num_vec[u]--;
vs[id]=times;
for(j=0;j<num[id];j++)
{
ip=adj[id][j];
if(dist[ip]<dist[id]&&uk[id][ip]==0)
{
dist[ip]=dist[id];
pa[ip]=id;
if(dist[ip]>=now[ip]+deg[ip]+2)
return ip;
vec[dist[ip]][num_vec[dist[ip]]++]=ip;
}
}
}
}
int main()
{
int i,j,s,p,q,id,ip,dk;
while(scanf("%d",&n)==1)
{
tot=n*(n-1)*(n-2)/6;
for(i=0;i<n;i++)
deg[i]=now[i]=num[i]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
mat[i][j]=getchar();
while(mat[i][j]<'0'||mat[i][j]>'9')
mat[i][j]=getchar();
if(i==j)
continue;
if(mat[i][j]=='0')
deg[i]++;
else if(mat[i][j]=='2')
{
if(i<j)
{
uk[i][j]=rand()%2;
uk[j][i]=1-uk[i][j];
if(uk[i][j]==0)
now[i]++;
else
now[j]++;
}
adj[i][num[i]++]=j;
}
}
u=n;
while(true)
{
ip=spfa();
if(ip<0)
break;
now[ip]++;
while(pa[ip]>=0)
{
uk[pa[ip]][ip]=1;
uk[ip][p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RlZ1tpXV1bbnVtX3ZlY1tub3dbaV0rZGVnW2ldXSsrXT1pOwogICAgfSAKICAgIHRpbWVzKys7CiAgICB3aGlsZSh0cnVlKQogICAgewogICAgICAgIGZvcig7dT49MDt1LS0pCiAgICAgICAgewogICAgICAgICAgICB3aGlsZShudW1fdmVjW3VdPjApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlkPXZlY1t1XVtudW1fdmVjW3VdLTFdOwogICAgICAgICAgICAgICAgaWYodnNbaWRdIT10aW1lcykKICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgbnVtX3ZlY1t1XS0tOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKG51bV92ZWNbdV0+MCkKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBpZih1PDApCiAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgIG51bV92ZWNbdV0tLTsKICAgICAgICB2c1tpZF09dGltZXM7CiAgICAgICAgZm9yKGo9MDtqPG51bVtpZF07aisrKQogICAgICAgIHsKICAgICAgICAgICAgaXA9YWRqW2lkXVtqXTsKICAgICAgICAgICAgaWYoZGlzdFtpcF08ZGlzdFtpZF0mJnVrW2lkXVtpcF09PTApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGRpc3RbaXBdPWRpc3RbaWRdOwogICAgICAgICAgICAgICAgcGFbaXBdPWlkOwogICAgICAgICAgICAgICAgaWYoZGlzdFtpcF0+PW5vd1tpcF0rZGVnW2lwXSsyKQogICAgICAgICAgICAgICAgICAgIHJldHVybiBpcDsKICAgICAgICAgICAgICAgIHZlY1tkaXN0W2lwXV1bbnVtX3ZlY1tkaXN0W2lwXV0rK109aXA7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KaW50IG1haW4oKQp7CiAgICBpbnQgaSxqLHMscCxxLGlkLGlwLGRrOwogICAgd2hpbGUoc2NhbmYoIiVkIiwmbik9PTEpCiAgICB7CiAgICAgICAgdG90PW4qKG4tMSkqKG4tMikvNjsKICAgICAgICBmb3IoaT0wO2k8bjtpKyspCiAgICAgICAgICAgIGRlZ1tpXT1ub3dbaV09bnVtW2ldPTA7CiAgICAgICAgZm9yKGk9MDtpPG47aSsrKQogICAgICAgICAgZm9yKGo9MDtqPG47aisrKQogICAgICAgICAgewogICAgICAgICAgICAgbWF0W2ldW2pdPWdldGNoYXIoKTsKICAgICAgICAgICAgIHdoaWxlKG1hdFtpXVtqXTwnMCd8fG1hdFtpXVtqXT4nOScpCiAgICAgICAgICAgICAgICAgbWF0W2ldW2pdPWdldGNoYXIoKTsKICAgICAgICAgICAgICBpZihpPT1qKQogICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgIGlmKG1hdFtpXVtqXT09JzAnKQogICAgICAgICAgICAgICAgIGRlZ1tpXSsrOwogICAgICAgICAgICAgIGVsc2UgaWYobWF0W2ldW2pdPT0nMicpCiAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgIGlmKGk8aikKICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICB1a1tpXVtqXT1yYW5kKCklMjsKICAgICAgICAgICAgICAgICAgICAgIHVrW2pdW2ldPTEtdWtbaV1bal07IAogICAgICAgICAgICAgICAgICAgICAgaWYodWtbaV1bal09PTApIAogICAgICAgICAgICAgICAgICAgICAgICAgbm93W2ldKys7CiAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgICBub3dbal0rKzsKICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgYWRqW2ldW251bVtpXSsrXT1qOwogICAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB1PW47CiAgICAgICAgd2hpbGUodHJ1ZSkKICAgICAgICB7CiAgICAgICAgICAgICBpcD1zcGZhKCk7CiAgICAgICAgICAgICBpZihpcDwwKQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICBub3dbaXBdKys7CiAgICAgICAgICAgICB3aGlsZShwYVtpcF0+PTApCiAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgdWtbcGFbaXBdXVtpcF09MTsKICAgICAgICAgICAgICAgICB1a1tpcF1bcGFbaXBdXT0wOwogICAgICAgICAgICAgICAgIGlwPXBhW2lwXTsKICAgICAgICAgICAgIH0KICAgICAgICAgICAgIG5vd1tpcF0tLTsKICAgICAgICB9CiAgICAgICAgZm9yKGk9MDtpPG47aSsrKQogICAgICAgIHsKICAgICAgICAgICAgZGs9ZGVnW2ldK25vd1tpXTsKICAgICAgICAgICAgdG90LT1kayooZGstMSkvMjsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCIlZFxuIix0b3QpOwogICAgICAgIGZvcihpPTA7aTxuO2krKykKICAgICAgICB7CiAgICAgICAgICAgZm9yKGo9MDtqPG47aisrKQogICAgICAgICAgIHsKICAgICAgICAgICAgICAgaWYobWF0W2ldW2pdPT0nMicpCiAgICAgICAgICAgICAgICAgICBtYXRbaV1bal09dWtbaV1bal0rJzAnOwogICAgICAgICAgICAgICBwdXRjaGFyKG1hdFtpXVtqXSk7CiAgICAgICAgICAgICAgIHB1dGNoYXIoJyAnKTsKICAgICAgICAgICB9CiAgICAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9