Language: C++

/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>

#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>

#include <cmath>
#include <numeric>
#include <random>
#include <cstring>
#include <cassert>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

// type
#include <cstdint>
#include <functional>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

/* ---------- Functions */
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

int table[500][500];
bool empty[500][500];
int N;

void f(int y, int x) {
    for (int i= 0; i < 4; i++) {
        int yy = y + dy[i];
        int xx = x + dx[i];
        if (yy < 0 || xx < 0 || yy >= N || xx >= N) continue;
        int val = table[y][x] + (empty[y][x] ? 0 : 1);
        if (table[yy][xx] > val) {
            table[yy][xx] = val;
            f(yy, xx);
        }
    }
}

signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> N;
    vector<int> A(N*N);
    for (int i = 0; i < N*N; i++) cin >> A[i];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            table[i][j] = min({i, j, N-1-i, N-1-j});
        }
    }

    int ret = 0;

    for (int i = 0; i < N*N; i++) {
        int y = (A[i]-1) / N;
        int x = (A[i]-1) % N;
        ret += table[y][x];
        empty[y][x] = true;
        f(y, x);
    }

    cout << ret << endl;
    return 0;
}

Compiler output:
a.cc: In function 'void f(ll, ll)':
a.cc:68:34: error: reference to 'empty' is ambiguous
   68 |         int val = table[y][x] + (empty[y][x] ? 0 : 1);
      |                                  ^~~~~
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/istream:40,
                 from /usr/include/c++/14/fstream:40,
                 from a.cc:5:
/usr/include/c++/14/bits/range_access.h:302:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(initializer_list<_Tp>)'
  302 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:292:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  292 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:282:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  282 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
a.cc:60:6: note:                 'bool empty [500][500]'
   60 | bool empty[500][500];
      |      ^~~~~
a.cc: In function 'int main()':
a.cc:96:9: error: reference to 'empty' is ambiguous
   96 |         empty[y][x] = true;
      |         ^~~~~
/usr/include/c++/14/bits/range_access.h:302:5: note: candidates are: 'template<class _Tp> constexpr bool std::empty(initializer_list<_Tp>)'
  302 |     empty(initializer_list<_Tp> __il) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:292:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr bool std::empty(const _Tp (&)[_Nm])'
  292 |     empty(const _Tp (&)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:282:5: note:                 'template<class _Container> constexpr decltype (__cont.empty()) std::empty(const _Container&)'
  282 |     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
      |     ^~~~~
a.cc:60:6: note:                 'bool empty [500][500]'
   60 | bool empty[500][500];
      |      ^~~~~