#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
ofstream fo("nconhau.out");
ifstream fi("nconhau.inp");
long int a[10000],dem=1;
bool Ok(int x2,int y2){
    for(int i = 1; i < x2 ;i++)
        if(a[i] == y2 || abs(i-x2) == abs(a[i] - y2) )
            return false;
    return true;
}

void Xuat(int n){
	fo<<dem<<endl;
	dem++;
    for(int i=1;i<=n;i++)
    {
      for (int j = 1;j<=n;j++)
        if (j != a[i]) fo<<0<<" ";
        else fo << i << " ";
        
       fo << endl;
    }
}

void Try(int i,int n){
    for(int j = 1;j<=n;j++){
        if(Ok(i,j)){
            a[i] = j;
            if(i==n) Xuat(n);
            Try(i+1,n);
        }
    }
}

int main(){
    long int n;
    fi>>n;
    Try(1,n);
    return 0;
}
