Language: C++

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

const int dy[4] = { -1, 0, 1, 0 }, dx[4] = { 0, 1, 0, -1 };

char a[1005][1005];
vector<pair<int, int> > v;
queue<pair<int, int> > Q, nq;
int visit[1005][1005];

int main(void)
{
	int h, w, sy, sx, ey, ex, i, j, dir, magic;

	scanf("%d %d %d %d %d %d", &h, &w, &sy, &sx, &ey, &ex);
	for (i = 1; i <= h; i++)
		scanf("%s", a[i] + 1);

	Q.push(make_pair(sy, sx));
	visit[sy][sx] = 1;

	for (magic = 0; ; magic++)
	{
		if (Q.empty())
			break;

		v.clear();

		while (!Q.empty())
		{
			int nowy = Q.front().first;
			int nowx = Q.front().second;
			Q.pop();

			v.push_back(make_pair(nowy, nowx));

			if (nowy == ey && nowx == ex)
			{
				printf("%d", magic);
				return 0;
			}
			// printf("%d %d\n", nowy, nowx);
			for (dir = 0; dir < 4; dir++)
			{
				int nexty = nowy + dy[dir];
				int nextx = nowx + dx[dir];
				// printf("> %d %d\n", nexty, nextx);
				if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && !visit[nexty][nextx]))
					continue;

				Q.push(make_pair(nexty, nextx));
				visit[nexty][nextx] = 1;
			}
		}

		for (pair<int, int> val : v)
		{
			for (i = -2; i <= 2; i++)
				for (j = -2; j <= 2; j++)
				{
					int nexty = val.first + i;
					int nextx = val.second + j;
					// printf("> %d %d\n", nexty, nextx);
					if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && !visit[nexty][nextx]))
						continue;

					nq.push(make_pair(nexty, nextx));
					visit[nexty][nextx] = 1;
				}
		}

		while (!nq.empty())
		{
			// printf("> %d %d\n", nq.front().first, nq.front().second);
			Q.push(nq.front());
			nq.pop();
		}
	}
	puts("-1");
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:20:9: error: reference to 'visit' is ambiguous
   20 |         visit[sy][sx] = 1;
      |         ^~~~~
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:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:48:122: error: reference to 'visit' is ambiguous
   48 |                                 if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && !visit[nexty][nextx]))
      |                                                                                                                          ^~~~~
/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:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:52:33: error: reference to 'visit' is ambiguous
   52 |                                 visit[nexty][nextx] = 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:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:64:130: error: reference to 'visit' is ambiguous
   64 |                                         if (!(1 <= nexty && nexty <= h && 1 <= nextx && nextx <= w && a[nexty][nextx] == '.' && !visit[nexty][nextx]))
      |                                                                                                                                  ^~~~~
/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:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~
a.cc:68:41: error: reference to 'visit' is ambiguous
   68 |                                         visit[nexty][nextx] = 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:5: note:                 'int visit [1005][1005]'
    9 | int visit[1005][1005];
      |     ^~~~~