Language: C++

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"
#define pb push_back
#define mod 1000000007
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int dx8[]={1,-1,0,0,1,1,-1,-1};
int dy8[]={0,0,1,-1,1,-1,1,-1};

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;
ll dist[10000][10000];
ll vis[10000][10000];
ll dp[10000][10000];
ll co[10000][10000];
ll d1,d2;ll c1,c2;
ll cc1=1;
ll total=INT_MAX;


bool isvalid(ll x,ll y,ll currx,ll curry){
	if(x<1||x>n||y<1||y>m)return false;

	if(dp[x][y]==0&&vis[x][y]==0)return true;

	 return false;


}

void bfs(ll x,ll y){
	vis[x][y]=1;
	dist[x][y]=0;
	queue<pair<ll,ll>> q;
	q.push({x,y});

	
	while(!q.empty()){
		ll currx=q.front().first;
		ll curry=q.front().second;
		q.pop();

		
		for(ll i=currx-2;i<=currx+2;i++){
		for(ll j=curry-2;j<=curry+2;j++){
			if(isvalid(i,j,currx,curry)){
				vis[i][j]=1;
				if(co[i][j]==co[currx][curry])dist[i][j]=dist[currx][curry];
				else
					dist[i][j]=dist[currx][curry]+1;

				if(co[i][j]==co[d1][d2]){
					//cout<<co[i][j]<<endl;

					//cout<<co[d1][d2]<<endl;

					//cout<<total<<endl;

					total=min(total,dist[i][j]);
					//cout<<i<<" "<<j<<" "<<total<<endl;
				}

				q.push({i,j});


			}

		}
	}

	}
	

}
bool isvalid1(ll x,ll y){
	if(x<1||x>n||y<1||y>m)return false;

	if(vis[x][y]==1||dp[x][y]==1)return false;

	return true;


}
void dfs(ll x,ll y){
	vis[x][y]=1;
	co[x][y]=cc1;
	for(ll i=0;i<4;i++){
		if(isvalid1(x+dx[i],y+dy[i])){
			co[x+dx[i]][y+dy[i]]=cc1;
			dfs(x+dx[i],y+dy[i]);
		}
	}

}



void solve(){
ll h,w;
cin>>h>>w;
 n=h; m=w;
 memset(dp,-1,sizeof(dp));




cin>>c1>>c2;

cin>>d1>>d2;

char cc;
bool flag;


for(ll i=1;i<=n;i++){
	for(ll j=1;j<=m;j++){
		cin>>cc;
		if(cc=='#'){
			dp[i][j]=1;
			flag=true;}
		else
			dp[i][j]=0;


	}
}

for(ll i=1;i<=n;i++){
	for(ll j=1;j<=m;j++){
		if(vis[i][j]==0&&dp[i][j]==0){
			co[i][j]=cc1;
			dfs(i,j);
			cc1++;
		}
	}
}
if(co[c1][c2]==co[d1][d2]){
	cout<<0<<endl;
	return;
}
memset(vis,0,sizeof(vis));



if(dp[d1][d2]==1){
	cout<<-1<<endl;
	return;

}


bfs(c1,c2);





if(vis[d1][d2]==0)cout<<-1<<endl;
else
	cout<<min(total,abs(co[d1][d2]-co[c1][c2]))<<endl;


	
	
	return;
}




int main()
{  
#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output.txt", "w", stdout);
#endif
    fast;
    ll t;
    t=1;
    //cin>>t;
    while(t--){
    	solve();
    }
    
   
  



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

Compiler output:
/tmp/cc7sZgDj.o: in function `bfs(long long, long long)':
a.cc:(.text+0x362): relocation truncated to fit: R_X86_64_PC32 against symbol `co' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x387): relocation truncated to fit: R_X86_64_PC32 against symbol `co' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x45d): relocation truncated to fit: R_X86_64_PC32 against symbol `co' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x468): relocation truncated to fit: R_X86_64_PC32 against symbol `d1' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x46f): relocation truncated to fit: R_X86_64_PC32 against symbol `d2' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x488): relocation truncated to fit: R_X86_64_PC32 against symbol `co' defined in .bss section in /tmp/cc7sZgDj.o
/tmp/cc7sZgDj.o: in function `dfs(long long, long long)':
a.cc:(.text+0x6ac): relocation truncated to fit: R_X86_64_PC32 against symbol `co' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x772): relocation truncated to fit: R_X86_64_PC32 against symbol `co' defined in .bss section in /tmp/cc7sZgDj.o
/tmp/cc7sZgDj.o: in function `solve()':
a.cc:(.text+0x83b): relocation truncated to fit: R_X86_64_PC32 against symbol `c1' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x857): relocation truncated to fit: R_X86_64_PC32 against symbol `c2' defined in .bss section in /tmp/cc7sZgDj.o
a.cc:(.text+0x869): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status