#include <bits/stdc++.h>
using namespace std;
void hantei(int x, int y, vector<vector<int>> &n) {
  if (n.at(x).at(y - 1) == 2) {
    n.at(x).at(y - 1) = 0;
    hantei(x, (y - 1), n);
  }
  n.at(x).at(y - 1) = 0;
  if (n.at(x).at(y + 1) == 2) {
    n.at(x).at(y + 1) = 0;
    hantei(x, (y + 1), n);
  }
  n.at(x).at(y + 1) = 0;
  if (n.at(x - 1).at(y) == 2) {
    n.at(x - 1).at(y) = 0;
    hantei((x - 1), y, n);
  }
  n.at(x - 1).at(y) = 0;
  if (n.at(x + 1).at(y) == 2) {
    n.at(x + 1).at(y) = 0;
    hantei((x + 1), y, n);
  }
  n.at(x + 1).at(y) = 0;
  return;
}
int main() {
  int32_t N, count, sum;
  cin >> N;
  vector<vector<int>> data((N + 2), vector<int>((N + 2)));
  sum = N * N;
  count = 0;
  vector<int> P(sum);
  for (int i = 0; i < N + 2; i++) {
    for (int j = 0; j < N + 2; j++) {
      if (i == 0 || i == 1 || i == N || i == (N + 1) || j == 0 || j == 1 ||
          j == N || j == (N + 1)) {
        data.at(i).at(j) = 0;
      } else {
        data.at(i).at(j) = 1;
      }
    }
  }
  for (int i = 0; i < sum; i++) {
    cin >> P.at(i);
  }
  for (int h = 0; h < sum; h++) {
    int k, l;
    k = (P.at(h) - 1) / N + 1;
    l = (P.at(h) - 1) % N + 1;
    if (data.at(k).at(l) == 1) {
      count++;
      data.at(k).at(l) = 2;
    } else {
      hantei(k, l, data);
    }
  }
  cout << count << endl;
}
