#include <bits/stdc++.h>
using namespace std;
const long long N = 205, M = N * N, inf = 0x7fffffff;
long long n, m, a[N][N], tot = 1, ver[M << 3], nxt[M << 3], edge[M << 3], h[M],
                         d[M], mxflow;
inline void add(long long x, long long y, long long z) {
  ver[++tot] = y, edge[tot] = z, nxt[tot] = h[x], h[x] = tot;
  ver[++tot] = x, edge[tot] = 0, nxt[tot] = h[y], h[y] = tot;
}
inline long long id(long long x, long long y) { return (x - 1) * m + y; }
queue<long long> q;
vector<pair<long long, long long>> vec;
bool bfs(long long s, long long t) {
  while (q.size()) q.pop();
  memset(d, 0, sizeof(d));
  q.push(s);
  d[s] = 1;
  while (q.size()) {
    long long u = q.front();
    q.pop();
    for (long long i = h[u]; i; i = nxt[i])
      if (edge[i] && !d[ver[i]]) {
        d[ver[i]] = d[u] + 1;
        if (ver[i] == t) return 1;
        q.push(ver[i]);
      }
  }
  return 0;
}
long long dinic(long long u, long long flow, long long t) {
  if (u == t) return flow;
  long long rest = flow;
  for (long long i = h[u]; i && rest; i = nxt[i])
    if (edge[i] && d[ver[i]] == d[u] + 1) {
      long long k = dinic(ver[i], min(rest, edge[i]), t);
      if (!k) d[ver[i]] = 0;
      edge[i] -= k, edge[i ^ 1] += k;
      rest -= k;
    }
  return flow - rest;
}
signed main() {
  ios ::sync_with_stdio(0);
  cin >> n;
  m = n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) cin >> a[i][j];
  long long s = 0, t = n * m + 1;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++)
      if (a[i][j] >= 0) {
        if (i + 1 <= n && a[i + 1][j] >= 0)
          add(id(i, j), id(i + 1, j), 1), add(id(i + 1, j), id(i, j), 1);
        if (j + 1 <= m && a[i][j + 1] >= 0)
          add(id(i, j), id(i, j + 1), 1), add(id(i, j + 1), id(i, j), 1);
        if (a[i][j] > 0) {
          add(id(i, j), t, inf);
          vec.push_back({a[i][j], id(i, j)});
        }
      }
  sort(vec.begin(), vec.end());
  long long sz = (long long)vec.size() - 1, ans = 0;
  for (long long i = 0; i < sz; i++) {
    long long flow, u = vec[i].second;
    for (long long i = h[u]; i; i = nxt[i])
      if (ver[i] == t) mxflow -= edge[i ^ 1], edge[i] = 0, edge[i ^ 1] = inf;
    add(s, u, inf);
    while (bfs(s, t))
      while (flow = dinic(s, inf, t)) mxflow += flow;
    ans += mxflow * (vec[i + 1].first - vec[i].first);
  }
  cout << ans << "\n";
  return 0;
}
