Language: C++

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

char maze[1010][1010];
int visit[1010][1010];
int H, W, ch, cw, dh, dw;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int ans = 1e9;
bool ok = false;

void dfs(int h, int w, int cnt)
{
    // cout << h << " " << w << endl;
    if (h == dh && w == dw)
    {
        ans = min(ans, cnt);
        ok = true;
    }
    visit[h][w] = 1;
    //move_A
    rep(i, 4)
    {
        int nh = h + dy[i];
        int nw = w + dx[i];
        if (maze[nh][nw] == '#')
            continue;
        if (visit[nh][nw] == 1)
            continue;
        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
            continue;
        dfs(nh, nw, cnt);
    }
    //move_B
    for (int i = h - 2; i <= h + 2; i++)
    {
        for (int j = w - 2; j <= w + 2; j++)
        {
            if (maze[i][j] == '#')
                continue;
            if (visit[i][j] == 1)
                continue;
            if (i < 0 || i >= H || j < 0 || j >= W)
                continue;
            cnt++;
            dfs(i, j, cnt);
        }
    }
}

int main()
{
    cin >> H >> W >> ch >> cw >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    rep(i, H) rep(j, W) cin >> maze[i][j];
    dfs(ch, cw, 0);
    if (ok)
        cout << ans << endl;
    else
        cout << -1 << endl;
    return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int, int)':
a.cc:23:5: error: reference to 'visit' is ambiguous
   23 |     visit[h][w] = 1;
      |     ^~~~~
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:8:5: note:                 'int visit [1010][1010]'
    8 | int visit[1010][1010];
      |     ^~~~~
a.cc:31:13: error: reference to 'visit' is ambiguous
   31 |         if (visit[nh][nw] == 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:8:5: note:                 'int visit [1010][1010]'
    8 | int visit[1010][1010];
      |     ^~~~~
a.cc:44:17: error: reference to 'visit' is ambiguous
   44 |             if (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:8:5: note:                 'int visit [1010][1010]'
    8 | int visit[1010][1010];
      |     ^~~~~