#include <bits/stdc++.h>
using namespace std;
const int inf = 1e5;
int begin1[1000010], ok = 0, total, hh = 0, st[1000010], p[1010][1010],
                     next1[1000010], to1[1000010], e = 1, dis[1000010],
                     q[1000010], x[1000010], y[1000010], t, w[1000010],
                     d[1000010], vis[1000010], a[1010][1010], s, z;
vector<int> u[1010];
void add(int x, int y, int z) {
  to1[++e] = y;
  next1[e] = begin1[x];
  begin1[x] = e;
  w[e] = z;
  to1[++e] = x;
  next1[e] = begin1[y];
  begin1[y] = e;
  w[e] = 0;
}
int dfs(int x, int y, int sum) {
  int i;
  if (x == y) {
    if (ok) {
      int h = min(w[hh], sum);
      w[hh] -= h;
      w[hh ^ 1] += h;
      return h;
    }
    return sum;
  }
  total++;
  int ans = 0;
  for (i = begin1[x]; i; i = next1[i])
    if (w[i] && dis[to1[i]] == dis[x] + 1) {
      int h = dfs(to1[i], y, min(sum, w[i]));
      w[i] -= h;
      w[i ^ 1] += h;
      sum -= h;
      ans += h;
      if (!sum) break;
    }
  if (ans == 0) dis[x] = 0;
  return ans;
}
int bfs(int x, int y) {
  int i;
  for (i = 1; i <= t; i++) dis[i] = 0;
  dis[x] = 1;
  int r = 0, l = 1;
  st[r] = x;
  int flag = 0;
  while (r < l) {
    int xx = st[r];
    r++;
    for (i = begin1[xx]; i; i = next1[i]) {
      if (!dis[to1[i]] && w[i] && (xx != s || (!vis[to1[i]])) && to1[i] != s) {
        dis[to1[i]] = dis[xx] + 1;
        if (dis[y] && dis[to1[i]] > dis[y])
          dis[to1[i]] = -inf;
        else {
          st[l] = to1[i];
          l++;
        }
      }
      if (xx == y && to1[i] == s && w[i]) {
        flag = 1;
        z = w[i];
        hh = i;
      }
    }
  }
  if (y != t)
    ok = 1;
  else
    flag = 1;
  return dis[y] && flag;
}
int main() {
  int n, i, j, tot = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);
      if (i == 1 || i == n || j == 1 || j == n) q[++tot] = a[i][j];
    }
  sort(q + 1, q + tot + 1);
  int l1 = unique(q + 1, q + tot + 1) - q - 1;
  tot = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (i == 1 || i == n || j == 1 || j == n) {
        tot++;
        x[tot] = i;
        y[tot] = j;
        p[i][j] = tot;
      }
  for (i = 1; i <= tot; i++) {
    int h = lower_bound(q + 1, q + l1 + 1, a[x[i]][y[i]]) - q;
    u[h].push_back(i);
  }
  s = n * n + 1;
  t = n * n + 2;
  for (i = 1; i <= tot; i++) add(s, i, inf);
  for (i = 2; i < n; i++)
    for (j = 2; j < n; j++) p[i][j] = ++tot;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) {
      if (i != n && a[i][j] != -1 && a[i + 1][j] != -1) {
        add(p[i][j], p[i + 1][j], 1);
        add(p[i + 1][j], p[i][j], 1);
      }
      if (j != n && a[i][j] != -1 && a[i][j + 1] != -1) {
        add(p[i][j], p[i][j + 1], 1);
        add(p[i][j + 1], p[i][j], 1);
      }
    }
  int H = 0;
  long long ans = 0;
  for (i = 1; i < l1; i++) {
    int h = u[i].size();
    for (j = 0; j < h; j++) {
      while (bfs(t, u[i][j])) {
        total = 0;
        H -= dfs(t, u[i][j], z);
      }
      vis[u[i][j]] = 1;
    }
    for (j = 0; j < h; j++) add(u[i][j], t, inf);
    ok = 0;
    while (bfs(s, t)) {
      H += dfs(s, t, inf);
    }
    ans += 1ll * H * (q[i + 1] - q[i]);
  }
  printf("%lld\n", ans);
}
