Language: C++

#include<iostream>
#include<queue>
#include<vector>
#include <bitset>

using namespace std;
typedef pair<int, int> P;
int i, j, k;
int m, n;
int dp[25][1024 * 1024 * 16], town[11][11];
P p[25];
void bfs(int now, int flag);
int main() {
	while (cin >> m >> n&& m && n) {
		int s, count = 1;
		memset(dp, 0, sizeof(dp));
		for (i = 1; i <= n; i++) {
			for (j = 1; j <= m; j++) {
				cin >> town[i][j];
				if (town[i][j] == 2) {
					s = count;
				}
				if (town[i][j]) {
					town[i][j] = count;
					p[count] = make_pair(i, j);
					count++;
				}
			}
		}
		/*for (i = 1; i <= n;i++){
			for (j = 1; j <= m; j++) {
				cout << town[i][j];
			}
			cout << endl;
		}*/
		bfs(s, 0);
		j = 1;
		for (i = 0; i < count - 1; i++)
			j = j * 2;
		cout << dp[s][j - 1] << endl;
	}
}
void bfs(int start, int flag) {
	dp[start][flag] = 1;
	vector<P> que;
	que.push_back(make_pair(flag, start));
	while (!que.empty()) {
		//cout << que.size()<<' ';
		que.erase(unique(que.begin(), que.end()), que.end());
		sort(que.begin(), que.end());
		P pp;
		pp = que[0];
		start = pp.second; flag = pp.first;
		//cout << start << ' ' <<  static_cast<std::bitset<24> >(flag)  << endl;
		que.erase(que.begin());
		for (j = p[start].second; j <= m; j++) {
			if (town[p[start].first][j]&&j!=p[start].second) {
				if (flag & 1 << (town[p[start].first][j]-1)) {
					break;
				}
				else {
					int flag2 = flag | 1 << (town[p[start].first][j] - 1);
					dp[town[p[start].first][j]][flag2] += dp[town[p[start].first][p[start].second]][flag];
				//	cout << 1 << endl;
					que.push_back(make_pair(flag2, town[p[start].first][j]));
				}
			}
		}
		for (j = p[start].second; j >= 1; j--) {
			if (town[p[start].first][j]&&j != p[start].second) {
				if (flag & 1 << (town[p[start].first][j]- 1)) {
					break;
				}
				else {
					int flag2 = flag | 1 << (town[p[start].first][j] - 1);
					dp[town[p[start].first][j]][flag2] += dp[town[p[start].first][p[start].second]][flag];
					que.push_back(make_pair(flag2, town[p[start].first][j]));
				//	cout << 2 << endl;
				}
			}
		}
		for (j = p[start].first; j <= n; j++) {
			if (town[j][p[start].second]&& j != p[start].first) {
				if (flag & 1 << (town[j][p[start].second] - 1)) {
					break;
				}
				else {
					int flag2 = flag | 1 << (town[j][p[start].second] - 1);
					dp[town[j][p[start].second]][flag2] += dp[town[p[start].first][p[start].second]][flag];
					que.push_back(make_pair(flag2, town[j][p[start].second]));
				//	cout << 3 << endl;
				}
			}
		}
		for (j = p[start].first; j >= 1; j--) {
			if (town[j][p[start].second] && j != p[start].first) {
				if (flag & 1 << (town[j][p[start].second] - 1)) {
					break;
				}
				else {
					int flag2 = flag | 1 << (town[j][p[start].second] - 1);
					dp[town[j][p[start].second]][flag2] += dp[town[p[start].first][p[start].second]][flag];
					que.push_back(make_pair(flag2, town[j][p[start].second]));
					//cout << 4 << endl;
					//cout << j << ' ' << flag2 << ' ' << p[start].first << endl;
				}
			}
		}
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:16:17: error: 'memset' was not declared in this scope
   16 |                 memset(dp, 0, sizeof(dp));
      |                 ^~~~~~
a.cc:5:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    4 | #include <bitset>
  +++ |+#include <cstring>
    5 | 
a.cc: In function 'void bfs(int, int)':
a.cc:49:27: error: 'unique' was not declared in this scope
   49 |                 que.erase(unique(que.begin(), que.end()), que.end());
      |                           ^~~~~~
a.cc:50:17: error: 'sort' was not declared in this scope; did you mean 'short'?
   50 |                 sort(que.begin(), que.end());
      |                 ^~~~
      |                 short