#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (long long i = (a); i < (b); ++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int h,w;
int fld[10][10];
int sx,sy;
int num,ans;
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};

void dfs(int x,int y,int cnt) {
	REP(i,4) {
		int nx=x,ny=y;
		while (true) {
			nx+=dx[i],ny+=dy[i];
			if(nx<0||nx>=w||ny<0||ny>=h) break;
			if(fld[ny][nx]==1) {
				fld[ny][nx]=0;
				dfs(nx,ny,cnt-1);
				fld[ny][nx]=1;
				break;
			}
			else if(fld[ny][nx]==2&&cnt==0) {
				ans++;
				return;
			}
		}
	}
	return;
}

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

    while(true) {
	cin>>w>>h;
	if(w==0) break;
	num=ans=0;
	REP(i,h) {
		REP(j,w) {
			cin>>fld[i][j];
			if(fld[i][j]==2) sy=i,sx=j;
			else if(fld[i][j]==1) num++;
		}
	}

	dfs(sx,sy,num);

	cout<<ans<<endl;
    }

	return 0;
}

