#include<bits/stdc++.h>

using namespace std;
ifstream f1("NCONHAU.INP");
ofstream f2("NCONHAU.OUT");
int n,cot[100],cheochinh[100],cheophu[100],a[100],cv[100][100],check=0;

void xuat()
{ int i,j;
f2<<check<<endl;
 for (i=1;i<=n;i++)
   {for (j=1;j<=n;j++)
    {f2<<setw(3)<<cv[i][j]; } 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;
      cv[i][j]=i;
      if (i==n) {check++;  xuat();}
      else {hau(i+1);}  cv[i][j]=0; cot[j]=0; cheochinh[j-i]=0; cheophu[j+i]=0;}
	}
int main()
{ f1>>n;
 hau(1);
}
