#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
#include <iomanip>
#include <unordered_map>
using namespace std;
typedef long long LL;

unordered_map<int,int> mem[23];
int n,m;
int table[10][10];
int search(int i, int j, int done)
{
	if(table[i][j]>=0&&mem[table[i][j]].find(done)!=mem[table[i][j]].end()){
		return mem[table[i][j]][done];
	}
	int ret=0;
	int dx=1;
	int dy=0;
	for(int k=0;k<4;k++,swap(dx,dy),dx=-dx){
		int x=i+dx;
		int y=j+dy;
		while(0<=x&&x<n&&0<=y&&y<m){
			if(table[x][y]>=0){
				if(done&(1<<table[x][y])){
					ret+=search(x,y,done^(1<<table[x][y]));
					break;
				}
			}
			else if(done==0&&table[x][y]==-1){
				ret++;
				break;
			}
			x+=dx;
			y+=dy;
		}
	}
	if(table[i][j]>=0){
		mem[table[i][j]][done]=ret;
	}
	return ret;
}
int main() {
while(1){
	cin >> m >> n;
	if(m==0)return 0;
	int s=0,t=0;
	int c=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			int v;
			cin >> v;
			if(v==1){
				table[i][j]=c++;
			}
			else if(v==2)
			{
				s=i;
				t=j;
				table[i][j]=-1;
			}
			else{
				table[i][j]=-2;
			}
		}
	}
	int ret=search(s,t,(1<<c)-1);
	for(int i=0;i<c;i++){
		mem[i].clear();
	}
	cout << ret << endl;
}
}