#include <bits/stdc++.h>
using namespace std;
const int N = 203, M = N * N, K = M << 3;
template <typename T>
void read(T &x) {
  int ch = getchar();
  x = 0;
  bool f = false;
  for (; ch < '0' || ch > '9'; ch = getchar()) f |= ch == '-';
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  if (f) x = -x;
}
int n, S, T, a[N][N], head[M], to[K], nxt[K], cap[K], res, cnt = 1;
vector<int> val, opr[N << 2];
long long ans;
int id(int x, int y) { return (x - 1) * n + y; }
void add(int a, int b, int c) {
  to[++cnt] = b;
  nxt[cnt] = head[a];
  head[a] = cnt;
  cap[cnt] = c;
}
void add(int a, int b, int c1, int c2) {
  add(a, b, c1);
  add(b, a, c2);
}
int dep[M], cur[M], qr[M], fr, re;
bool bfs() {
  memset(dep, -1, sizeof dep);
  memcpy(cur, head, sizeof cur);
  dep[S] = 0;
  qr[fr = 0] = S;
  re = 1;
  while (fr < re) {
    int u = qr[fr++];
    for (int i = head[u]; i; i = nxt[i])
      if (cap[i] && !~dep[to[i]]) {
        dep[to[i]] = dep[u] + 1;
        qr[re++] = to[i];
      }
  }
  return ~dep[T];
}
int dfs(int x, int lim) {
  if (x == T || !lim) return lim;
  int flow = 0, f;
  for (int &i = cur[x]; i; i = nxt[i])
    if (dep[to[i]] == dep[x] + 1 && (f = dfs(to[i], min(lim, cap[i])))) {
      flow += f;
      lim -= f;
      cap[i] -= f;
      cap[i ^ 1] += f;
      if (!lim) break;
    }
  return flow;
}
int main() {
  read(n);
  S = n * n + 1;
  T = S + 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      read(a[i][j]);
      if (a[i][j] > 0) val.emplace_back(a[i][j]);
    }
  sort(val.begin(), val.end());
  val.erase(unique(val.begin(), val.end()), val.end());
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (~a[i][j]) {
        if (a[i][j] > 0) {
          add(id(i, j), T, M, 0);
          opr[lower_bound(val.begin(), val.end(), a[i][j]) - val.begin()]
              .emplace_back(cnt);
        }
        if (i > 1 && ~a[i - 1][j]) add(id(i, j), id(i - 1, j), 1, 1);
        if (j > 1 && ~a[i][j - 1]) add(id(i, j), id(i, j - 1), 1, 1);
      }
  for (int i = 0; i + 1 < val.size(); ++i) {
    for (int j : opr[i]) {
      res -= cap[j];
      cap[j] = cap[j ^ 1] = 0;
      add(S, to[j], M, 0);
    }
    while (bfs()) res += dfs(S, 1e9);
    ans += (long long)res * (val[i + 1] - val[i]);
  }
  printf("%lld\n", ans);
}
