#include <cstdio>
#include <cstring>
#define NMax 20
#define LMax 6
using namespace std;

const char IN[]="avioane.in",OUT[]="avioane.out";

int N,Rez;
int a[NMax][NMax];
int R[NMax][NMax];
int av[4][LMax][LMax];

void rotate(int a[][LMax])
{
	int i,j;
	static int v[LMax][LMax];
	memset(v,0,sizeof(v));
	for (i=0;i<5;++i)
		for (j=0;j<5;++j)
			v[j][5-i-1]=a[i][j];
	memcpy(a,v,sizeof(v));
}

void init()
{
	int i;
	av[0][0][2]=1;
	av[0][1][0]=av[0][1][1]=av[0][1][2]=av[0][1][3]=av[0][1][4]=1;
	av[0][2][2]=1;
	av[0][3][1]=av[0][3][2]=av[0][3][3]=1;
	av[0][4][2]=1;

	for (i=1;i<4;++i)
	{
		memcpy(av[i],av[i-1],sizeof(av[i]));
		rotate(av[i]);
	}
}

bool test(int x,int y,int av[][LMax])
{
	int i,j;
	for (i=0;i<5;++i)
		for (j=0;j<5;++j)
			if (av[i][j]==1 && a[x+i][y+j]!=0)
				return false;
	return true;
}

void fill(int x,int y,int av[][LMax],int v)
{
	int i,j;
	for (i=0;i<5;++i)
		for (j=0;j<5;++j) if (av[i][j])
			a[x+i][y+j]=v;
}

bool bkt(int x=0)
{
	int i,j,k;
	if (x>Rez)
	{
		Rez=x;
		memcpy(R,a,sizeof(a));
		if (x==4) return true;
	}
	for (i=0;i<N-4;++i)
		for (j=0;j<N-4;++j)
			for (k=0;k<4;++k)
				if (test(i,j,av[k]))
				{
					fill(i,j,av[k],x+1);
					if (bkt(x+1)) return true;
					fill(i,j,av[k],0);
				}
	return false;
}

int main()
{
	int i,j;
	freopen(IN,"r",stdin);
	scanf("%d",&N);
	for (i=0;i<N;++i)
		for (j=0;j<N;++j)
			scanf("%d",&a[i][j]);
	fclose(stdin);

	init();
	bkt();

	freopen(OUT,"w",stdout);
	printf("%d\n",Rez);
	for (i=0;i<N;++i){
		for (j=0;j<N;++j)
			printf("%d ",R[i][j]);
		printf("\n");
	}
	fclose(stdout);

	return 0;
}
