#include <bits/stdc++.h>
using ll = long long;
using pii = std::pair<int, int>;
const int maxn = 205 * 205;
const int inf = 1e8;
int n, a[205][205], P[maxn];
ll ans;
std::vector<pii> v;
int id(int x, int y) { return (x - 1) * n + y; }
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
struct edge {
  int v, c, nxt;
} E[maxn << 4];
namespace Flow {
int hd[maxn], vis[maxn], s, t, tl, MaxFlow;
struct edge {
  int v, nxt;
  int c;
} E[maxn << 4];
void init(int s1, int t1) {
  s = s1;
  t = t1;
}
void reset() {
  tl = 0;
  std::memset(hd, 0, sizeof(hd));
}
int rev(int x) { return ((x - 1) ^ 1) + 1; }
void addedge(int u, int v, int c) {
  E[++tl].v = v;
  E[tl].c = c;
  E[tl].nxt = hd[u];
  hd[u] = tl;
}
int flow(int x) { return E[rev(x)].c; }
void ban(int x) { E[x].c = E[rev(x)].c = 0; }
int add_edge(int u, int v, int c) {
  addedge(u, v, c);
  int x = tl;
  addedge(v, u, 0);
  return x;
}
int dfs(int u, int cap, int aim) {
  if (u == aim) return cap;
  int flow = 0;
  vis[u] = 1;
  for (int i = hd[u]; i; i = E[i].nxt) {
    if (E[i].c == 0 || vis[E[i].v]) continue;
    int f = dfs(E[i].v, std::min(cap, E[i].c), aim);
    E[i].c -= f;
    E[rev(i)].c += f;
    flow += f;
    if (f) return f;
  }
  return flow;
}
void Dinic() {
  while (1) {
    std::memset(vis, 0, sizeof(vis));
    int f = dfs(s, inf, t);
    MaxFlow += f;
    if (!f) break;
  }
}
void revert(int u) {
  int f = flow(P[u]);
  E[rev(P[u])].c -= f;
  E[P[u]].c = 0;
  while (f) {
    std::memset(vis, 0, sizeof(vis));
    int c = dfs(u, f, s);
    f -= c;
    MaxFlow -= c;
    if (c == 0) break;
  }
  E[P[u]].c = E[rev(P[u])].c = 0;
  add_edge(s, u, inf);
  Dinic();
}
}  // namespace Flow
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &a[i][j]);
      if (a[i][j] > 0) v.push_back({i, j});
    }
  std::sort(v.begin(), v.end(), [](pii x, pii y) {
    return a[x.first][x.second] < a[y.first][y.second];
  });
  int s = n * n + 1, t = n * n + 2;
  Flow::init(s, t);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      if (a[i][j] > 0) P[id(i, j)] = Flow::add_edge(id(i, j), t, inf);
      for (int d = 0; d < 4; ++d) {
        int x = i + dx[d], y = j + dy[d];
        if (1 <= x && x <= n && 1 <= y && y <= n && (a[x][y] > -1))
          Flow::add_edge(id(i, j), id(x, y), 1);
      }
    }
  for (int i = 0; i + 1 < v.size(); ++i) {
    auto p = v[i];
    int x = p.first, y = p.second;
    Flow::revert(id(x, y));
    ans += (ll)Flow::MaxFlow *
           (a[v[i + 1].first][v[i + 1].second] - a[v[i].first][v[i].second]);
  }
  printf("%lld", ans);
  return 0;
}
