#include<iostream>
#include<fstream>
using namespace std;
ifstream f1("NCONHAU.INP");
ofstream f2("NCONHAU.OUT");
int n,cot[1000],cheochinh[1000],cheophu[1000],a[1000],cv[100][100],check=0;
void kq()
{int i,so=0;
f2<<check<<endl;
 for (i=1;i<=n;i++)
  {so++;
   cv[i][a[i]]= cv[i][a[i]]+so;}}
void xuat()
{ int i,j;
 for (i=1;i<=n;i++)
   {for (j=1;j<=n;j++)
    {f2<<"  "<<cv[i][j]; cv[i][j]=0;} f2<<endl;}
}
void hau(int i)
{int j;
  for (j=1;j<=n;j++)
  if (cot[j]==0&&cheochinh[j-i]==0&&cheophu[j+i]==0)
    { a[i]=j;
      cot[j]=1;
      cheochinh[j-i]=1;
      cheophu[j+i]=1;
      if (i==n) {check++; kq(); xuat();}
      else {hau(i+1);} cot[j]=0; cheochinh[j-i]=0; cheophu[j+i]=0;}
	}
int main()
{ f1>>n;
 hau(1);
}

