#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const long long inf = 1ll << 50;
namespace mincost {
const int V = 1234567, E = 1234567;
int vis[V], ti, g[V], et;
long long r[V];
int vtot, s, t;
long long infc = 1e18, inff = 1e18;
struct edge {
  int v, ne;
  long long f, c;
} e[E * 2];
void init(int _s, int _t, int _n) {
  s = _s;
  t = _t;
  vtot = _n;
  for (int i = 0; i < vtot; i++) g[i] = -1;
  et = 0;
}
void add(int u, int v, long long f, long long c) {
  e[et] = {v, g[u], f, c};
  g[u] = et++;
  e[et] = {u, g[v], 0, -c};
  g[v] = et++;
}
int d[V], cu[V];
void spfa() {
  static int qu[V], in[V];
  int p = 0, q = 0;
  for (int i = 0; i < vtot; i++) r[i] = infc, cu[i] = g[i];
  r[s] = 0;
  qu[q++] = s, in[s] = 1;
  while (p != q) {
    int u = qu[p++];
    if (p == V) p = 0;
    in[u] = 0;
    for (int i = g[u]; ~i; i = e[i].ne) {
      int v = e[i].v;
      if (e[i].f && r[v] > r[u] + e[i].c) {
        r[v] = r[u] + e[i].c;
        if (!in[v]) {
          in[v] = 1;
          qu[q++] = v;
          if (q == V) q = 0;
        }
      }
    }
  }
}
void dij() {
  static pair<long long, int> qu[E * 2];
  int tq = 0;
  static long long d[V];
  for (int i = 0; i < vtot; i++) d[i] = infc, cu[i] = g[i];
  d[s] = 0;
  qu[tq++] = make_pair(0, s);
  push_heap(qu, qu + tq);
  while (tq) {
    auto x = qu[0];
    pop_heap(qu, qu + tq);
    --tq;
    int u = x.second;
    if (d[u] != -x.first) continue;
    for (int i = g[u]; ~i; i = e[i].ne) {
      long long w = d[u] + e[i].c + r[u] - r[e[i].v];
      if (e[i].f && w < d[e[i].v]) {
        d[e[i].v] = w;
        qu[tq++] = make_pair(-d[e[i].v], e[i].v);
        push_heap(qu, qu + tq);
      }
    }
  }
  for (int i = 0; i < vtot; i++) r[i] += d[i];
}
long long aug(int u, long long m) {
  if (u == t) return m;
  long long su = 0, f;
  vis[u] = ti;
  for (int i = cu[u]; ~i; cu[u] = i = e[i].ne)
    if (e[i].f && vis[e[i].v] != ti && r[u] + e[i].c == r[e[i].v]) {
      f = aug(e[i].v, min(m, e[i].f));
      e[i].f -= f;
      e[i ^ 1].f += f;
      m -= f;
      su += f;
      if (!m) break;
    }
  if (su) vis[u] = -1;
  return su;
}
long long mcmf() {
  for (int i = 0; i < vtot; i++) r[i] = 0;
  spfa();
  long long co = 0, flo = 0, f;
  while (ti++, dij(), f = aug(s, inff)) {
    if (r[t] > 0) break;
    flo += f;
    co += r[t] * f;
  }
  return co;
}
}  // namespace mincost
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  mincost::init(n * n, n * n + 1, n * n + 2);
  int src = n * n, snk = n * n + 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int v = i * n + j;
      if (j + 1 < n && i > 0 && i < n - 1 && a[i][j] != -1 &&
          a[i][j + 1] != -1) {
        mincost::add(v, v + 1, 1, 0);
        mincost::add(v + 1, v, 1, 0);
      }
      if (i + 1 < n && j > 0 && j < n - 1 && a[i][j] != -1 &&
          a[i + 1][j] != -1) {
        mincost::add(v, v + n, 1, 0);
        mincost::add(v + n, v, 1, 0);
      }
      if (a[i][j] > 0) {
        mincost::add(src, v, 1, a[i][j]);
        mincost::add(v, snk, 1, -a[i][j]);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i + 1 < n; ++i) {
    ans += abs(a[0][i] - a[0][i + 1]);
    ans += abs(a[n - 1][i] - a[n - 1][i + 1]);
    ans += abs(a[i][0] - a[i + 1][0]);
    ans += abs(a[i][n - 1] - a[i + 1][n - 1]);
  }
  cout << ans - mincost::mcmf() << "\n";
  return 0;
}
