Language: C++

///   BISMILLAHIR RAHMANIR RAHEEM
///   ALLAH IS WATCHING ME
///   ALLAH save us from COVID-19.Amin.

///   Every soul shall taste death.

///   Tag ::

#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>

///  order_of_key return number of elements less than x.
///  find_by_order return index.

using namespace std;
using namespace __gnu_pbds;


#define MOHAMMAD  ios::sync_with_stdio(0);cin.tie(0);
#define all(x)    (x).begin(), (x).end()
#define AE        cout << fixed << setprecision(2);

/// faster.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; // 8-direction.......
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[]= {1,-1,0,0}; // 4-direction...........
int dy4[]= {0,0,1,-1};

using ll = long long;
const ll MOD = 1e9 + 7;

typedef tree< int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;

//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

/// Modular arithmetic
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }  /// When MOD is prime.
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

//***********************************************  The END **********************************************************************************************************************************
const ll N = 1e3;
const ll INF = 1e15;
const ll limit = (1LL<<64) - 1;

char grid[N+1][N+1];
ll h , w , ch , cw , dh , dw;
bool visit[N+1][N+1];

ll dep[N+1][N+1];

ll dir[] = { -2 , -1 , 0 , 1 , 2 };
ll dir1[] = { -2 , -1 , 0 , 1 , 2 };
ll ans = 0;

void bfs ( ll x , ll y ){

queue < pair < ll , ll > > q;

visit[x][y] = 1;
dep[x][y] = 0;

q.push( { x , y } );
bool flg = false;
while ( q.size( ) ){
    if (  flg ) break;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    dbg ( x , y );
    bool got = false;
    for ( ll i = 0 ; i < 4 ; i++ ){
       ll xx = x + dx4[i];
       ll yy = y + dy4[i];
       if ( xx<0 or xx>=h or yy<0 or yy>=w or visit[xx][yy] or grid[xx][yy]=='#' ) continue;
       dep[xx][yy] = dep[x][y] + 0;
       q.push( { xx , yy } );
       visit[xx][yy] = 1;
       got = true;
       if ( xx==dh and yy==dw ) { flg = true; ans = dep[xx][yy];break;}
    }
    if ( got ) continue;
    else{
        for ( ll i = 0 ; i < 5 ; i++ ){
       ll xx = x - 2;
       ll yy = y + dir[i];
       if ( xx<0 or xx>=h or yy<0 or yy>=w or visit[xx][yy] or grid[xx][yy]=='#' ) continue;
       dep[xx][yy] = dep[x][y] + 1;
       q.push( { xx , yy } );
       visit[xx][yy] = 1;
       got = true;
       if ( xx==dh and yy==dw ) { flg = true; ans = dep[xx][yy];break;}
       }
       if ( got ) continue;
       for ( ll i = 0 ; i < 5 ; i++ ){
       ll xx = x + 2;
       ll yy = y + dir1[i];
       if ( xx<0 or xx>=h or yy<0 or yy>=w or visit[xx][yy] or grid[xx][yy]=='#'  ) continue;
       dep[xx][yy] = dep[x][y] + 1;
       q.push( { xx , yy } );
       visit[xx][yy] = 1;
       got = true;
       if ( xx==dh and yy==dw ) { flg = true; ans = dep[xx][yy];break;}
       }
    }
    if ( !got ) ans = -1;
}
}


int main()
{

  MOHAMMAD

  cin >> h >> w >> ch >> cw >> dh >> dw;

  for ( ll i = 0 ; i<h ; i++ ){
     for ( ll j = 0 ; j<w ; j++ ){
         cin >> grid[i][j];
     }
  }
  dh-- , dw--;
  bfs ( ch-1 , cw-1 );

  cout << ans << "\n";




}


/*

Explanation:



TODO :

  Alhamdulillah
*/


Compiler output:
a.cc:65:22: warning: left shift count >= width of type [-Wshift-count-overflow]
   65 | const ll limit = (1LL<<64) - 1;
      |                   ~~~^~~~
a.cc: In function 'void bfs(ll, ll)':
a.cc:81:1: error: reference to 'visit' is ambiguous
   81 | visit[x][y] = 1;
      | ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:9:
/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:69:6: note:                 'bool visit [1001][1001]'
   69 | bool visit[N+1][N+1];
      |      ^~~~~
a.cc:96:47: error: reference to 'visit' is ambiguous
   96 |        if ( xx<0 or xx>=h or yy<0 or yy>=w or visit[xx][yy] or grid[xx][yy]=='#' ) continue;
      |                                               ^~~~~
/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:69:6: note:                 'bool visit [1001][1001]'
   69 | bool visit[N+1][N+1];
      |      ^~~~~
a.cc:99:8: error: reference to 'visit' is ambiguous
   99 |        visit[xx][yy] = 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:69:6: note:                 'bool visit [1001][1001]'
   69 | bool visit[N+1][N+1];
      |      ^~~~~
a.cc:108:47: error: reference to 'visit' is ambiguous
  108 |        if ( xx<0 or xx>=h or yy<0 or yy>=w or visit[xx][yy] or grid[xx][yy]=='#' ) continue;
      |                                               ^~~~~
/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:69:6: note:                 'bool visit [1001][1001]'
   69 | bool visit[N+1][N+1];
      |      ^~~~~
a.cc:111:8: error: reference to 'visit' is ambiguous
  111 |        visit[xx][yy] = 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:69:6: note:                 'bool visit [1001][1001]'
   69 | bool visit[N+1][N+1];
      |      ^~~~~
a.cc:119:47: error: reference to 'visit' is ambiguous
  119 |        if ( xx<0 or xx>=h or yy<0 or yy>=w or visit[xx][yy] or grid[xx][yy]=='#'  ) continue;
      |                                               ^~~~~
/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:69:6: note:                 'bool visit [1001][1001]'
   69 | bool visit[N+1][N+1];
      |      ^~~~~
a.cc:122:8: error: reference to 'visit' is ambiguous
  122 |        visit[xx][yy] = 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:69:6: note:                 'bool visit [1001][1001]'
   69 | bool visit[N+1][N+1];
      |      ^~~~~