Language: C++

#if _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#ifndef ONLINE_JUDGE 
const bool DEBUG = true;
#else
const bool DEBUG = false;
#endif

// Reads a 1D vector
template <class T>
void read1d(vector<T>& arg) {
	const int l = arg.size();
	for (int i = 0; i < l; ++i) cin >> arg[i];
}

void read() {}
// Reads arbitrary values
template <class T1, class... T2>
void read(T1& var1, T2&... var2) { cin >> var1; read(var2...); }



void pln() { cout << (DEBUG ? "]\n" : "\n"); }
template <class T1, class... T2>
void pln(const T1& var1, const T2& ... var2) { cout << var1 << " "; pln(var2...); }

// Prints arbitrary values and goes to next line
template <class T1, class... T2>
constexpr void print(const T1& var1, const T2& ... var2) { cout << (DEBUG ? " [ " : ""); pln(var1, var2...); }

// Prints arbitrary values and goes to next line ONLY IF IN DEBUG MODE
template <class T1, class... T2>
constexpr void debug(const T1& var1, const T2& ... var2) { if (!DEBUG) return;  cout << " debug : [ "; pln(var1, var2...); }





using ll = long long;
using ull = unsigned long long;


#define all(x) begin(x), end(x)

vector<pair<int, int>> dirs{
	{-1,0}, {1, 0}, {0, 1}, {0, -1}
};

const bool MULTIPLE_TEST_CASES = 0;

void bfs(vector<vector<char>>& grid, int y, int x, vector<vector<int>>& dist, int mark, vector<vector<bool>>& vis) {
	queue<pair<int, int>> b;
	b.push({ y, x });

	while (!b.empty()) {
		int l = (int)b.size();

		for (int i = 0; i < l; ++i) {
			auto p = b.front();
			b.pop();

			int _x = p.first, _y = p.second;
			if (dist[_x][_y] != -1) continue;
			dist[_x][_y] = mark;
			vis[_x][_y] = true;

			for (auto dir : dirs) {
				int dx = _x + dir.first;
				int dy = _y + dir.second;
				if (dx < 0 || dy < 0 || dx >= grid.size() || dy >= grid[0].size()) continue;
				if (dist[dx][dy] != -1) continue;

				b.push({ dx, dy });
			}
		}
	}
}

int ans = INT_MAX;
void dfs(vector<vector<int>>& graph, vector<bool>& visited, int root, int parent, int depth, int targetColor) {
	if (root == targetColor) {
		depth = min(ans, depth); return;
	}
	if (ans != INT_MAX) return;
	if (visited[root]) return;

	for (int i : graph[root]) {
		if (i == parent) continue;
		visited[i] = true;
		dfs(graph, visited, i, root, depth + 1, targetColor);
		visited[i] = false;
	}
}


void exec()
{
	int n, m;
	read(n, m);

	int cy, cx;
	read(cy, cx);
	--cy; --cx;

	int dy, dx;
	read(dy, dx);
	--dy; --dx;

	vector<vector<char>> arr(n, vector<char>(m));
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			cin >> arr[i][j];

	vector<vector<int>> dist(n, vector<int>(m, -1));
	vector<vector<int>> visited(n, vector<int>(m, false));
	vector<vector<int>> outsideMarked(n, vector<int>(m, false));

	int current = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (!visited[i][j]) {
				bfs(arr, i, j, dist, current, visited);
				++current;
			}
		}
	}

	vector<vector<int>> graph(current, vector<int>());
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			set<int> marks;

			if (dist[i][j] == -1) continue;
			int orig = dist[i][j];

			for (int a = -2; a <= 2; ++a) {
				for (int b = -2; b <= 2; ++b) {
					if (i + a < 0 || i + a >= n || j + b < 0 || j + b >= m) continue;

					int dx = i + a, dy = j + b;
					if (dist[dx][dy] != -1 && dist[dx][dy] != orig) marks.insert(dist[dx][dy]);
				}
			}

			for (int k : marks) {
				graph[orig].push_back(k);
				graph[k].push_back(orig);
			}
		}
	}

	vector<bool> visited2(current, false);
	visited2[dist[cy][cx]] = true;
	dfs(graph, visited2, dist[cy][cx], -1, 0, dist[dy][dx]);
	if (ans == INT_MAX) ans = -1;
	print(ans);
}

















int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	if (!MULTIPLE_TEST_CASES)
		exec();
	else {
		int n;
		cin >> n;

		while (n--)
			exec();
	}

	return 0;
}

Compiler output:
a.cc: In function 'void exec()':
a.cc:128:63: error: invalid initialization of reference of type 'std::vector<std::vector<bool> >&' from expression of type 'std::vector<std::vector<int> >'
  128 |                                 bfs(arr, i, j, dist, current, visited);
      |                                                               ^~~~~~~
a.cc:57:111: note: in passing argument 6 of 'void bfs(std::vector<std::vector<char> >&, int, int, std::vector<std::vector<int> >&, int, std::vector<std::vector<bool> >&)'
   57 | void bfs(vector<vector<char>>& grid, int y, int x, vector<vector<int>>& dist, int mark, vector<vector<bool>>& vis) {
      |                                                                                         ~~~~~~~~~~~~~~~~~~~~~~^~~