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 <bitset>
#include <stack>
#include <cstdlib>
#include <complex>
#include <unordered_map>
#define rep(i,s,n) for(int i = s; n > i; i++)
#define REP(i,n) rep(i,0,n)
#define MODU 33
#define Range(x,a,b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef vector<int> Ivec;
typedef pair<int, int> pii;
typedef long long int ll;
const pii Dir[8] = { //?????????
	{ 0,1 },{ 0,-1 },{ 1,0 },{ -1,0 },
	{ 1,1 },{ 1,-1 },{ -1,-1 },{ -1,1 }
};
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*)array, (T*)(array + N), val);
}
int main() {
	int r;
	scanf("%d", &r);
	int w, h, x, y;
	scanf("%d %d %d %d", &w, &h, &x, &y);
	x--; y--;
	vector<vector<int>> m(h, vector<int>(w));
	REP(i, h) {
		REP(j, w) {
			scanf("%d", &m[i][j]);
		}
	}
	int w2, h2, x2, y2;
	scanf("%d %d %d %d", &w2, &h2, &x2, &y2);
	x2--; y2--;
	vector<vector<int>> m2(h2, vector<int>(w2));

	REP(i, h2) {
		REP(j, w2) {
			scanf("%d", &m2[i][j]);
		}
	}
	priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> que;

	que.push({ 1,{x,y} });

	map<int, int> mcount;
	vector<vector<int>> al(h, vector<int>(w));
	while (que.size()) {
		int  p = que.top().first;
		pii cur = que.top().second;
		que.pop();
		if (al[cur.first][cur.second])
			continue;
		al[cur.first][cur.second] = 1;
		mcount[p]++;

		REP(i, 4) {
			pii next = { cur.first + Dir[i].first, cur.second + Dir[i].second };
			if (Range(next.first, 0, h - 1) && Range(next.second, 0, w - 1)) {
				if (!al[next.first][next.second]) {
					que.push({ max(p,m[next.first][next.second]),next });
					//al[next.first][next.second] = 1;
				}
			}
		}
	}
	vector<pii> cou;//???????????°?????¬??????
	for (auto cur : mcount) {
		cou.push_back({ cur.second,cur.first });
	}
	rep(i, 1, cou.size()) {
		if (cou[i].first == cou[i - 1].first) {
			if (cou[i].second > cou[i - 1].second)
				cou.erase(cou.begin() + i);
			else
				cou.erase(cou.begin() + i - 1);
			i--;
		}
	}

	mcount.clear();
	al.swap(vector<vector<int>>(h2,vector<int>(w2)));
	que.push({ 1,{x2,y2} });
	while (que.size()) {
		int  p = que.top().first;
		pii cur = que.top().second;
		que.pop();
		if (al[cur.first][cur.second])
			continue;
		al[cur.first][cur.second] = 1;
		mcount[p]++;

		REP(i, 4) {
			pii next = { cur.first + Dir[i].first, cur.second + Dir[i].second };
			if (Range(next.first, 0, h2 - 1) && Range(next.second, 0, w2 - 1)) {
				if (!al[next.first][next.second]) {
					que.push({ max(p,m2[next.first][next.second]),next });
					//al[next.first][next.second] = 1;
				}
			}
		}
	}
	vector<pii> cou2;
	for (auto cur : mcount) {
		cou2.push_back({ cur.second,cur.first });
	}
	rep(i, 1, cou2.size()) {
		if (cou2[i].first == cou2[i - 1].first) {
			if (cou2[i].second > cou2[i - 1].second)
				cou2.erase(cou2.begin() + i);
			else
				cou2.erase(cou2.begin() + i - 1);
			i--;
		}
	}

	int Min = INT_MAX;
	REP(i, cou.size()) {
		auto itr = upper_bound(ALL(cou2) ,make_pair(cou[i].first,0));
		if (itr != cou2.end()) {
			Min = min(Min,itr->second + cou[i].second);
		}
	}
	printf("%d\n", Min);
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:99:17: error: cannot bind non-const lvalue reference of type 'std::vector<std::vector<int> >&' to an rvalue of type 'std::vector<std::vector<int> >'
   99 |         al.swap(vector<vector<int>>(h2,vector<int>(w2)));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_vector.h:1588:20: note:   initializing argument 1 of 'void std::vector<_Tp, _Alloc>::swap(std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >]'
 1588 |       swap(vector& __x) _GLIBCXX_NOEXCEPT
      |            ~~~~~~~~^~~