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 int N = 5e4 + 42, inf = 1e9 + 42;
char m[N][N];
int h, w;
int sx, sy, fx, fy;
int cur = 0;
int movex[4] = {-1, 0, 0, 1};
int movey[4] = {0, -1, 1, 0};
int comp[N][N], d[N][N];
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(int i = 0; i < h; i++)
    for(int j = 0; j < w; j++) {
      cin >> m[i][j];
      comp[i][j] = -1;
    }

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

  for(int i = 0; i < cur; i++)
    for(int j = 0; j < cur; j++)
      d[i][j] = inf;

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

  for(int i = 0; i < cur; i++) d[i][i] = 0;

  for(int k = 0; k < cur; k++)
    for(int i = 0; i < cur; i++)
      for(int j = 0; j < cur; j++)
        if(d[i][k] < inf && d[k][j] < inf)
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

  int ans = d[comp[sx][sy]][comp[fx][fy]];

  cout << (ans >= inf ? -1 : ans);

  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::_Deque_base<std::pair<int, int>, std::allocator<std::pair<int, int> > >::_Deque_impl::~_Deque_impl()':
/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 = std::pair<int, int>]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/deque:66,
                 from /usr/include/c++/14/stack:62,
                 from a.cc:9:
/usr/include/c++/14/bits/stl_deque.h:542:14: note: called from here
  542 |       struct _Deque_impl
      |              ^~~~~~~~~~~