Language: C++

#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;
const double PI=3.1415926535897932;

ll H, W, h1, w1, h2, w2, ans=-1, tmp=0;
string S;
ll dy[] = {1, 0, -1, 0};
ll dx[] = {0, 1, 0, -1};
vector<ll> visit;
vector<vector<ll>> color, G;
vector<vector<char>> A;

ll dfs(ll h, ll w, ll id){
    for(int i=0;i<4;i++){
        int ny = h + dy[i], nx = w + dx[i];
        if(!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
        if(A[ny][nx]=='.'&&color[ny][nx]==-1){
            color[ny][nx]=id;
            dfs(ny, nx, id);
        }
    }
}

void bfs(ll bd1, ll bd2) {
    queue<pair<ll,ll>> Q;
    Q.push(make_pair(bd1, 0));
    while(!Q.empty()) {
        pair<ll,ll> p = Q.front();
        Q.pop();
        if(visit[p.first]!=-1) continue;
        visit[p.first] = p.second;
        if(p.first==bd2) {
            ans = p.second;
            break;
        }
        for(int i=0;i<G[p.first].size();i++) {
            if(visit[G[p.first][i]]==-1) Q.push(make_pair(G[p.first][i], p.second+1));
        }
    }
}

signed main(){
    cin >> H >> W >> h1 >> w1 >> h2 >> w2;
    h1--;
    w1--;
    h2--;
    w2--;
    A.resize(H);
    color.resize(H);
    for(int i=0;i<H;i++) {
        color[i].resize(W, -1);
        for(int j=0;j<W;j++) {
            char a;
            cin >> a;
            A[i].push_back(a);
        }
    }
    for(int i=0;i<H;i++) {
        for(int j=0;j<W;j++) {
            if(A[i][j]=='.'&&color[i][j]==-1) {
                color[i][j] = tmp;
                dfs(i, j, tmp);
                tmp++;
            }
        }
    }
    G.resize(tmp);
    visit.resize(tmp, -1);
    //cout << "OK" << endl;
    /*
    for(int i=0;i<H;i++) {
        for(int j=0;j<W;j++) {
            cout << color[i][j] << " ";
        }
        cout << endl;
    }
    */
    for(int i=0;i<H;i++) {
        for(int j=0;j<W;j++) {
            for(int l=i-2;l<i+3;l++) {
                if(l>=0&&l<H) {
                    for(int k=j-2;k<j+3;k++) {
                        if(k>=0&&k<W) {
                            if(color[l][k]!=-1&&color[i][j]!=-1&&color[l][k]!=color[i][j]) {                                
                                G[color[i][j]].push_back(color[l][k]);
                                G[color[l][k]].push_back(color[i][j]);                                
                            }
                        }
                    }
                }
            }
        }
    }
    //cout << "OK" << endl;
    if(color[h1][w1]!=-1&&color[h2][w2]!=-1) bfs(color[h1][w1], color[h2][w2]);
    cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'll dfs(ll, ll, ll)':
a.cc:28:1: warning: no return statement in function returning non-void [-Wreturn-type]
   28 | }
      | ^
a.cc: In function 'void bfs(ll, ll)':
a.cc:36:12: error: reference to 'visit' is ambiguous
   36 |         if(visit[p.first]!=-1) continue;
      |            ^~~~~
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:15:12: note:                 'std::vector<long long int> visit'
   15 | vector<ll> visit;
      |            ^~~~~
a.cc:37:9: error: reference to 'visit' is ambiguous
   37 |         visit[p.first] = p.second;
      |         ^~~~~
/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:15:12: note:                 'std::vector<long long int> visit'
   15 | vector<ll> visit;
      |            ^~~~~
a.cc:43:16: error: reference to 'visit' is ambiguous
   43 |             if(visit[G[p.first][i]]==-1) Q.push(make_pair(G[p.first][i], p.second+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:15:12: note:                 'std::vector<long long int> visit'
   15 | vector<ll> visit;
      |            ^~~~~
a.cc: In function 'int main()':
a.cc:74:5: error: reference to 'visit' is ambiguous
   74 |     visit.resize(tmp, -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:15:12: note:                 'std::vector<long long int> visit'
   15 | vector<ll> visit;
      |            ^~~~~