#include <bits/stdc++.h>
#define inf 1e9
using namespace std;
int dist[2010];
vector<int> adj[2010];
bool bfs( int x )
{
queue<int>q;
q.push(x);
memset( dist, -1, sizeof(dist) );
dist[x]= 0;
while(!q.empty())
{
int u= q.front();
q.pop();
for( int i=0; i<adj[u].size(); i++ )
{
int v= adj[u][i];
if( dist[v]==-1 )
{
dist[v]= dist[u]+1;
q.push(v);
}
else if( dist[v]!=-1 and v==x )
{
dist[v]= dist[u]+1;
return 1;
}
}
}
return 0;
}
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int p;
scanf("%d", &p);
if(p)
adj[i].push_back(j);
}
for( int i=0; i<n; i++ )
{
if( bfs(i) )
printf("%d\n", dist[i]);
else
printf("NO WAY\n");
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgaW5mIDFlOQp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGRpc3RbMjAxMF07CnZlY3RvcjxpbnQ+IGFkalsyMDEwXTsKCmJvb2wgYmZzKCBpbnQgeCApCnsKICAgIHF1ZXVlPGludD5xOwogICAgcS5wdXNoKHgpOwoKICAgIG1lbXNldCggZGlzdCwgLTEsIHNpemVvZihkaXN0KSApOwogICAgZGlzdFt4XT0gMDsKCiAgICB3aGlsZSghcS5lbXB0eSgpKQogICAgewogICAgICAgIGludCB1PSBxLmZyb250KCk7CiAgICAgICAgcS5wb3AoKTsKCiAgICAgICAgZm9yKCBpbnQgaT0wOyBpPGFkalt1XS5zaXplKCk7IGkrKyApCiAgICAgICAgewogICAgICAgICAgICBpbnQgdj0gYWRqW3VdW2ldOwoKICAgICAgICAgICAgaWYoIGRpc3Rbdl09PS0xICkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZGlzdFt2XT0gZGlzdFt1XSsxOwogICAgICAgICAgICAgICAgcS5wdXNoKHYpOwogICAgICAgICAgICB9CgogICAgICAgICAgICBlbHNlIGlmKCBkaXN0W3ZdIT0tMSBhbmQgdj09eCApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGRpc3Rbdl09IGRpc3RbdV0rMTsKICAgICAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiAwOwp9CgppbnQgbWFpbigpCnsKICAgIGludCBuOwogICAgY2luPj5uOwoKICAgIGZvcihpbnQgaT0wOyBpPG47IGkrKykKICAgICAgICBmb3IoaW50IGo9MDsgajxuOyBqKyspCiAgICAgICAgewogICAgICAgICAgICBpbnQgcDsKICAgICAgICAgICAgc2NhbmYoIiVkIiwgJnApOwoKICAgICAgICAgICAgaWYocCkKICAgICAgICAgICAgICAgIGFkaltpXS5wdXNoX2JhY2soaik7CiAgICAgICAgfQoKICAgIGZvciggaW50IGk9MDsgaTxuOyBpKysgKQogICAgewogICAgICAgIGlmKCBiZnMoaSkgKQogICAgICAgICAgICBwcmludGYoIiVkXG4iLCBkaXN0W2ldKTsKICAgICAgICBlbHNlCiAgICAgICAgICAgIHByaW50ZigiTk8gV0FZXG4iKTsKICAgIH0KfQo=