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;
ll H,W;

ll Dh,Dw;

void dfs(ll H,ll W) {
  
  if(vec[H+1][W]=='.'&&note[H+1][W]==0) {
note[H+1][W]=note[H][W];
  dfs(H+1,W);
  }
 
    if(vec[H-1][W]=='.'&&note[H-1][W]==0) {
note[H-1][W]=note[H][W];
  dfs(H-1,W);
  }
  
   if(vec[H][W+1]=='.'&&note[H][W+1]==0) {
note[H][W+1]=note[H][W];
  dfs(H,W+1);
  }
  
     if(vec[H][W-1]=='.'&&note[H][W-1]==0) {
note[H][W-1]=note[H][W];
  dfs(H,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-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() {

  cin>>H>>W;
  
  cin>>Ch>>Cw;
  Ch+=2;
  Cw+=2;
  
  cin>>Dh>>Dw;
  Dh+=2;
  Dw+=2;
  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+"##";
  }
  
  dfs(Ch,Cw);
  if(note[Dh][Dw]==0) {
cout<<0<<endl;
    return 0;
  }


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


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

Compiler output:
a.cc: In function 'int main()':
a.cc:188:8: error: 'Ch' was not declared in this scope; did you mean 'Dh'?
  188 |   cin>>Ch>>Cw;
      |        ^~
      |        Dh
a.cc:188:12: error: 'Cw' was not declared in this scope; did you mean 'Dw'?
  188 |   cin>>Ch>>Cw;
      |            ^~
      |            Dw