#include <bits/stdc++.h>
using namespace std;
const int Maxx = 100005, INF = 0x3f3f3f3f;
inline int read() {
  int res = 0, bj = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') bj |= (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
  return bj ? -res : res;
}
inline int Abs(const int x) { return x > 0 ? x : -x; }
struct node {
  int x, y;
  inline node(int a = 0, int b = 0) { x = a, y = b; }
  inline bool operator<(const node &tp) const { return x < tp.x; }
} P[805];
int tot, a[205][205], N[205][205];
char v[40005];
const int M = 65535;
int S, T, cnt = 1, h[40005], d[40005], pr[40005], Q[M + 1];
struct edge {
  int to, nx, vl, wl;
} E[166005];
inline void Add(int x, int y, int v, int w, int p = 0) {
  E[++cnt].nx = h[x], E[h[x] = cnt].to = y, E[cnt].vl = v, E[cnt].wl = w;
  E[++cnt].nx = h[y], E[h[y] = cnt].to = x, E[cnt].vl = p, E[cnt].wl = -w;
}
inline bool Spfa() {
  memset(d, -0x3f, sizeof(int) * (T + 1));
  unsigned int l = 0, r = 0;
  int x;
  d[Q[++r] = S] = 0;
  while (l != r) {
    v[x = Q[(++l) &= M]] = 0;
    for (int i = h[x], y; i; i = E[i].nx)
      if (E[i].vl && d[y = E[i].to] < d[x] + E[i].wl) {
        d[y = E[i].to] = d[x] + E[i].wl, pr[y] = i;
        if (!v[y]) {
          v[y] = 1;
          if (d[Q[(l + 1) & M]] <= d[y])
            Q[l] = y, (--l) &= M;
          else
            Q[(++r) &= M] = y;
        }
      }
  }
  return d[T] > 0;
}
signed main() {
  int n = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) a[i][j] = read();
  for (int i = 2; i < n; ++i)
    for (int j = 2; j < n; ++j)
      if (~a[i][j]) N[i][j] = ++tot;
  S = ++tot, T = ++tot, tot = 0;
  for (int i = 2; i < n; ++i) {
    if (N[2][i]) P[++tot] = node(a[1][i], N[2][i]);
    if (N[n - 1][i]) P[++tot] = node(a[n][i], N[n - 1][i]);
  }
  for (int i = 2; i < n; ++i) {
    if (N[i][2]) P[++tot] = node(a[i][1], N[i][2]);
    if (N[i][n - 1]) P[++tot] = node(a[i][n], N[i][n - 1]);
  }
  sort(P + 1, P + tot + 1);
  for (int i = 1; i <= tot; ++i) Add(S, P[i].y, 1, P[i].x);
  for (int i = 1; i <= tot; ++i) Add(P[i].y, T, 1, -P[i].x);
  for (int i = 2; i < n; ++i)
    for (int j = 2; j < n; ++j)
      if (N[i][j]) {
        if (N[i][j + 1]) Add(N[i][j], N[i][j + 1], 1, 0, 1);
        if (N[i + 1][j]) Add(N[i][j], N[i + 1][j], 1, 0, 1);
      }
  long long Ans = 0;
  for (int i = 2; i <= n; ++i) {
    Ans += Abs(a[1][i] - a[1][i - 1]) + Abs(a[n][i] - a[n][i - 1]);
    Ans += Abs(a[i][1] - a[i - 1][1]) + Abs(a[i][n] - a[i - 1][n]);
  }
  while (Spfa()) {
    Ans += d[T];
    for (int P = T; P != S; P = E[P].to) --E[P = pr[P]].vl, ++E[P ^= 1].vl;
  }
  cout << Ans << "\n";
  return 0;
}
