Language: C++

#include <bits/stdc++.h>

template<typename T>
void fill_all(T& ar,const T& v){
	ar=v;
}
template<typename T,size_t N,typename U>
void fill_all(T(& ar)[N],const U& v){
	for(auto&i:ar){fill_all(i,v);}
}


int w,h;
const int goal = 23;
const int empty = 24;

int graph[25][4][25];//????????????
int map[10][10];

const bitmax = 1<<20;
//bit????????????
int dp[23][bitmax];
int func(int now, int bit){
	if(now==empty){return 0;}
	if(now==goal){
	#ifdef DEBUG
	std::cout<<"OUTg0:" << now <<' '<<bit<<':'<<((now==goal&&bit==0)?1:0)<<std::endl;
	#endif
		return ((now==goal&&bit==0)?1:0);
	}
	if(bit==0){
		for(auto& g:graph[now]){
			if(g[0] == goal){
	#ifdef DEBUG
	std::cout<<"OUTb0:" << now <<' '<<bit<<':'<<1<<std::endl;
	#endif
				return 1;
			}
		}
	#ifdef DEBUG
	std::cout<<"OUTb0:" << now <<' '<<bit<<':'<<0<<std::endl;
	#endif
		return 0;
	}
	
	if(bit<bitmax)if(dp[now][bit]>=0){return dp[now][bit];}
	int res = 0;
	for(int graph_num =0;graph_num<4;++graph_num){
		for(auto iter=graph[now][graph_num];*iter!=empty;++iter){
			if(bit&(1<<*iter)){
				res += func(*iter,bit&(~(1<<*iter)));
			}
			else{
				break;
			}
		}
	}
	#ifdef DEBUG
	std::cout<<"OUT:" << now <<' '<<bit<<':'<<res<<std::endl;
	for(int graph_num =0;graph_num<4;++graph_num){
		for(auto iter=graph[now][graph_num];*iter!=empty;++iter){
			std::cout << ' '<<*iter;
			if(bit&(1<<*iter)){std::cout<<"(OK:"<<(bit&(~(1<<*iter)))<<")";}
			else{break;}
		}
	}
	std::cout<< std::endl;
	#endif
	if(bit<bitmax)return dp[now][bit]=res;
	return res;
}
int main()
{
	for(;;){
	fill_all(dp,-1);
	fill_all(map,empty);
	fill_all(graph,empty);
	std::cin>>w>>h;if(w==0&&h==0){break;}
	int count = 0;
	for(auto y=0;y<h;++y)for(auto x=0;x<w;++x){
		std::cin >> map[y][x];
		if(map[y][x]==0){
			map[y][x]=empty;
			continue;
		}
		else if(map[y][x]==1){
			map[y][x]=count++;
		}
		else if(map[y][x]==2){
			map[y][x]=goal;
		}
			auto pushback=[](int*g, int v){
				while(*g!=empty){++g;}*g=v;
			};
			auto iter = graph[map[y][x]][0];
			for(int ox=x-1;ox>=0;--ox){
				if(empty>map[y][ox]){
	#ifdef DEBUG
	std::cout <<"PASSx "<<x<<' '<<y<<"<->"<<ox<<' '<<y<<' '<<map[y][ox] <<"\n";
	#endif
					*(iter++) = map[y][ox];
					pushback(graph[map[y][ox]][2],map[y][x]);
	#ifdef DEBUG
	std::cout <<"graph "<<*(iter-1) <<' '<<map[y][x]<<"\n";
	#endif
				}
			}
			iter = graph[map[y][x]][1];
			for(int oy=y-1;oy>=0;--oy){
				if(empty>map[oy][x]){
	#ifdef DEBUG
	std::cout <<"PASSy "<<x<<' '<<y<<"<->"<<x<<' '<<oy<<' '<<map[oy][x]<<"\n";
	#endif
					*(iter++) = map[oy][x];
					pushback(graph[map[oy][x]][3],map[y][x]);
	#ifdef DEBUG
	std::cout <<"graph "<<*(iter-1) <<' '<<map[y][x]<<"\n";
	#endif
				}
			}
	}
	int bit = 0;
	while(--count>=0){bit<<=1;bit|=1;}
	#ifdef DEBUG
	std::cout<<bit<<std::endl;
	for(auto& g:graph[goal])for(auto iter=g;iter!=g+5;++iter){
	std::cout << *iter << ' ';
	}std::cout<<std::endl;
	for(auto& g:graph[4])for(auto iter=g;iter!=g+5;++iter){
	std::cout << *iter << ' ';
	}std::cout<<std::endl;
	for(auto& g:graph[3])for(auto iter=g;iter!=g+5;++iter){
	std::cout << *iter << ' ';
	}std::cout<<std::endl;
	#endif
	int res = 0;
	for(auto& arr : graph[goal]){
		for(auto iter=arr;*iter!=empty;++iter){
			res+=func(*iter,bit&(~(1<<*iter)));
		}
	}
	std::cout << res << std::endl;
	
	}
}

Compiler output:
a.cc:20:7: error: 'bitmax' does not name a type
   20 | const bitmax = 1<<20;
      |       ^~~~~~
a.cc:22:12: error: 'bitmax' was not declared in this scope
   22 | int dp[23][bitmax];
      |            ^~~~~~
a.cc: In function 'int func(int, int)':
a.cc:46:16: error: 'bitmax' was not declared in this scope
   46 |         if(bit<bitmax)if(dp[now][bit]>=0){return dp[now][bit];}
      |                ^~~~~~
a.cc:46:26: error: 'dp' was not declared in this scope; did you mean 'dup'?
   46 |         if(bit<bitmax)if(dp[now][bit]>=0){return dp[now][bit];}
      |                          ^~
      |                          dup
a.cc:69:16: error: 'bitmax' was not declared in this scope
   69 |         if(bit<bitmax)return dp[now][bit]=res;
      |                ^~~~~~
a.cc:69:30: error: 'dp' was not declared in this scope; did you mean 'dup'?
   69 |         if(bit<bitmax)return dp[now][bit]=res;
      |                              ^~
      |                              dup
a.cc: In function 'int main()':
a.cc:75:18: error: 'dp' was not declared in this scope; did you mean 'dup'?
   75 |         fill_all(dp,-1);
      |                  ^~
      |                  dup