Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FOR0(i, n) for (i = 0; i < n; i++)
#define FOR(i, j, n) for (i = j; i < n; i++)
#define FORD(i, j, k) for (i = j; i >= k; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 1000000000
#define ninf -1000000000
#define endl '\n'
#define she_told_me_dont_worry                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2
// source.cpp
inline long long MAX2(long long a, long long int b) {
  return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
  return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
const int N = 505;
int n;
int grid[N][N] = {};
int l[N][N] = {};
int p[N * N];
int xx[5] = {0, 0, -1, 1};
int yy[5] = {-1, 1, 0, 0};
int dfs(int i, int d) {
  int r = i / n, c = i % n;
  int j;
  FOR0(j, 4) {
    int xd = r + xx[j], yd = c + yy[j];
    if (xd >= 0 && xd < n && yd >= 0 && yd < n && d < grid[xd][yd]) {
      grid[xd][yd] = d;
      int nd = d;
      if (!l[xd][yd])
        nd++;
      dfs(xd * n + yd, nd);
    }
  }
}
int main() {
  she_told_me_dont_worry cin >> n;
  int i, j;
  FOR0(i, n * n) {
    cin >> p[i];
    p[i]--;
  }
  FOR0(i, n)
  FOR0(j, n)
  grid[i][j] = MIN2(MIN2(i, n - 1 - i), MIN2(j, n - 1 - j));
  int ans = 0;
  FOR0(i, n * n) {
    int r = p[i] / n, c = p[i] % n;
    l[r][c] = 1;
    ans += grid[r][c];
    dfs(p[i], grid[r][c]);
  }
  cout << ans << endl;
  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FOR0(i, n) for (i = 0; i < n; i++)
#define FOR(i, j, n) for (i = j; i < n; i++)
#define FORD(i, j, k) for (i = j; i >= k; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 1000000000
#define ninf -1000000000
#define endl '\n'
#define she_told_me_dont_worry                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2
// source.cpp
inline long long MAX2(long long a, long long int b) {
  return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
  return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
const int N = 505;
int n;
int grid[N][N] = {};
int l[N][N] = {};
int p[N * N];
int xx[5] = {0, 0, -1, 1};
int yy[5] = {-1, 1, 0, 0};
void dfs(int i, int d) {
  int r = i / n, c = i % n;
  int j;
  FOR0(j, 4) {
    int xd = r + xx[j], yd = c + yy[j];
    if (xd >= 0 && xd < n && yd >= 0 && yd < n && d < grid[xd][yd]) {
      grid[xd][yd] = d;
      int nd = d;
      if (!l[xd][yd])
        nd++;
      dfs(xd * n + yd, nd);
    }
  }
}
int main() {
  she_told_me_dont_worry cin >> n;
  int i, j;
  FOR0(i, n * n) {
    cin >> p[i];
    p[i]--;
  }
  FOR0(i, n)
  FOR0(j, n)
  grid[i][j] = MIN2(MIN2(i, n - 1 - i), MIN2(j, n - 1 - j));
  int ans = 0;
  FOR0(i, n * n) {
    int r = p[i] / n, c = p[i] % n;
    l[r][c] = 1;
    ans += grid[r][c];
    dfs(p[i], grid[r][c]);
  }
  cout << ans << endl;
  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
}