#include <bits/stdc++.h>

using namespace std;

const int infi=32767;
const int tot=500;

struct edge{
	int to,next;
}e[tot*9+1];

bool link[tot+1][tot+1];
int box[tot+1];
int node_num,edge_num;

short dist[tot+1][tot+1][tot+1];
short action[tot+1][tot+1][tot+1][3];
short q[tot*tot*tot+1][3];

void addedge(int s,int t){
	link[s][t]=true;
	edge_num++;
	e[edge_num].to=t;
	e[edge_num].next=box[s];
	box[s]=edge_num;
}

void make_graph(){
	for (int i=1;i<=tot;i++){
		for (int j=1;j<=tot;j++)
			link[i][j]=false;
		box[i]=0;
	}
	scanf("%d",&node_num);
	edge_num=0;
	for (int i=1;i<=node_num;i++)
		for (int j=1;j<=node_num;j++){
			int x;
			scanf("%d",&x);
			if (x==1) addedge(i,j);
		}
}

bool cond1(int i,int j,int l){return link[i][l]||link[j][l];}

void calc(int num){
	int head=1,tail=0;
	for (int i=1;i<=node_num;i++)
		for (int j=1;j<=node_num;j++)
			for (int l=1;l<=node_num;l++){
				dist[i][j][l]=infi;
				if (cond1(i,j,l)){
					dist[i][j][l]=0;
					action[i][j][l][0]=l;
					action[i][j][l][1]=i;
					action[i][j][l][2]=j;
					tail++;
					q[tail][0]=l;
					q[tail][1]=i;
					q[tail][2]=j;
				}
			}
	while (head<=tail){
		int t=q[head][0],x=q[head][1],y=q[head][2];
		for (int l=box[t];l;l=e[l].next){
			bool ok=true;
			for (int i=box[e[l].to];i;i=e[i].next)
				if (dist[x][y][t]<dist[x][y][e[i].to]){
					ok=false;
					break;
				}
			if (!ok) continue;
			for (int i=box[x];i;i=e[i].next)
				for (int j=box[y];j;j=e[j].next)
					if (dist[e[i].to][e[j].to][e[l].to]==infi){
						dist[e[i].to][e[j].to][e[l].to]=dist[x][y][t]+1;
						action[e[i].to][e[j].to][e[l].to][0]=t;
						action[e[i].to][e[j].to][e[l].to][1]=x;
						action[e[i].to][e[j].to][e[l].to][2]=y;
						tail++;
						q[tail][0]=e[l].to;
						q[tail][1]=e[i].to;
						q[tail][2]=e[j].to;
					}
		}
		head++;
	}
	printf("%d %d %d ",num,tail,node_num*node_num*node_num);
	if (tail==node_num*node_num*node_num)
		printf("ok\n");
	else printf("not ok\n");
}

void output(){
	freopen("D.txt","w",stdout);
	for (int i=1;i<=node_num;i++)
		for (int j=1;j<=node_num;j++)
			for (int l=1;l<=node_num;l++)
				printf("%d\n",dist[i][j][l]);
}

int main(){
	freopen("adj_matrix.txt","r",stdin);
	make_graph();
	calc(0);
	output();
	return 0;
}
