#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int, char>P;

int f[10][10], x[23], y[23], dat[10][10], dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 };

int main() {
	int m, n;
	while (scanf("%d%d", &m, &n), m) {
		int p = 0, sx, sy;
		rep(i, n)rep(j, m) {
			scanf("%d", &f[i][j]);
			if (f[i][j] == 1)x[p] = i, y[p] = j, dat[i][j] = p++;
			if (f[i][j] == 2)sx = i, sy = j;
		}
		map<P, int>dp;
		rep(k, 4) {
			for (int nx = sx, ny = sy; 0 <= nx&&nx < n && 0 <= ny&&ny < m; nx += dx[k], ny += dy[k]) {
				if (f[nx][ny] == 1) {
					dp[{1 << dat[nx][ny], dat[nx][ny]}] = 1;
					break;
				}
			}
		}
		int ans = 0;
		rep(i, 1 << p)rep(j, p) {
			if (dp.find({ i,j }) == dp.end())continue;
			if (i == (1 << p) - 1) {
				if (x[j] == sx || y[j] == sy)ans += dp[{i, j}];
				continue;
			}
			rep(k, 4) {
				for (int nx = x[j], ny = y[j]; 0 <= nx&&nx < n && 0 <= ny&&ny < m; nx += dx[k], ny += dy[k]) {
					if (f[nx][ny] == 1 && !(i >> dat[nx][ny] & 1)) {
						dp[{i | 1 << dat[nx][ny], dat[nx][ny]}] += dp[{i, j}];
						break;
					}
				}
			}
			dp.erase({ i,j });
		}
		printf("%d\n", ans);
	}
}