Language: C++

#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()

vector<vector<ll>> note;

vector<string>  vec;




void dfs(ll &H,ll &W) {
  
  if(vec[H+1][W]=='.') {
    if(note[H+1][W]==0||note[H+1][W]>note[H][W]) {

note[H+1][W]=note[H][W];
  dfs(H+1,W);
  }
  }
  
  if(vec[H-1][W]=='.') {
    if(note[H-1][W]==0||note[H-1][W]>note[H][W]) {

note[H-1][W]=note[H][W];
  dfs(H-1,W);
  }
  }
  
   if(vec[H][W+1]=='.') {
    if(note[H][W+1]==0||note[H][W+1]>note[H][W]) {

note[H][W+1]=note[H][W];
  dfs(H,W+1);
  }
  }
  
   if(vec[H][W-1]=='.') {
    if(note[H][W-1]==0||note[H][W-1]>note[H][W]) {

note[H][W-1]=note[H][W];
  dfs(H,W-1);
  }
  }
 
 
   
  
  if(vec[H+2][W+2]=='.') {
if(note[H+2][W+2]==0||note[H+2][W+2]>note[H][W]+1) {
note[H+2][W+2]=note[H][W]+1;
  dfs(H+2,W+2);
}
 }
   if(vec[H+1][W+1]=='.') {
if(note[H+1][W+1]==0||note[H+1][W+1]>note[H][W]+1) {
note[H+1][W+1]=note[H][W]+1;
  dfs(H+1,W+1);
}
 }
  
  
  if(vec[H-1][W-1]=='.') {
if(note[H-1][W-1]==0||note[H-1][W-1]>note[H][W]+1) {
note[H-1][W-1]=note[H][W]+1;
  dfs(H-1,W-1);
}
 }
  
   if(vec[H-2][W-2]=='.') {
if(note[H-2][W-2]==0||note[H-2][W-2]>note[H][W]+1) {
note[H-2][W-2]=note[H][W]+1;
  dfs(H-2,W-2);
}
 }
  
  
  if(vec[H-1][W+1]=='.') {
if(note[H-1][W+1]==0||note[H-1][W+1]>note[H][W]+1) {
note[H-1][W+1]=note[H][W]+1;
  dfs(H-1,W+1);
}
 }
  
    if(vec[H-2][W+2]=='.') {
if(note[H-2][W+2]==0||note[H-2][W+2]>note[H][W]+1) {
note[H-2][W+2]=note[H][W]+1;
  dfs(H-2,W+2);
}
 }
  
   if(vec[H+2][W-2]=='.') {
if(note[H+2][W-2]==0||note[H+2][W-2]>note[H][W]+1) {
note[H+2][W-2]=note[H][W]+1;
  dfs(H+2,W-2);
}
 }
  
  if(vec[H+1][W-1]=='.') {
if(note[H+1][W-1]==0||note[H+1][W-1]>note[H][W]+1) {
note[H+1][W-1]=note[H][W]+1;
  dfs(H+1,W-1);
}
 }
  
  
 if(vec[H+2][W]=='.') {
if(note[H+2][W]==0||note[H+2][W]>note[H][W]+1) {
note[H+2][W]=note[H][W]+1;
  dfs(H+2,W);
}
 }
  
   if(vec[H-2][W]=='.') {
if(note[H-2][W]==0||note[H-2][W]>note[H][W]+1) {
note[H-2][W]=note[H][W]+1;
  dfs(H-2,W);
}
 }
  if(vec[H-2][W+1]=='.') {
if(note[H-2][W+1]==0||note[H-2][W+1]>note[H][W]+1) {
note[H-2][W+1]=note[H][W]+1;
  dfs(H-2,W+1);
}
 }
  
    if(vec[H-2][W-1]=='.') {
if(note[H-2][W-1]==0||note[H-2][W-1]>note[H][W]+1) {
note[H-2][W-1]=note[H][W]+1;
  dfs(H-2,W-1);
}
 }

if(vec[H+2][W+1]=='.') {
if(note[H+2][W+1]==0||note[H+2][W+1]>note[H][W]+1) {
note[H+2][W+1]=note[H][W]+1;
  dfs(H+2,W+1);
}
 }
  if(vec[H+2][W-1]=='.') {
if(note[H+2][W-1]==0||note[H+2][W-1]>note[H][W]+1) {
note[H+2][W-1]=note[H][W]+1;
  dfs(H+2,W-1);
}
 }


  if(vec[H][W+2]=='.') {
if(note[H][W+2]==0||note[H][W+2]>note[H][W]+1) {
note[H][W+2]=note[H][W]+1;
  dfs(H,W+2);
}
 }


 if(vec[H][W-2]=='.') {
if(note[H][W-2]==0||note[H][W-2]>note[H][W]+1) {
note[H][W-2]=note[H][W]+1;
  dfs(H,W-2);
}
 }
  
   if(vec[H+1][W-2]=='.') {
if(note[H+1][W-2]==0||note[H+1][W-2]>note[H][W]+1) {
note[H+1][W-2]=note[H][W]+1;
  dfs(H+1,W-2);
}
 }
   
   if(vec[H-1][W-2]=='.') {
if(note[H-1][W-2]==0||note[H-1][W-2]>note[H][W]+1) {
note[H-1][W-2]=note[H][W]+1;
  dfs(H-1,W-2);
}
 }
  
  if(vec[H+1][W+2]=='.') {
if(note[H+1][W+2]==0||note[H+1][W+2]>note[H][W]+1) {
note[H+1][W+2]=note[H][W]+1;
  dfs(H+1,W+2);
}
 }
  
   if(vec[H-1][W+2]=='.') {
if(note[H-1][W+2]==0||note[H-1][W+2]>note[H][W]+1) {
note[H-1][W+2]=note[H][W]+1;
  dfs(H-1,W+2);
}
 }
  
  return ;
}

  
  
  
  
