Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define MODI 10000
#define bitcheck(a,b)   (a >> b) & 1
#define bitset(a,b)       a |= (1 << b)
#define bitunset(a,b)    a &= ~(1 << b)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

signed main() {
	int w,h;
	scanf("%d %d", &w, &h);
	vector<vector<int>> mp(w,vector<int>(h,-1));
	pii cha;
	vector<pii> homp;
	int homc = 0;
	REP(i, h) {
		REP(j, w) {
			int a = 0;
			scanf("%d", &a);
			if (a == 2) {
				cha = { j,i };
			}
			if (a == 1) {
				homp.push_back({ j,i });
				mp[j][i] = homc;
				homc++;
			}
		}
	}
	homp.push_back(cha);
	map<pii, int> dp[2];//?±\??´????????¨??°
	dp[0][{0, homc}] = 1;
	bool t = 0;
	REP(i,homc){
		for(auto cur : dp[t]) {
			pii curpos = homp[cur.first.second];
			int cnum;
			for (int j = curpos.second + 1; h > j; j++) {//???
				cnum = mp[curpos.first][j];
				if (cnum == -1)
					continue;
				if (bitcheck(cur.first.first, cnum))
					break;
				int buf = cur.first.first; bitset(buf, cnum);
				dp[1 - t][{buf, cnum}] += cur.second;
			}
			for (int j = curpos.second - 1; 0 <= j; j--) {//???
				cnum = mp[curpos.first][j];
				if (cnum == -1)
					continue;
				if (bitcheck(cur.first.first, cnum))
					break;
				int buf = cur.first.first; bitset(buf, cnum);
				dp[1 - t][{buf, cnum}] += cur.second;
			}
			for (int j = curpos.first + 1; w > j; j++) {//???
				cnum = mp[j][curpos.second];
				if (cnum == -1)
					continue;
				if (bitcheck(cur.first.first, cnum))
					break;
				int buf = cur.first.first; bitset(buf, cnum);
				dp[1 - t][{buf, cnum}] += cur.second;
			}
			for (int j = curpos.first - 1; 0 <= j; j--) {//???
				cnum = mp[j][curpos.second];
				if (cnum == -1)
					continue;
				if (bitcheck(cur.first.first, cnum))
					break;
				int buf = cur.first.first; bitset(buf, cnum);
				dp[1 - t][{buf, cnum}] += cur.second;
			}
		}
		dp[t].swap(map<pii,int>());
		t = 1 - t;
	}
	int cnum,ans = 0;
	set<int> targets;
	for (int j = cha.first; w > j; j++) {
		cnum = mp[j][cha.second];
		if (cnum > -1) {
			targets.insert(cnum);
			break;
		}
	}
	for (int j = cha.first; 0 <= j; j--) {
		cnum = mp[j][cha.second];
		if (cnum > -1) {
			targets.insert(cnum);
			break;
		}
	}
	for (int j = cha.second; h > j; j++) {
		cnum = mp[cha.first][j];
		if (cnum > -1) {
			targets.insert(cnum);
			break;
		}
	}
	for (int j = cha.second; 0 <= j; j--) {
		cnum = mp[cha.first][j];
		if (cnum > -1) {
			targets.insert(cnum);
			break;
		}
	}
	for (auto cur : dp[t]) {
		if(targets.find(cur.first.second) != targets.end())
			ans += cur.second;
	}
	printf("%d\n", ans);

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:95:28: error: cannot bind non-const lvalue reference of type 'std::map<std::pair<int, int>, int>&' to an rvalue of type 'std::map<std::pair<int, int>, int>'
   95 |                 dp[t].swap(map<pii,int>());
      |                            ^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/map:63,
                 from a.cc:10:
/usr/include/c++/14/bits/stl_map.h:1171:17: note:   initializing argument 1 of 'void std::map<_Key, _Tp, _Compare, _Alloc>::swap(std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >]'
 1171 |       swap(map& __x)
      |            ~~~~~^~~