Language: C++

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Yes() std::cout << "Yes" << std::endl
#define No() std::cout << "No" << std::endl
#define Say(s) std::cout << s << std::endl
const ll MOD = 1000000007;
const ll INF = 1061109567;

template <class T>
using Grid = std::vector<std::vector<T>>;
void dfs_grid(const ll& start_tate, const ll& start_yoko, Grid<char>& grid_char, Grid<bool>& grid_num, ll number)
{
    grid_num[start_tate][start_yoko] = number;
    //grid_char[start_tate][start_yoko] = '#';  //'.'を虱潰しにしたい時に使う
    for (ll dx = -1; dx <= 1; ++dx) {
        for (ll dy = -1; dy <= 1; ++dy) {
            if (0 <= start_tate + dx && start_tate + dx < grid_seen.size() && 0 <= start_yoko + dy && start_yoko + dy < grid_seen[0].size()) {
                if (grid_num[start_tate + dx][start_yoko + dy] > number && grid_char[start_tate + dx][start_yoko + dy] == '.') {
                    dfs_grid(start_tate + dx, start_yoko + dy, grid_char, grid_num, number);
                }
            }
        }
    }
    for (ll dx = -2; dx <= 2; ++dx) {
        for (ll dy = -2; dy <= 2; ++dy) {
            if (0 <= start_tate + dx && start_tate + dx < grid_seen.size() && 0 <= start_yoko + dy && start_yoko + dy < grid_seen[0].size()) {
                if (grid_num[start_tate + dx][start_yoko + dy] > number + 1 && grid_char[start_tate + dx][start_yoko + dy] == '.') {
                    dfs_grid(start_tate + dx, start_yoko + dy, grid_char, grid_num, number + 1);
                }
            }
        }
    }
}
int main()
{
    ll h, w;
    ll C_h, C_w, D_h, D_w;
    std::cin >> h >> w >> C_h >> C_w >> D_h >> D_w;
    --C_h;
    --C_w;
    --D_h;
    --D_w;
    Grid<ll> N(h, std::vector<ll>(w, INF));
    Grid<char> S(h, std::vector<char>(w));
    for (ll i = 0; i < h; ++i) {
        std::string temp;
        std::cin >> temp;
        for (ll j = 0; j < w; ++j) {
            S[i][j] = temp[j];
        }
    }
    dfs_grid(C_h, C_w, S, N, 0);

    if (N[D_h][D_w] > inf / 2) {
        std::cout << "-1" << std::endl;
    } else {
        std::cout << N[D_h][D_w] << std::endl;
    }

    return 0;
}


Compiler output:
a.cc: In function 'void dfs_grid(const long long int&, const long long int&, Grid<char>&, Grid<bool>&, long long int)':
a.cc:19:59: error: 'grid_seen' was not declared in this scope
   19 |             if (0 <= start_tate + dx && start_tate + dx < grid_seen.size() && 0 <= start_yoko + dy && start_yoko + dy < grid_seen[0].size()) {
      |                                                           ^~~~~~~~~
a.cc:28:59: error: 'grid_seen' was not declared in this scope
   28 |             if (0 <= start_tate + dx && start_tate + dx < grid_seen.size() && 0 <= start_yoko + dy && start_yoko + dy < grid_seen[0].size()) {
      |                                                           ^~~~~~~~~
a.cc: In function 'int main()':
a.cc:54:27: error: invalid initialization of reference of type 'Grid<bool>&' {aka 'std::vector<std::vector<bool, std::allocator<bool> >, std::allocator<std::vector<bool, std::allocator<bool> > > >&'} from expression of type 'Grid<long long int>' {aka 'std::vector<std::vector<long long int, std::allocator<long long int> >, std::allocator<std::vector<long long int, std::allocator<long long int> > > >'}
   54 |     dfs_grid(C_h, C_w, S, N, 0);
      |                           ^
a.cc:13:94: note: in passing argument 4 of 'void dfs_grid(const long long int&, const long long int&, Grid<char>&, Grid<bool>&, long long int)'
   13 | void dfs_grid(const ll& start_tate, const ll& start_yoko, Grid<char>& grid_char, Grid<bool>& grid_num, ll number)
      |                                                                                  ~~~~~~~~~~~~^~~~~~~~
a.cc:56:23: error: 'inf' was not declared in this scope; did you mean 'ynf'?
   56 |     if (N[D_h][D_w] > inf / 2) {
      |                       ^~~
      |                       ynf