Language: C++

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=100010;
const ll mod=1e9+7;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

vector<vector<int>> data(500, vector<int>(500,0));
int n;

int bfs(int s, int used[500][500]){
  used[s/n][s%n]=0;
  queue<P> q;
  q.push({s/n,s%n});
  int res = inf;
  while(!q.empty()){
    auto p = q.front(); q.pop();
    rep(i,4){
      int nx = p.F+dx[i];
      int ny = p.S+dy[i];
      if(nx<0 || n<=nx || ny<0 || n<=ny){ res = min(res, used[p.F][p.S]); if(res==0){return 0;} continue;}
      if(used[nx][ny]<=used[p.F][p.S] + (data[nx][ny]==1?1:0)){ continue; }
      used[nx][ny] = used[p.F][p.S] + (data[nx][ny]==1?1:0);
      q.push({nx,ny});
    }
  }
  return res;
}

int main(){
  
  cin >> n;
  vector<int> p(n*n);
  rep(i,n*n){
    cin >> p[i]; p[i]--;
  }
  ll ans= 0;
  rep(i,n*n){
    int used[500][500];
    rep(i,500)rep(j,500) used[i][j]=inf;
    ans += bfs(p[n*n-1-i], used);
    data[p[n*n-1-i]/n][p[n*n-1-i]%n] = 1;
  }
  cout << ans << endl;
  return 0;  
}

Compiler output:
a.cc: In function 'int bfs(int, int (*)[500])':
a.cc:48:42: error: reference to 'data' is ambiguous
   48 |       if(used[nx][ny]<=used[p.F][p.S] + (data[nx][ny]==1?1:0)){ continue; }
      |                                          ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:34:21: note:                 'std::vector<std::vector<int> > data'
   34 | vector<vector<int>> data(500, vector<int>(500,0));
      |                     ^~~~
a.cc:49:40: error: reference to 'data' is ambiguous
   49 |       used[nx][ny] = used[p.F][p.S] + (data[nx][ny]==1?1:0);
      |                                        ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:34:21: note:                 'std::vector<std::vector<int> > data'
   34 | vector<vector<int>> data(500, vector<int>(500,0));
      |                     ^~~~
a.cc: In function 'int main()':
a.cc:68:5: error: reference to 'data' is ambiguous
   68 |     data[p[n*n-1-i]/n][p[n*n-1-i]%n] = 1;
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:34:21: note:                 'std::vector<std::vector<int> > data'
   34 | vector<vector<int>> data(500, vector<int>(500,0));
      |                     ^~~~