Language: C++

//Optional FAST

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")

//Required Libraries

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

//Required namespaces

using namespace std;
using namespace __gnu_pbds;

//Required defines

#define endl '\n'

#define READ(X) cin>>X;
#define READV(X) int X; cin>>X;
#define READAR(A,N) int A[N]; for(int i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (int)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a,b,c) for(int a=b;a<c;a++)
#define FORD(a,b,c) for(int a=b;a>c;a--)

//Required typedefs

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,greater<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;
typedef int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,int> pll;

//Required Constants

// Required random number generators

// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());

//Required Functions

//Work

int main()
{
    #ifndef ONLINE_JUDGE
    if (fopen("INPUT.txt", "r"))
    {
        freopen ("INPUT.txt" , "r" , stdin);
        //freopen ("OUTPUT.txt" , "w" , stdout);
    }
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    clock_t clk;
    clk = clock();
//-----------------------------------------------------------------------------------------------------------//

    READV(H);
    READV(W);
    ll A[H][W];
    char S[H][W];
    memset(A,-1,sizeof(A));
    READV(Cx);
    READV(Cy);
    Cx--;
    Cy--;
    READV(Dx);
    READV(Dy);
    Dx--;
    Dy--;
    FORI(i,0,H)
    {
        FORI(j,0,W)
        {
            cin>>S[i][j];
        }
    }
    A[Cx][Cy]=0;
    deque<pll> pre;
    pre.pb({Cx,Cy});
    while (pre.size()>0)
    {
        vector<pll> done;
        while (pre.size()>0)
        {
            pll st=pre.front();
            pre.pop_front();
            deque<pll> bfs;
            bfs.pb(st);
            ll val=A[st.fi][st.se];
            while (bfs.size()>0)
            {
                pll u=bfs.front();
                A[u.fi][u.se]=val;
                done.pb(u);
                bfs.pop_front();
                if(u.fi-1>=0 && S[u.fi-1][u.se]=='.' && A[u.fi-1][u.se]==-1)
                {
                    bfs.pb({u.fi-1,u.se});
                }
                if(u.se-1>=0 && S[u.fi][u.se-1]=='.' && A[u.fi][u.se-1]==-1)
                {
                    bfs.pb({u.fi,u.se-1});
                }
                if(u.fi+1<H && S[u.fi+1][u.se]=='.' && A[u.fi+1][u.se]==-1)
                {
                    bfs.pb({u.fi+1,u.se});
                }
                if(u.se+1<W && S[u.fi][u.se+1]=='.' && A[u.fi][u.se+1]==-1)
                {
                    bfs.pb({u.fi,u.se+1});
                }
            }
        }
        for(auto v: done)
        {
            FORI(i,max(0,v.fi-2),min(H-1,v.fi+2)+1)
            {
                FORI(j,max(0,v.se-2),min(W-1,v.se+2)+1)
                {
                    if(A[i][j]==-1 && S[i][j]!='#')
                    {
                        A[i][j]=1+A[v.fi][v.se];
                        pre.pb({i,j});
                    }
                }
            }
        }
    }
    cout<<A[Dx][Dy]<<endl;
    

//-----------------------------------------------------------------------------------------------------------//


    clk = clock() - clk;
    cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << endl;

    return 0;
}

Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:9:
/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/x86_64-linux-gnu/c++/14/bits/stdc++.h:139:
/usr/include/c++/14/bits/stl_deque.h:542:14: note: called from here
  542 |       struct _Deque_impl
      |              ^~~~~~~~~~~