Language: C++

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;


vector<string> vec_splitter(string s) {
    s += ',';
    vector<string> res;
    while(!s.empty()) {
        res.push_back(s.substr(0, s.find(',')));
        s = s.substr(s.find(',') + 1);
    }
    return res;
}
void debug_out(
        vector<string> __attribute__ ((unused)) args,
        __attribute__ ((unused)) int idx,
        __attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
    if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)((x).size())
#define pb push_back
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
//#define debug(x) cerr << #x << " : " << (x) << " "
const double PI = 3.14159265358979323846;
const double eps = (1e-8);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

int n;
const int MAX_N = 505;
int dist[MAX_N][MAX_N];
bool danger[MAX_N][MAX_N], visit[MAX_N][MAX_N];

int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};

bool valid(int i, int j)
{
    return i >= 0 && j >= 0 && i < n && j < n && !visit[i][j];
}

deque<pair<int, int> > dq;
int bfs(int sti, int stj)
{
    rep(i,0,n)
    {
        rep(j, 0, n)
        {
            dist[i][j] = 1e9;
            visit[i][j] = false;
        }
    }
    dist[sti][stj] = 0;

    dq.clear();
    dq.push_back({sti, stj});


    for (int size = sz(dq); !dq.empty(); size = sz(dq))
    {
        while(size--)
        {
            pair<int, int> u = dq.front();
            dq.pop_front();
            int i = u.first, j = u.second;

            if (visit[i][j])
                continue;

            if (!i || !j || i == n - 1|| j == n - 1)
                return dist[i][j];

            visit[i][j] = true;

            rep(mv, 0, 4)
            {
                int ni = i + di[mv], nj = j + dj[mv];
                if (valid(ni, nj))
                {
                    int w = danger[ni][nj];
                    if (dist[ni][nj] > dist[i][j] + w)
                    {
                        dist[ni][nj] = dist[i][j] + w;
                        if (w)
                            dq.push_back({ni, nj});
                        else
                        {
                            dq.push_front({ni, nj});
                            size++;
                        }
                    }
                }
            }
        }
    }
    assert(false);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    clr(danger, 1);
    int ans = 0;
    rep(k,0,n * n)
    {
        int x;
        cin >> x;
        --x;
        int sti = x/n, stj = x%n;
        danger[sti][stj] = false;
        ans += bfs(sti, stj);
    }

    cout << ans << '\n';
}

Compiler output:
a.cc: In function 'bool valid(int, int)':
a.cc:69:51: error: reference to 'visit' is ambiguous
   69 |     return i >= 0 && j >= 0 && i < n && j < n && !visit[i][j];
      |                                                   ^~~~~
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:62:28: note:                 'bool visit [505][505]'
   62 | bool danger[MAX_N][MAX_N], visit[MAX_N][MAX_N];
      |                            ^~~~~
a.cc: In function 'int bfs(int, int)':
a.cc:80:13: error: reference to 'visit' is ambiguous
   80 |             visit[i][j] = false;
      |             ^~~~~
/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:62:28: note:                 'bool visit [505][505]'
   62 | bool danger[MAX_N][MAX_N], visit[MAX_N][MAX_N];
      |                            ^~~~~
a.cc:97:17: error: reference to 'visit' is ambiguous
   97 |             if (visit[i][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:62:28: note:                 'bool visit [505][505]'
   62 | bool danger[MAX_N][MAX_N], visit[MAX_N][MAX_N];
      |                            ^~~~~
a.cc:103:13: error: reference to 'visit' is ambiguous
  103 |             visit[i][j] = true;
      |             ^~~~~
/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:62:28: note:                 'bool visit [505][505]'
   62 | bool danger[MAX_N][MAX_N], visit[MAX_N][MAX_N];
      |                            ^~~~~