#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, sgn = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') sgn = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
  return x * sgn;
}
const int N = 40010;
const int inf = 0x3f3f3f3f;
int n, m, S, T, val[N], h[N], cnt = 1, maxflow, a[210][210];
bool vis[N];
vector<int> v;
long long ans;
struct edge {
  int v, w, nxt;
} e[N << 5];
void link(int x, int y, int w) {
  e[++cnt] = (edge){y, w, h[x]};
  h[x] = cnt;
  e[++cnt] = (edge){x, 0, h[y]};
  h[y] = cnt;
}
int id(int x, int y) { return (x - 1) * m + y; }
bool cmp(int x, int y) { return val[x] < val[y]; }
int dfs(int x, int flow) {
  if (x == T) return flow;
  vis[x] = 1;
  int res = flow;
  for (int i = h[x], y; i && res; i = e[i].nxt) {
    if (!vis[y = e[i].v] && e[i].w) {
      int d = dfs(y, min(res, e[i].w));
      res -= d;
      e[i].w -= d;
      e[i ^ 1].w += d;
    }
  }
  return flow - res;
}
int rdfs(int x, int flow, int from) {
  if (x == S) return flow;
  vis[x] = 1;
  int res = flow;
  for (int i = h[x], y; i && res; i = e[i].nxt) {
    if (!vis[y = e[i].v] && e[i].w) {
      if (x == T && y != from) continue;
      int d = rdfs(y, min(res, e[i].w), from);
      res -= d;
      e[i].w -= d;
      e[i ^ 1].w += d;
    }
  }
  return flow - res;
}
void insert(int x) {
  int flow = 0;
  while (memset(vis, 0, sizeof(vis)), flow = rdfs(T, inf, x)) maxflow -= flow;
  for (int i = h[x]; i; i = e[i].nxt)
    if (e[i].v == T) {
      e[i].w = e[i ^ 1].w = 0;
      break;
    }
  link(S, x, inf);
  while (memset(vis, 0, sizeof(vis)), flow = dfs(S, inf)) maxflow += flow;
}
int main() {
  n = m = read();
  S = 0;
  T = n * m + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) a[i][j] = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] > 0)
        link(id(i, j), T, inf), v.push_back(id(i, j)), val[id(i, j)] = a[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] != -1) {
        if (j < m && a[i][j + 1] != -1)
          link(id(i, j), id(i, j + 1), 1), link(id(i, j + 1), id(i, j), 1);
        if (i < n && a[i + 1][j] != -1)
          link(id(i, j), id(i + 1, j), 1), link(id(i + 1, j), id(i, j), 1);
      }
  sort(v.begin(), v.end(), cmp);
  for (int i = 0; i + 1 < v.size(); i++)
    insert(v[i]), ans += (long long)maxflow * (val[v[i + 1]] - val[v[i]]);
  printf("%lld\n", ans);
  return 0;
}
