Language: C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int h, w;
int c_y, c_x, d_y, d_x;
vector<string> v;
int color[1005][1005];
bool visit[1005][1005];
bool visit_col[1005];
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

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

void dfs(int y, int x, int col) {
	color[y][x] = col;
	for (int i = 0; i < 4; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];
		if (in_range(ny, nx) && !color[ny][nx] && v[ny][nx] == '.') {
			dfs(ny, nx, col);
		}
	}
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> h >> w;
	cin >> c_y >> c_x >> d_y >> d_x;
	c_y--, c_x--, d_y--, d_x--;
	v.resize(h);
	for (auto &i : v) cin >> i;

	int cnt = 0;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			if (!color[i][j] && v[i][j] == '.') {
				dfs(i, j, ++cnt);
			}
		}
	}

	queue<tuple<int, int, int>> q;  // y, x, count
	q.emplace(c_y, c_x, 0);
	visit_col[color[c_y][c_x]] = true;

	while (!q.empty()) {
		int y, x, cnt;
		tie(y, x, cnt) = q.front();
		// cout << y << ", " << x << " (" << cnt << ")" << endl;
		q.pop();

		if (color[y][x] == color[d_y][d_x]) {
			cout << cnt << endl;
			return 0;
		}

		for (int i = -2; i <= 2; i++) {
			for (int j = -2; j <= 2; j++) {
				if (i == 0 && j == 0) continue;
				int ny = y + i;
				int nx = x + j;
				if (in_range(ny, nx) && v[ny][nx] == '.') {
					if (!visit_col[color[ny][nx]]) {
						visit[ny][nx] = true;
						visit_col[color[ny][nx]] = true;
						q.emplace(ny, nx, cnt + 1);
					} else if (!visit[ny][nx] && color[ny][nx] == color[y][x]) {
						visit[ny][nx] = true;
						q.emplace(ny, nx, cnt);
					}
				}
			}
		}
	}

	cout << -1 << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:68:49: error: reference to 'visit' is ambiguous
   68 |                                                 visit[ny][nx] = true;
      |                                                 ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:9:6: note:                 'bool visit [1005][1005]'
    9 | bool visit[1005][1005];
      |      ^~~~~
a.cc:71:53: error: reference to 'visit' is ambiguous
   71 |                                         } else if (!visit[ny][nx] && color[ny][nx] == color[y][x]) {
      |                                                     ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:9:6: note:                 'bool visit [1005][1005]'
    9 | bool visit[1005][1005];
      |      ^~~~~
a.cc:72:49: error: reference to 'visit' is ambiguous
   72 |                                                 visit[ny][nx] = true;
      |                                                 ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:9:6: note:                 'bool visit [1005][1005]'
    9 | bool visit[1005][1005];
      |      ^~~~~