Language: C++

// ######      ###      #######    #######    ##     #     #####        ###     ##### //
// #     #    #   #          #        #       # #    #    #     #      #   #     ###  //
// #     #   #     #        #         #       #  #   #   #       #    #     #    ###  //
// ######   #########      #          #       #   #  #   #           #########    #   //
// #     #  #       #     #           #       #    # #   #    ####   #       #    #   //
// #     #  #       #    #            #       #     ##   #    #  #   #       #        //
// ######   #       #   #######    #######    #      #    #####  #   #       #    #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i)	cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 100005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 200005;
const long double epsilon = 1e-9;
inline ll myceil(ll a, ll b) { if (a % b == 0)	return (a / b);	return (a / b + 1);}
inline ll binlen(ll n) {
	ll c = 0;
	while (n > 0)
		++c , n /= 2;
	return c;
}
ll binexp(ll a, ll b, ll m) {
	a %= m;
	ll res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return res;
}
ll modinvfermat(ll a, ll m)
{
	return binexp(a, m - 2, m);
}
void task(bool flag)
{
	if (flag)
		cout << "YES\n";
	else
		cout << "NO\n";
}
ll lcm(ll a, ll b)
{
	return ((1LL * a * b) / (__gcd(a, b)));
}

ll n, m, sx, sy, fx, fy;
vector<vector<char>> grid;
vvl dist;
vl dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
bool isvalid(ll x, ll y)
{
	if (x < 1 || x > n || y < 1 || y > m)
		return false;
	if (grid[x][y] == '#')
		return false;
	return true;
}
void bfs()
{
	queue<pair<ll, ppl>> q;
	dist[sx][sy] = 0;
	q.push({0, {sx, sy}});
	while (!q.empty())
	{
		ll x = q.top().ss.ff, y = q.top().ss.ss, d = q.top().ff;
		q.pop();
		loop(i, 4)
		{
			if (isvalid(x + dx[i], y + dy[i]))
			{
				if (d < dist[x + dx[i]][y + dy[i]])
				{
					dist[x + dx[i]][y + dy[i]] = d;
					q.push({d, {x + dx[i], y + dy[i]}});
				}
			}
		}
		FOR(i, -2, 2)
		{
			FOR(j, -2, 2)
			{
				if (isvalid(x + i, y + j))
				{
					if (d + 1 < dist[x + i][y + j])
					{
						dist[x + i][y + j] = min(dist[x+i][y+j],d + 1);
						q.push({dist[x+i][y+j] + 1, {x + i, y + j}});
					}
				}
			}
		}
	}
}

signed main() {
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif
	fast
	cin >> n >> m >> sx >> sy >> fx >> fy;
	grid = vector<vector<char>>(n + 1, vector<char>(m + 1));
	dist = vvl(n + 1, vl(m + 1, inf));
	FOR(i, 1, n)	FOR(j, 1, m)	cin >> grid[i][j];
	bfs();
	if (dist[fx][fy] == inf)
		cout << -1 << END;
	else
		cout << dist[fx][fy] << END;



// #ifndef ONLINE_JUDGE
// 	cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
// #endif
	return 0;
}


Compiler output:
a.cc: In function 'void bfs()':
a.cc:98:26: error: 'class std::queue<std::pair<long long int, std::pair<long long int, long long int> > >' has no member named 'top'; did you mean 'pop'?
   98 |                 ll x = q.top().ss.ff, y = q.top().ss.ss, d = q.top().ff;
      |                          ^~~
      |                          pop
a.cc:102:48: error: 'y' was not declared in this scope
  102 |                         if (isvalid(x + dx[i], y + dy[i]))
      |                                                ^
a.cc:104:37: error: 'd' was not declared in this scope
  104 |                                 if (d < dist[x + dx[i]][y + dy[i]])
      |                                     ^
a.cc:107:47: error: no matching function for call to 'std::queue<std::pair<long long int, std::pair<long long int, long long int> > >::push(<brace-enclosed initializer list>)'
  107 |                                         q.push({d, {x + dx[i], y + dy[i]}});
      |                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/queue:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:157,
                 from a.cc:13:
/usr/include/c++/14/bits/stl_queue.h:283:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::pair<long long int, std::pair<long long int, long long int> >; _Sequence = std::deque<std::pair<long long int, std::pair<long long int, long long int> >, std::allocator<std::pair<long long int, std::pair<long long int, long long int> > > >; value_type = std::pair<long long int, std::pair<long long int, long long int> >]'
  283 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:283:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::queue<std::pair<long long int, std::pair<long long int, long long int> > >::value_type&' {aka 'const std::pair<long long int, std::pair<long long int, long long int> >&'}
  283 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:288:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = std::pair<long long int, std::pair<long long int, long long int> >; _Sequence = std::deque<std::pair<long long int, std::pair<long long int, long long int> >, std::allocator<std::pair<long long int, std::pair<long long int, long long int> > > >; value_type = std::pair<long long int, std::pair<long long int, long long int> >]'
  288 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:288:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::queue<std::pair<long long int, std::pair<long long int, long long int> > >::value_type&&' {aka 'std::pair<long long int, std::pair<long long int, long long int> >&&'}
  288 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc:115:52: error: 'y' was not declared in this scope
  115 |                                 if (isvalid(x + i, y + j))
      |                                                    ^
a.cc:117:45: error: 'd' was not declared in this scope
  117 |                                         if (d + 1 < dist[x + i][y + j])
      |                                             ^
a.cc:120:55: error: no matching function for call to 'std::queue<std::pair<long long int, std::pair<long long int, long long int> > >::push(<brace-enclosed initializer list>)'
  120 |                                                 q.push({dist[x+i][y+j] + 1, {x + i, y + j}});
      |                                                 ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:283:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::pair<long long int, std::pair<long long int, long long int> >; _Sequence = std::deque<std::pair<long long int, std::pair<long long int, long long int> >, std::allocator<std::pair<long long int, std::pair<long long int, long long int> > > >; value_type = std::pair<long long int, std::pair<long long int, long long int> >]'
  283 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:283:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::queue<std::pair<long long int, std::pair<long long int, long long int> > >::value_type&' {aka 'const std::pair<long long int, std::pair<long long int, long long int> >&'}
  283 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:288:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = std::pair<long long int, std::pair<long long int, long long int> >; _Sequence = std::deque<std::pair<long long int, std::pair<long long int, long long int> >, std::allocator<std::pair<long long int, std::pair<long long int, long long int> > > >; value_type = std::pair<long long int, std::pair<long long int, long long int> >]'
  288 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:288:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::queue<std::pair<long long int, std::pair<long long int, long long int> > >::value_type&&' {aka 'std::pair<long long int, std::pair<long long int, long long int> >&&'}
  288 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~