#include <bits/stdc++.h>
using namespace std;
int a[210][210];
int iS[40010], iT[40010], vis[40010];
int hed[40010], tot = 1, mxfl;
long long ans;
int n;
vector<pair<int, int>> v;
inline int idx(int x, int y) { return (x - 1) * n + y; }
struct edg {
  int to, nxt, fl;
} e[40010 << 2];
void adde(int x, int y) {
  e[++tot] = (edg){y, hed[x], 0};
  hed[x] = tot;
  e[++tot] = (edg){x, hed[y], 0};
  hed[y] = tot;
}
int push(int x, int fl) {
  if (iT[x] && fl == +1) return 1;
  if (iS[x] && fl == -1) return 1;
  vis[x] = 1;
  for (int i = hed[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    if (fl == 1 && e[i].fl == 1) continue;
    if (fl == -1 && e[i].fl != -1) continue;
    if (vis[y]) continue;
    if (push(y, fl)) {
      e[i].fl++;
      e[i ^ 1].fl--;
      return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j] >= 0) {
        if (i + 1 <= n && a[i + 1][j] >= 0) adde(idx(i, j), idx(i + 1, j));
        if (j + 1 <= n && a[i][j + 1] >= 0) adde(idx(i, j), idx(i, j + 1));
        if (a[i][j] > 0) {
          iT[idx(i, j)] = 1;
          v.push_back(make_pair(a[i][j], idx(i, j)));
        }
      }
  sort(v.begin(), v.end());
  int sz = v.size();
  for (int i = 0; i <= sz - 2; i++) {
    int x = v[i].second;
    memset(vis, 0, sizeof(vis));
    while (push(x, -1)) {
      mxfl--;
      memset(vis, 0, sizeof(vis));
    }
    iT[x] = 0;
    iS[x] = 1;
    memset(vis, 0, sizeof(vis));
    for (int j = 0; j <= i; j++)
      if (!vis[v[j].second])
        while (push(v[j].second, 1)) mxfl++, memset(vis, 0, sizeof(vis));
    ans += 1ll * (v[i + 1].first - v[i].first) * mxfl;
  }
  printf("%lld", ans);
  return 0;
}
