#include <bits/stdc++.h>
int n, m, a[(205 * 205)];
int hd[(205 * 205)], _hd = 1;
struct edge {
  int v, f, nxt;
} e[(205 * 205) << 2];
inline void addedge(int u, int v) {
  e[++_hd] = (edge){v, 0, hd[u]};
  hd[u] = _hd;
  e[++_hd] = (edge){u, 0, hd[v]};
  hd[v] = _hd;
}
std::pair<int, int> val[(205 * 205)];
int tot;
bool S[(205 * 205)], T[(205 * 205)], vis[(205 * 205)];
inline bool dfs(int u, int tp) {
  if (T[u] && tp == 1) return 1;
  if (S[u] && tp == -1) return 1;
  vis[u] = 1;
  for (int i = hd[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (vis[v]) continue;
    if (tp == 1 && e[i].f == 1) continue;
    if (tp == -1 && e[i].f != -1) continue;
    if (dfs(v, tp)) {
      e[i].f++;
      e[i ^ 1].f--;
      return 1;
    }
  }
  return 0;
}
int flw;
inline void sol(int u, int tp) {
  while (dfs(u, tp)) {
    flw += tp;
    memset(vis, 0, sizeof(vis));
  }
}
long long ans;
int main() {
  scanf("%d", &n);
  m = n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[(((i)-1) * m + (j))]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (i > 1 && a[(((i)-1) * m + (j))] != -1 &&
          a[(((i - 1) - 1) * m + (j))] != -1)
        addedge((((i)-1) * m + (j)), (((i - 1) - 1) * m + (j)));
      if (j > 1 && a[(((i)-1) * m + (j))] != -1 &&
          a[(((i)-1) * m + (j - 1))] != -1)
        addedge((((i)-1) * m + (j)), (((i)-1) * m + (j - 1)));
      if (a[(((i)-1) * m + (j))] > 0) {
        T[(((i)-1) * m + (j))] = 1;
        val[++tot] =
            std::make_pair(a[(((i)-1) * m + (j))], (((i)-1) * m + (j)));
      }
    }
  std::sort(val + 1, val + tot + 1);
  for (int i = 1; i < tot; i++) {
    int u = val[i].second;
    memset(vis, 0, sizeof(vis));
    sol(u, -1);
    T[u] = 0, S[u] = 1;
    memset(vis, 0, sizeof(vis));
    for (int j = 1; j <= i; j++)
      if (!vis[val[j].second]) sol(val[j].second, 1);
    ans += 1ll * flw * (val[i + 1].first - val[i].first);
  }
  printf("%lld\n", ans);
}
