#include <iostream>
#include <vector>
using namespace std;

int count = 0; // 记录解的个数

// 检查当前位置是否可以放置皇后
bool isSafe(vector<vector<int>>& board, vector<int>& blackQueens, vector<int>& whiteQueens, int row, int col, bool isBlack) {
    // 检查棋盘位置是否为 0
    if (board[row][col] == 0) {
        return false;
    }

    // 检查是否与已放置的黑皇后冲突
    if (isBlack) {
        for (int i = 0; i < row; i++) {
            if (blackQueens[i] == col || abs(blackQueens[i] - col) == abs(i - row)) {
                return false;
            }
        }
    }
    // 检查是否与已放置的白皇后冲突
    else {
        for (int i = 0; i < row; i++) {
            if (whiteQueens[i] == col || abs(whiteQueens[i] - col) == abs(i - row)) {
                return false;
            }
        }
    }

    return true;
}

// 回溯函数
void solve(vector<vector<int>>& board, vector<int>& blackQueens, vector<int>& whiteQueens, int row, int n) {
    if (row == n) {
        // 所有皇后都放置完成，记录一个解
        count++;
        return;
    }

    // 尝试在当前行的每一列放置黑皇后和白皇后
    for (int blackCol = 0; blackCol < n; blackCol++) {
        if (isSafe(board, blackQueens, whiteQueens, row, blackCol, true)) {
            blackQueens[row] = blackCol; // 放置黑皇后
            for (int whiteCol = 0; whiteCol < n; whiteCol++) {
                if (whiteCol != blackCol && isSafe(board, blackQueens, whiteQueens, row, whiteCol, false)) {
                    whiteQueens[row] = whiteCol; // 放置白皇后
                    solve(board, blackQueens, whiteQueens, row + 1, n); // 递归放置下一行
                    whiteQueens[row] = -1; // 回溯：撤销白皇后的选择
                }
            }
            blackQueens[row] = -1; // 回溯：撤销黑皇后的选择
        }
    }
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> board(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> board[i][j];
        }
    }

    vector<int> blackQueens(n, -1); // 记录每行黑皇后的列位置
    vector<int> whiteQueens(n, -1); // 记录每行白皇后的列位置

    solve(board, blackQueens, whiteQueens, 0, n);

    cout << count << endl;
    return 0;
}
