Language: C++

#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int h, w;
char p[102][103];
int label[102][102], labelNum;
vector<int> et[2][11000];

bool is_range(int y, int x) {
	return (0 <= y && y < h && 0 <= x && x < w);
}

void labeling() {
	int i, j, id = 0;
	bool used[102][102] = {false};
	
	rep(i, h) {
		rep(j, w) {
			if (used[i][j]) continue;
			//(i, j)????§???????????????????????????°?????????
			queue<P> que;
			que.push(P(i, j));
			while (!que.empty()) {
				int y = que.front().first;
				int x = que.front().second;
				que.pop();
				if (used[y][x]) continue;
				used[y][x] = true;
				label[y][x] = id;
				
				for (int dy = -1; dy <= 1; dy++) {
					for (int dx = -1; dx <= 1; dx++) {
						int ny = y + dy;
						int nx = x + dx;
						if (!is_range(ny, nx)) continue;
						if (p[y][x] == '.') {
							if (abs(dy) + abs(dx) != 1) continue;
							if (p[ny][nx] != '.') continue;
							que.push(P(ny, nx));
						}
						else {
							if (dy == 0 && dx == 0) continue;
							if (p[ny][nx] != '#') continue;
							que.push(P(ny, nx));
						}
					}
				}
			}
			id++;
		}
	}
	labelNum = id;
}

void make_graph(int type) {
	vector<vector<P>> pos;
	pos.resize(labelNum);
	
	int i, j;

	rep(i, h) rep(j, w) pos[label[i][j]].push_back(P(i, j));
	
	int dy[4] = {-1, 0, 1, 0};
	int dx[4] = {0, 1, 0, -1};
	bool used[11000] = {false};
	queue<int> que;
	
	que.push(0);
	used[0] = true;
	
	while (!que.empty()) {
		int id = que.front(); que.pop();
		
		rep(i, pos[id].size()) {
			rep(j, 4) {
				int y = pos[id][i].first + dy[j];
				int x = pos[id][i].second + dx[j];
				if (!is_range(y, x)) continue;
				if (used[label[y][x]]) continue;
				
				que.push(label[y][x]);
				used[label[y][x]] = true;
				et[type][id].push_back(label[y][x]);
			}
		}
	}
}

vector<int> dfs(int v, int type) {
	vector<int> ret;
	if (et[type][v].size() == 0) { ret.push_back(0); return ret; }
	
	vector<vector<int> > res;
	for (int i = 0; i < et[type][v].size(); i++) {
		res.push_back(dfs(et[type][v][i], type));
	}
	sort(res.begin(), res.end());
	
	for (int i = 0; i < res.size(); i++) {
		for (int j = 0; j < res[i].size(); j++) {
			ret.push_back(res[i][j]);
		}
	}
	ret.push_back(et[type][v].size());
	
	return ret;
}

bool _is_same(vector<int> a, vector<int> b) {
	if (a.size() != b.size()) return false;
	for (int i = 0; i < a.size(); i++) {
		if (a[i] != b[i]) return false;
	}
	return true;
}

int main() {
	int i, j;
	
	while (cin >> h >> w) {
		if (!h) break;
		rep(i, h + 2) rep(j, w + 2) p[i][j] = '.';
		rep(i, h) rep(j, w) cin >> p[i + 1][j + 1];
		h += 2;
		w += 2;
		labeling();
		make_graph(0);
		
		cin >> h >> w;
		rep(i, h + 2) rep(j, w + 2) p[i][j] = '.';
		rep(i, h) rep(j, w) cin >> p[i + 1][j + 1];
		h += 2;
		w += 2;
		labeling();
		make_graph(1);
		
		vector<int> list[2];
		list[0] = dfs(0, 0);
		list[1] = dfs(0, 1);
		if (_is_same(list[0], list[1])) { cout << "yes" << endl; }
		else { cout << "no" << endl; }
		
		rep(i, 11000) et[0][i].clear();
		rep(i, 11000) et[1][i].clear();
	}
	return 0;
}

Compiler output:
a.cc: In function 'std::vector<int> dfs(int, int)':
a.cc:102:9: error: 'sort' was not declared in this scope; did you mean 'sqrt'?
  102 |         sort(res.begin(), res.end());
      |         ^~~~
      |         sqrt