#define F(i,n)for(i=n;i--;) m;d[200][202][100];e;z;k;l;i;j;x;y;main(n,a){scanf("%d%d",&n,&m);z=2*n;F(k,m)F(i,n)F(j,n)scanf("%d",&e),d[k][j][i]=d[k][n+i][j]=d[k][i-j?0:z][i]=d[k][n-i+~j?0:z+1][i]=e;F(k,m)F(l,k)F(i,z+2)F(j,z+2){e=z;F(x,n)F(y,n)e-=d[k][i][x]==d[l][j][y];a=e