#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int fi[11][11];

int n, m;
int res, tot, sx, sy;

void rec(int x, int y, int con) {
    ++con;
    if (con == tot) {
        if (x == sx || y == sy) ++res;
        return;
    }
        
    fi[x][y] = 3;
    for (int i = 0; i < 4; ++i) {
        int nx = x, ny = y;
        while (true) {
            nx += dx[i], ny += dy[i];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m) break;
            if (fi[nx][ny] == 1) {
                rec(nx, ny, con);
                break;
            }
        }
    }
    fi[x][y] = 1;
}

int main() {
    while (scanf("%d%d", &m, &n), n) {
        if (m == 0) break;
        tot = 0;
        for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
            scanf("%d", &fi[i][j]);
            if (fi[i][j] == 1) ++tot;
            if (fi[i][j] == 2) sx = i, sy = j;
        }
        res = 0;
        rec(sx, sy, -1);
        
        printf("%d\n", res);
    }
    return 0;
}
