#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<random>
#include<functional>
#include<utility>

typedef long long ll;

#define rep(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define P pair<int, int>
#define PLI pair<ll, int>
#define PLL pair<ll, ll>
#define VI vector<int>
#define VII vector<VI>

using namespace std;


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

int N, M, I, J, num;
VII mp;

int solve(int x, int y, int cnt) {
	if (cnt == 0) {
		if (x == I || y == J) return 1;
	}

	int res = 0;
	rep(i, 4) {
		int nx = x, ny = y;
		while (true)
		{
			nx += dx[i], ny += dy[i];
			if (mp[nx][ny] == -1) break;
			if (mp[nx][ny] == 1) {
				mp[nx][ny] = 0;
				res += solve(nx, ny, cnt-1);
				mp[nx][ny] = 1;
				break;
			}
		}
	}

	return res;
}

int main() {
	while (cin >> M >> N, M)
	{
		mp = VII(N + 2, VI(M + 2, -1));
		num = 0;
		for(int i=1; i<=N; i++) {
			for(int j=1; j<=M; j++) {
				cin >> mp[i][j];
				if (mp[i][j] == 1) num++;
				else if (mp[i][j] == 2) {
					I = i; J = j;
				}
			}
		}

		cout << solve(I, J, num) << endl;
	}

	return 0;
}

