Language: C++

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 1e5 + 42, inf = 1e9 + 42;
vector<pair<ll, ll> > g[N];
string m[N];
ll h, w;
ll sx, sy, fx, fy;
ll comp[N][N], movex[4] = {-1, 0, 0, 1}, movey[4] = {0, -1, 1, 0};
ll cur = 0;
int main() {

  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  cin >> h >> w;

  cin >> sx >> sy >> fx >> fy;

  sx--; sy--; fx--; fy--;

  for(ll i = 0; i < h; i++) cin >> m[i];

  for(ll i = 0; i < h; i++)
    for(ll j = 0; j < w; j++)
      if(m[i][j] == '.' && !comp[i][j]) {
        cur++;
        stack<pair<ll, ll> > s;
        s.push(mp(i, j));
        while(!s.empty()) {
          auto p = s.top(); s.pop();
          comp[p.fi][p.se] = cur;
          for(ll mm = 0; mm < 4; mm++) {
            ll cx = p.fi + movex[mm], cy = p.se + movey[mm];
            if(cx >= 0 && cx < h && cy >= 0 && cy <= w && m[cx][cy] == '.' && !comp[cx][cy])
              s.push(mp(cx, cy));
          }
        }
      }

  for(ll i = 0; i < h; i++) {
    for(ll j = 0; j < w; j++)
      if(comp[i][j]) {
        for(ll xx = -2; xx <= 2; xx++)
          for(ll yy = -2; yy <= 2; yy++) {
            ll cx = i + xx, cy = j + yy;
            if(cx >= 0 && cx < h && cy >= 0 && cy <= w && m[cx][cy] == '.' && comp[cx][cy] != comp[i][j]) {
              g[comp[i][j] - 1].pb(mp(comp[cx][cy] - 1, 1));
              g[comp[cx][cy] - 1].pb(mp(comp[i][j] - 1, 1));
            }
          }
      }
  }

  ll s = comp[sx][sy] - 1;

  vector<ll> d(cur + 42, inf);

  d[s] = 0;

  vector<char> u(cur + 42);

  for(ll i = 0; i < cur; i++) {
    ll v = -1;
    for(ll j = 0; j < cur; j++)
      if(!u[j] && (v == -1 || d[j] < d[v])) v = j;
    if(v == -1 || d[v] == inf) break;
    u[v] = true;
    for(ll j = 0; j < g[v].size(); j++) {
      ll to = g[v][j].fi, cst = g[v][j].se;
      if(d[v] + cst < d[to]) d[to] = d[v] + cst;
    }
  }

  ll need = comp[fx][fy] - 1;

  ll ans = (d[need] >= inf ? -1 : d[need]);

  cout << ans << endl;

  return 0;
}
/*

*/


Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:4:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::__cxx11::basic_string<char>::_Alloc_hider::~_Alloc_hider()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = char]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:186:14: note: called from here
  186 |       struct _Alloc_hider : allocator_type // TODO check __is_final
      |              ^~~~~~~~~~~~