#include <bits/stdc++.h>
using namespace std;
const int MAXA = 1000000001;
const int MAXN = 500 + 16;
const long long INF = 6000000000000000000;
vector<pair<int, int> > di(4);
int N;
int gr[MAXN][MAXN];
void solve() {
  cin >> N;
  vector<vector<int> > num(N, vector<int>(N, -1));
  vector<vector<int> > vis(N, vector<int>(N, 0));
  for (int i = 0; i < int(N * N); ++i) {
    cin >> gr[i / N][i % N];
  }
  di[0] = {-1, 0};
  di[1] = {1, 0};
  di[2] = {0, -1};
  di[3] = {0, 1};
  queue<pair<int, int> > q;
  for (int j = 0; j < int((N / 2) + 1); ++j) {
    for (int t = int(j); t < int(N - j); ++t) {
      if (!vis[j][t]) {
        num[j][t] = j;
        vis[j][t] = 1;
        q.push({j, t});
      }
      if (!vis[t][j]) {
        num[t][j] = j;
        vis[t][j] = 1;
        q.push({t, j});
      }
      if (!vis[N - 1 - j][t]) {
        num[N - 1 - j][t] = j;
        vis[N - 1 - j][t] = 1;
        q.push({N - 1 - j, t});
      }
      if (!vis[t][N - 1 - j]) {
        num[t][N - 1 - j] = j;
        vis[t][N - 1 - j] = 1;
        q.push({t, N - 1 - j});
      }
      num[t][j] = 0;
      num[N - 1 - j][t] = 0;
      num[t][N - 1 - j] = 0;
    }
    pair<int, int> current;
    while (!q.empty()) {
      current = q.front();
      q.pop();
      for (int h = 0; h < int(4); ++h) {
        if (current.first + di[h].first < N &&
            current.first + di[h].first >= 0 &&
            current.second + di[h].second < N &&
            current.second + di[h].second >= 0) {
          int n = current.first + di[h].first;
          int m = current.second + di[h].second;
          if (gr[n][m] > gr[current.first][current.second]) {
            q.push({n, m});
            vis[n][m] = 1;
            num[n][m] = num[current.first][current.second];
          }
        }
      }
    }
  }
  long long suma = 0;
  for (int i = 0; i < int(N); ++i) {
    for (int j = 0; j < int(N); ++j) {
      suma += num[i][j];
    }
  }
  cout << suma << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