int main() {
 ll H,W;
  cin>>H>>W;
  ll Ch,Cw;
  cin>>Ch>>Cw;
  Ch+=1;
  Cw+=1;
  ll Dh,Dw;
  cin>>Dh>>Dw;
  Dh+=1;
  Dw+=1;
  note=vector<vector<ll>>(H+4,vector<ll>(W+4));
  
  vec=vector<string> (H+4);
  string S;
  for(ll i=0;i<W+4;i++) {
S+='#';
  }
  vec[0]=S;
  vec[1]=S;
  vec[H+2]=S;
  vec.back()=S;
  for(ll i=2;i<=H+1;i++) {
  string T;
    cin>>T;
    vec[i]="##"+T+"##";
  }
  note[Ch][Cw]=1;
  dfs(Ch,Cw);
  if(note[Dh][Dw]==0) {
cout<<-1<<endl;
    return 0;
  }


else {
cout<<note[Dh][Dw]-1<<endl;
}
}


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  


Compiler output:
a.cc: In function 'void dfs(ll&, ll&)':
a.cc:19:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   19 |   dfs(H+1,W);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:27:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   27 |   dfs(H-1,W);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:35:10: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   35 |   dfs(H,W+1);
      |         ~^~
a.cc:13:20: note:   initializing argument 2 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |                ~~~~^
a.cc:43:10: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   43 |   dfs(H,W-1);
      |         ~^~
a.cc:13:20: note:   initializing argument 2 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |                ~~~~^
a.cc:53:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   53 |   dfs(H+2,W+2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:59:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   59 |   dfs(H+1,W+1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:67:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   67 |   dfs(H-1,W-1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:74:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   74 |   dfs(H-2,W-2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:82:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   82 |   dfs(H-1,W+1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:89:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   89 |   dfs(H-2,W+2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:96:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
   96 |   dfs(H+2,W-2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:103:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  103 |   dfs(H+1,W-1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:111:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  111 |   dfs(H+2,W);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:118:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  118 |   dfs(H-2,W);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:124:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  124 |   dfs(H-2,W+1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:131:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  131 |   dfs(H-2,W-1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:138:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  138 |   dfs(H+2,W+1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:144:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  144 |   dfs(H+2,W-1);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:152:10: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  152 |   dfs(H,W+2);
      |         ~^~
a.cc:13:20: note:   initializing argument 2 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |                ~~~~^
a.cc:160:10: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  160 |   dfs(H,W-2);
      |         ~^~
a.cc:13:20: note:   initializing argument 2 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |                ~~~~^
a.cc:167:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  167 |   dfs(H+1,W-2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:174:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  174 |   dfs(H-1,W-2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:181:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  181 |   dfs(H+1,W+2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^
a.cc:188:8: error: cannot bind non-const lvalue reference of type 'll&' {aka 'long long int&'} to an rvalue of type 'll' {aka 'long long int'}
  188 |   dfs(H-1,W+2);
      |       ~^~
a.cc:13:14: note:   initializing argument 1 of 'void dfs(ll&, ll&)'
   13 | void dfs(ll &H,ll &W) {
      |          ~~~~^