Language: C++

#include <iostream>
#include<vector>
using namespace std;

int dfsUtil(vector<vector<char>>&v ,pair<int, int> src, pair<int, int> dest, vector<vector<bool>> visited)
{
    if (src == dest)
        return 0;
    int x = src.first, y = src.second;
    visited[x][y] = true;
    unsigned int a = INT_MAX, b = INT_MAX, c = INT_MAX, d = INT_MAX;
    if(x != 0 && !visited[x-1][y] && v[x-1][y] == '.')
        a = dfsUtil(v, {x-1,y}, dest, visited);

    if(x != v.size()-1 && !visited[x+1][y] && v[x+1][y] == '.')
        b = dfsUtil(v, {x+1,y}, dest, visited);
    
    if(y != 0 && !visited[x][y-1] && v[x][y-1] == '.')
        c =  dfsUtil(v, {x,y-1}, dest, visited);
    
    if(y != v[0].size()-1 && !visited[x][y+1] && v[x][y+1] == '.')
        d = dfsUtil(v, {x,y+1}, dest, visited);
    
    a =  min(min(a,b), min(c,d));
    if (a == INT_MAX)
    {
        for(int i = x - 2; i <= x + 2; i++)
        {
            for(int j = y - 2; j <= y+2; j++)
            {
                if(i < 0 || i >= v.size()  || j < 0 || j >= v[0].size())
                    continue;
                else if (v[i][j] == '#')
                    continue;
                else if (v[i][j] == '.' && !visited[i][j])
                    a = min(a, (unsigned int)(dfsUtil(v, {i,j}, dest, visited) + 1));
            }
        }
    }
    return a;
}

int dfs(vector<vector<char>> &v, pair<int , int> src, pair<int , int> dest, int h, int w)
{
    vector<vector<bool>> visited;
    for(int i = 0; i < h; i++)
    {
        vector<bool> temp;
        for(int j = 0; j < w; j++)
            temp.push_back(false);
        visited.push_back(temp);
    }
    int ans = dfsUtil(v, src, dest, visited);
    return ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int h, w;
    cin >> h >> w;
    pair<int, int> src;
    cin >> src.first >> src.second;
    pair<int, int> dest;
    cin >> dest.first >> dest.second;

    vector<vector<char>> v;
    for(int i = 0; i < h ;i++)
    {
        vector<char> temp(w);
        for(int j = 0; j < w; j++)
        {
            cin >> temp[j];
        }
        v.push_back(temp);
    }
    src.first--;
    src.second--;
    dest.first--;
    dest.second--;
    int ans = dfs(v, src, dest, h, w);
    if(ans == INT_MAX)
        ans = -1;
    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int dfsUtil(std::vector<std::vector<char> >&, std::pair<int, int>, std::pair<int, int>, std::vector<std::vector<bool> >)':
a.cc:11:22: error: 'INT_MAX' was not declared in this scope
   11 |     unsigned int a = INT_MAX, b = INT_MAX, c = INT_MAX, d = INT_MAX;
      |                      ^~~~~~~
a.cc:3:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    2 | #include<vector>
  +++ |+#include <climits>
    3 | using namespace std;
a.cc:16:9: error: 'b' was not declared in this scope
   16 |         b = dfsUtil(v, {x+1,y}, dest, visited);
      |         ^
a.cc:19:9: error: 'c' was not declared in this scope
   19 |         c =  dfsUtil(v, {x,y-1}, dest, visited);
      |         ^
a.cc:22:9: error: 'd' was not declared in this scope
   22 |         d = dfsUtil(v, {x,y+1}, dest, visited);
      |         ^
a.cc:24:20: error: 'b' was not declared in this scope
   24 |     a =  min(min(a,b), min(c,d));
      |                    ^
a.cc:24:28: error: 'c' was not declared in this scope
   24 |     a =  min(min(a,b), min(c,d));
      |                            ^
a.cc:24:30: error: 'd' was not declared in this scope
   24 |     a =  min(min(a,b), min(c,d));
      |                              ^
a.cc: In function 'int main()':
a.cc:84:15: error: 'INT_MAX' was not declared in this scope
   84 |     if(ans == INT_MAX)
      |               ^~~~~~~
a.cc:84:15: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'