import std.stdio;
import std.array;
import std.conv;
import std.typecons;
import std.container;
import std.range;

alias Tuple!(int,"y",int,"x") p;

struct w{
	uint visited;
	int last;
	
	this(int a,uint b){
		last = a;
		visited = b;
	}
	
	int opCmp(w rhs){
		if(visited != rhs.visited)
			return 2 * (visited > rhs.visited) - 1;
		if(last != rhs.last)
			return 2 * (visited > rhs.visited) - 1;
		return 0;
	}
}

void main(){
	while(true){
	uint mask[23];
	for(int i=0;i<23;++i){
		mask[i] = 1U<<i;
	}
	
	auto inmn = readln().split();
	int m = to!int(inmn[0]);
	int n = to!int(inmn[1]);
	if(m==0&&n==0)
		break;
	p[] houses;
	p church;
	
	for(int i=0;i<n;++i){
		auto inmp = readln().split();
		for(int j=0;j<m;++j){
			if(to!int(inmp[j]) == 1){
				houses ~= p(i,j);
			}
			if(to!int(inmp[j]) == 2){
				church = p(i,j);
			}
		}
	}
	
	int hn = to!int(houses.length);
	houses ~= church;
	int[4][24] nearhouse = -1;
	
	foreach(int i,p h;houses){
		int[4] nd = 10000;
		foreach(int j,p g;houses){
			if(i != j && j != hn){
				if(h.x == g.x){
					if(h.y>g.y){
						if(nd[0] > h.y-g.y){
							nd[0] = h.y-g.y;
							nearhouse[i][0] = j;
						}
					}
					if(h.y<g.y){
						if(nd[2] > g.y-h.y){
							nd[2] = g.y-h.y;
							nearhouse[i][2] = j;
						}
					}
				}
				if(h.y == g.y){
					if(h.x>g.x){
						if(nd[3] > h.x-g.x){
							nd[3] = h.x-g.x;
							nearhouse[i][3] = j;
						}
					}
					if(h.x<g.x){
						if(nd[1] > g.x-h.x){
							nd[1] = g.x-h.x;
							nearhouse[i][1] = j;
						}
					}
				}
			}
		}
	}
	
	SList!w list1;
	auto now = &list1;
	SList!w list2;
	auto next = &list2;
	
	now.insert(w(hn,0U));
	
	int res = 0;
	
	for(int i=0;i<hn;++i){
		foreach(w k;*now){
			for(int j=0;j<4;++j){
				if(nearhouse[k.last][j] != -1){
					int d = nearhouse[k.last][j];
					while(mask[d] & k.visited){
						d = nearhouse[d][j];
						if(d == -1)
							break;
					}
					if(d != -1){
						next.insert(w(d,mask[d] | k.visited));
						if((mask[d] | k.visited) == (1U<<hn)-1){
							if(houses[d].x == church.x || houses[d].y == church.y)
								res++;
						}
					}
				}
			}
		}
		now.clear();
		auto tmp = now;
		now = next;
		next = tmp;
	}
	
	writeln(res);
}
}