Language: C++

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

bool exist[500][500];
bool zero[500][500];
bool visit[500][500];
int n;
int reach(int x, int y) {
    if (visit[x][y]) return 9999;
    if (x<0 || x>=n || y<0 || y>=n) {
        return 0;
    }
    if (zero[x][y]) return 0;

    visit[x][y]=true;
    int mini =      reach(x+1,y);
    if (mini>0) mini = min(mini, reach(x-1,y));
    if (mini>0) mini = min(mini, reach(x,y+1));
    if (mini>0) mini = min(mini, reach(x,y-1));
    visit[x][y]=false;

    if (exist[x][y]) mini++;
    if (mini==0) zero[x][y]=true;
    return mini;
}


int main() {
    cin >> n;

    rep(i,n) {
        rep(j,n) {
            exist[i][j]=true;
            zero[i][j]=false;
            visit[i][j]=false;
        }
    }

    int ans = 0;
    rep(i,n*n) {
        int p;
        cin >> p;
        int x=(p-1)/n, y=(p-1)%n;
        exist[x][y]=false;
        int t = reach(x,y);
        ans += t;
        //cout << p << ":" << t << endl;
    }

    cout << ans << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int reach(int, int)':
a.cc:14:9: error: reference to 'visit' is ambiguous
   14 |     if (visit[x][y]) return 9999;
      |         ^~~~~
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:11:6: note:                 'bool visit [500][500]'
   11 | bool visit[500][500];
      |      ^~~~~
a.cc:20:5: error: reference to 'visit' is ambiguous
   20 |     visit[x][y]=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:11:6: note:                 'bool visit [500][500]'
   11 | bool visit[500][500];
      |      ^~~~~
a.cc:25:5: error: reference to 'visit' is ambiguous
   25 |     visit[x][y]=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:11:6: note:                 'bool visit [500][500]'
   11 | bool visit[500][500];
      |      ^~~~~
a.cc: In function 'int main()':
a.cc:40:13: error: reference to 'visit' is ambiguous
   40 |             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:11:6: note:                 'bool visit [500][500]'
   11 | bool visit[500][500];
      |      ^~~~~