#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1, c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f == 1 ? x : -x;
}
const int N = 40004, inf = 0x3f3f3f3f;
struct edge {
  int v, f, nxt;
} e[N * 10];
int first[N], cur[N], cnt = 1;
inline void Add(int u, int v, int f) {
  e[++cnt].v = v;
  e[cnt].f = f;
  e[cnt].nxt = first[u];
  first[u] = cnt;
}
inline void addedge(int u, int v, int f) {
  Add(u, v, f);
  Add(v, u, 0);
}
int s, t, flow, dep[N];
queue<int> q;
inline bool bfs() {
  while (!q.empty()) q.pop();
  memset(dep, 0x3f, sizeof(dep));
  dep[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = first[x], v; i; i = e[i].nxt) {
      v = e[i].v;
      if (!e[i].f || dep[v] <= dep[x] + 1) continue;
      dep[v] = dep[x] + 1;
      if (v == t) return 1;
      q.push(v);
    }
  }
  return 0;
}
int dfs(int x, int f) {
  if (x == t || !f) return f;
  int used = 0;
  for (int &i = cur[x], v, w; i; i = e[i].nxt) {
    v = e[i].v;
    if (!e[i].f || dep[v] != dep[x] + 1) continue;
    w = dfs(v, min(f, e[i].f));
    if (!w) continue;
    e[i].f -= w;
    e[i ^ 1].f += w;
    f -= w;
    used += w;
    if (!f) return used;
  }
  return used;
}
inline int dinic() {
  int res = 0;
  while (bfs()) {
    memcpy(cur, first, sizeof(first));
    res += dfs(s, inf);
  }
  return res;
}
long long ans;
int n, m, a[N], c[N];
inline int id(int x, int y) { return (x - 1) * m + y; }
inline bool comp(int x, int y) { return a[x] < a[y]; }
inline void insert(int x) {
  for (int i = first[x]; i; i = e[i].nxt)
    if (e[i].v == t) {
      addedge(t + 1, x, e[i ^ 1].f);
      e[i].f = e[i ^ 1].f = 0;
      break;
    }
  s = t + 1;
  t--;
  flow -= dinic();
  s = n * m + 1;
  t = s + 1;
  addedge(s, x, inf);
  flow += dinic();
}
int main() {
  n = m = read();
  s = n * m + 1;
  t = s + 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      a[id(i, j)] = read();
      if (a[id(i, j)] > 0) {
        addedge(id(i, j), t, inf);
        c[++c[0]] = id(i, j);
      }
      if (i > 1 && a[id(i, j)] != -1 && a[id(i - 1, j)] != -1) {
        addedge(id(i, j), id(i - 1, j), 1);
        addedge(id(i - 1, j), id(i, j), 1);
      }
      if (j > 1 && a[id(i, j)] != -1 && a[id(i, j - 1)] != -1) {
        addedge(id(i, j), id(i, j - 1), 1);
        addedge(id(i, j - 1), id(i, j), 1);
      }
    }
  }
  sort(c + 1, c + c[0] + 1, comp);
  for (int i = 1; i < c[0]; i++) {
    insert(c[i]);
    ans += (long long)(a[c[i + 1]] - a[c[i]]) * flow;
  }
  cout << ans << "\n";
  return (0 - 0);
}
