#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"

using namespace std;

constexpr long long int MOD = 1000000007;
//const int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;

//const double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

int dir[] = { 1,0,-1,0,1 };

void Search(const vector<vector<int>>&v, const vector<vector<int>>&b, const vector<vector<int>>&l, const vector<vector<int>>&r, const vector<vector<int>>&d, const vector<vector<int>>&u, int x, int y, int use, int &ans) {
	//cout << y << " " << x << " " << use << endl;
	if (x) {
		int cy = y, cx = l[y][x - 1];
		while (cy >= 0 && cx >= 0 && cy < v.size() && cx < v[0].size()) {
			if (v[cy][cx] == 1) {
				if (((use >> b[cy][cx]) & 1) == 0) {
					if (cx) {
						cx = l[cy][cx - 1];
					}
					else break;
					continue;
				}
				Search(v, b, l, r, d, u, cx, cy, use ^ (1 << b[cy][cx]), ans);
				break;
			}
			if (v[cy][cx] == 2 && !use) {
				ans++;
				break;
			}
			if (cx) {
				cx = l[cy][cx - 1];
			}
			else break;
		}
	}
	if (x + 1 < W) {
		int cy = y, cx = r[y][x + 1];
		while (cy >= 0 && cx >= 0 && cy < v.size() && cx < v[0].size()) {
			if (v[cy][cx] == 1) {
				if (((use >> b[cy][cx]) & 1) == 0) {
					if (cx + 1 < W) {
						cx = r[cy][cx + 1];
					}
					else break;
					continue;
				}
				Search(v, b, l, r, d, u, cx, cy, use ^ (1 << b[cy][cx]), ans);
				break;
			}
			if (v[cy][cx] == 2 && !use) {
				ans++;
				break;
			}
			if (cx + 1 < W) {
				cx = r[cy][cx + 1];
			}
			else break;
		}
	}
	if (y) {
		int cy = d[y - 1][x], cx = x;
		while (cy >= 0 && cx >= 0 && cy < v.size() && cx < v[0].size()) {
			if (v[cy][cx] == 1) {
				if (((use >> b[cy][cx]) & 1) == 0) {
					if (cy) {
						cy = d[cy - 1][cx];
					}
					else break;
					continue;
				}
				Search(v, b, l, r, d, u, cx, cy, use ^ (1 << b[cy][cx]), ans);
				break;
			}
			if (v[cy][cx] == 2 && !use) {
				ans++;
				break;
			}
			if (cy) {
				cy = d[cy - 1][cx];
			}
			else break;
		}
	}
	if (y + 1 < H) {
		int cy = u[y + 1][x], cx = x;
		while (cy >= 0 && cx >= 0 && cy < v.size() && cx < v[0].size()) {
			if (v[cy][cx] == 1) {
				if (((use >> b[cy][cx]) & 1) == 0) {
					if (cy + 1 < H) {
						cy = u[cy + 1][cx];
					}
					else break;
					continue;
				}
				Search(v, b, l, r, d, u, cx, cy, use ^ (1 << b[cy][cx]), ans);
				break;
			}
			if (v[cy][cx] == 2 && !use) {
				ans++;
				break;
			}
			if (cy + 1 < H) {
				cy = u[cy + 1][cx];
			}
			else break;
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> W >> H;
	while(W){
	vector<vector<int>>v(H, vector<int>(W));
	vector<vector<int>>b(H, vector<int>(W));
	vector<vector<int>>l(H, vector<int>(W, -1));
	vector<vector<int>>r(H, vector<int>(W, -1));
	vector<vector<int>>d(H, vector<int>(W, -1));
	vector<vector<int>>u(H, vector<int>(W, -1));
	int cnt = 0;
	for (auto &i : v)for (auto &j : i)cin >> j;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (v[i][j] == 1)b[i][j] = cnt++;
		}
	}
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (v[i][j]) {
				l[i][j] = j;
				r[i][j] = j;
				d[i][j] = i;
				u[i][j] = i;
			}
		}
	}
	for (int i = 0; i < H; i++) {
		for (int j = 1; j < W; j++)if (l[i][j] == -1)l[i][j] = l[i][j - 1];
		for (int j = W - 2; j >= 0; j--)if (r[i][j] == -1)r[i][j] = r[i][j + 1];
	}
	for (int j = 0; j < W; j++) {
		for (int i = 1; i < H; i++)if (d[i][j] == -1)d[i][j] = d[i - 1][j];
		for (int i = H - 2; i >= 0; i--)if (u[i][j] == -1)u[i][j] = u[i + 1][j];
	}
	int sy, sx;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (v[i][j] == 2)sy = i, sx = j;
		}
	}
	int ans = 0;
	Search(v, b, l, r, d, u, sx, sy, (1 << cnt) - 1, ans);
	cout << ans << endl;
	cin>>W>>H;
	}
}


Measured memory_bytes: 3092