import std.stdio;
import std.algorithm;
import core.stdc.stdio;
import core.memory;

void main(){
	while(true){
	GC.collect;

	int[][4][23] nextTown;
	int[4] beTown;
	beTown[] = -1;
	int[10][10] mapData;
	int[] dx = [0,-1,0,1];
	int[] dy = [-1,0,1,0];
	int w,h;
	scanf("%d%d",&w,&h);
	if(w==0&&h==0)
		break;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			scanf("%d",&mapData[i][j]);
		}
	}
	int tc=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(mapData[i][j] == 0){
				mapData[i][j] = 255;
			}else if(mapData[i][j] == 1){
				mapData[i][j] = tc++;
			}else{
				mapData[i][j] = 114;
			}
		}
	}
	bool[] sl = new bool[tc];
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(mapData[i][j] < 23){
				for(int k=0;k<4;k++){
					int ny=i+dy[k],nx=j+dx[k];
					while(0<=nx&&nx<w&&0<=ny&&ny<h){
						if(mapData[ny][nx] < 23)
							nextTown[mapData[i][j]][k] ~= mapData[ny][nx];
						ny += dy[k];
						nx += dx[k];
					}
				}
			}
			if(mapData[i][j] == 114){
				for(int k=0;k<4;k++){
					int ny=i+dy[k],nx=j+dx[k];
					while(0<=nx&&nx<w&&0<=ny&&ny<h){
						if(mapData[ny][nx] < 23){
							if(beTown[k] == -1)
								beTown[k] = mapData[ny][nx];
							sl[mapData[ny][nx]]=true;
						}
						ny += dy[k];
						nx += dx[k];
					}
				}
			}
		}
	}
	int[int] now;
	for(int k=0;k<4;k++){
		if(beTown[k] >= 0){
			now[(beTown[k]<<tc)|(1<<beTown[k])] = 1;
		}
	}
	for(int _=1;_<tc;_++){
		int[int] next;
		foreach(int state,int num;now){
			for(int k=0;k<4;k++){
				foreach(s;nextTown[state>>tc][k]){
					if((state&(1<<s))==0){
						int idx = (s<<tc)|(1<<s)|(state&((1<<tc)-1));
						int* np = idx in next;
						if(np == null)
							next[idx] = num;
						else
							(*np)+=num;
						break;
					}
				}
			}
		}
		swap(now,next);
	}
	int ans=0;
	foreach(int state,int num;now){
		if(sl[state>>tc]){
			ans += num;
		}
	}
	printf("%d\n",ans);
	
	}
}