Language: C++

#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
#define big 998244353
#define ff first
#define se second
#define pb push_back
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define PSET(x,y) fixed<<setprecision(y)<<x
#define mp make_pair
#define pi 3.141592653589
int power(int x,int y){
    int r=1,z=x;
    while(y){
        if(y & 1)r*=z;
        z*=z;y=y>>1;}
    return r;}
int powerm(int x,int y,int p){
    int r=1;
    while(y){
        if(y & 1)r=(r*x)%p;
        y=y>>1;
        x=(x*x)%p;}
    return r%p;}
int modinv(int x,int m){
    return powerm(x,m-2,m);}
int logarithm(int a,int b){
    int x=0;
    while(a>1){
        x++;
        a/=b;}
    return x;}
char mat[1000][1000];
int visit[1000][1000];
set<pair<int,int> > s,s2;
int h,w;
void dfs(int i,int j,int x){
    visit[i][j]=x;
    if(s.find(mp(i,j))!=s.end())
        s.erase(mp(i,j));
    if(i+1<h && mat[i+1][j]=='.' && !visit[i+1][j]){
        dfs(i+1,j,x);
    }
    if(i-1>-1 && mat[i-1][j]=='.' && !visit[i-1][j]){
        dfs(i-1,j,x);
    }
    if(j+1<w && mat[i][j+1]=='.' && !visit[i][j+1]){
        dfs(i,j+1,x);
    }
    if(j-1>-1 && mat[i][j-1]=='.' && !visit[i][j-1]){
        dfs(i,j-1,x);
    }
    for(int l=-2;l<=2;l++)
        for(int r=-2;r<=2;r++){
            if(l+r==0)
                continue;
            if(!visit[i+l][r+j] && mat[i+l][r+j]=='.')
                s.insert(mp(i+l,r+j));
        }
}
int32_t main(){
    fast;
    cin>>h>>w;
    int c1,c2;
    cin>>c1>>c2;
    c1--,c2--;
    int dx,dy;
    cin>>dx>>dy;
    dx--,dy--;
    for(int i=0;i<h;i++)
        for(int j=0;j<w;j++){
            visit[i][j]=0;
            cin>>mat[i][j];
        }
    s2.insert(mp(c1,c2));
    int x=1;
    while(s2.size()){
        for(auto p:s2){
            if(!visit[p.ff][p.se])
                dfs(p.ff,p.se,x);
        }
        s2.clear();
        s2=s;
        s.clear();
        x++;
    }
    if(visit[dx][dy])
        cout<<visit[dx][dy]-1;
    else
        cout<<-1;
    return 0;
}

Compiler output:
a.cc: In function 'void dfs(long long int, long long int, long long int)':
a.cc:40:5: error: reference to 'visit' is ambiguous
   40 |     visit[i][j]=x;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc: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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:43:38: error: reference to 'visit' is ambiguous
   43 |     if(i+1<h && mat[i+1][j]=='.' && !visit[i+1][j]){
      |                                      ^~~~~
/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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:46:39: error: reference to 'visit' is ambiguous
   46 |     if(i-1>-1 && mat[i-1][j]=='.' && !visit[i-1][j]){
      |                                       ^~~~~
/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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:49:38: error: reference to 'visit' is ambiguous
   49 |     if(j+1<w && mat[i][j+1]=='.' && !visit[i][j+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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:52:39: error: reference to 'visit' is ambiguous
   52 |     if(j-1>-1 && mat[i][j-1]=='.' && !visit[i][j-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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:59:17: error: reference to 'visit' is ambiguous
   59 |             if(!visit[i+l][r+j] && mat[i+l][r+j]=='.')
      |                 ^~~~~
/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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc: In function 'int32_t main()':
a.cc:74:13: error: reference to 'visit' is ambiguous
   74 |             visit[i][j]=0;
      |             ^~~~~
/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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:81:17: error: reference to 'visit' is ambiguous
   81 |             if(!visit[p.ff][p.se])
      |                 ^~~~~
/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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:89:8: error: reference to 'visit' is ambiguous
   89 |     if(visit[dx][dy])
      |        ^~~~~
/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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~
a.cc:90:15: error: reference to 'visit' is ambiguous
   90 |         cout<<visit[dx][dy]-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:36:5: note:                 'long long int visit [1000][1000]'
   36 | int visit[1000][1000];
      |     ^~~~~