#include <stdbool.h>
#include <stdio.h>
int n;
int a[10][10];
int sum;
bool black[10], white[10];
void putQueen(int line)
{
if (line >= n)
{
sum++;
return;
}
for (int i = 0; i < n; i++)
{
if (black[i] || !a[line][i])
continue;
bool flag = true;
for (int x = line - 1, y = i - 1; x >= 0 && y >= 0; x--, y--)
if (a[x][y] == 2)
{
flag = false;
break;
}
if (!flag)
continue;
for (int x = line - 1, y = i + 1; x >= 0 && y < n; x--, y++)
{
if (a[x][y] == 2)
{
flag = false;
break;
}
}
if (!flag)
continue;
black[i] = true;
a[line][i] = 2;
for (int j = 0; j < n; j++)
{
if (i == j)
continue;
if (white[j] || !a[line][j])
continue;
bool flag = true;
for (int x = line - 1, y = j - 1; x >= 0 && y >= 0; x--, y--)
{
if (a[x][y] == 3)
{
flag = false;
break;
}
}
if (!flag)
continue;
for (int x = line - 1, y = j + 1; x >= 0 && y < n; x--, y++)
{
if (a[x][y] == 3)
{
flag = false;
break;
}
}
if (!flag)
continue;
white[j] = true;
a[line][j] = 3;
putQueen(line + 1);
a[line][j] = 1;
white[j] = false;
}
black[i] = false;
a[line][i] = 1;
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
putQueen(0);
printf("%d", sum);
return 0;
}