#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int inf = 1e9;
int to[N], ww[N], nxt[N], tt = 1, a[210][210], h[N], dep[N], s, t, cur[N], n,
                          id[210][210], dx[4] = {1, 0, 0, -1},
                          dy[4] = {0, 1, -1, 0}, st[N], tp;
bool vis[N];
void adde(int u, int v, int w, bool b = 0) {
  to[++tt] = v;
  ww[tt] = w;
  nxt[tt] = h[u];
  h[u] = tt;
  to[++tt] = u;
  ww[tt] = b ? w : 0;
  nxt[tt] = h[v];
  h[v] = tt;
}
bool bfs() {
  memset(dep, 0, 4 * (t + 1));
  queue<int> q;
  q.push(s);
  dep[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = h[u], v; v = to[i], i; i = nxt[i])
      if (!dep[v] && ww[i]) {
        dep[v] = dep[u] + 1;
        q.push(v);
        if (v == t) return 1;
      }
  }
  return 0;
}
int dfs(int u, int flow) {
  if (u == t) return flow;
  int ret = 0;
  for (int &i = cur[u], v; v = to[i], i; i = nxt[i])
    if (dep[v] == dep[u] + 1 && ww[i]) {
      int d = dfs(v, min(flow, ww[i]));
      ww[i] -= d;
      ww[i ^ 1] += d;
      ret += d;
      flow -= d;
      if (!flow) break;
    }
  return ret;
}
int dinic() {
  int ret = 0;
  while (bfs()) {
    memcpy(cur, h, 4 * (t + 1));
    ret += dfs(s, inf);
  }
  return ret;
}
void getans(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  for (int i = h[u], v; v = to[i], i; i = nxt[i])
    if (ww[i]) getans(v);
}
struct dat {
  int x, y, k;
};
void solve(int l, int r, vector<dat> vec) {
  if (vec.empty()) return;
  if (l == r) {
    for (auto p : vec) a[p.x][p.y] = st[l];
    return;
  }
  int mid = (l + r) >> 1, cnt = 0;
  for (auto p : vec) id[p.x][p.y] = ++cnt;
  s = 0;
  t = cnt + 1;
  tt = 1;
  memset(h, 0, 4 * (t + 1));
  for (auto p : vec) {
    if (p.k >= 0) {
      adde(s, id[p.x][p.y], 0);
      adde(id[p.x][p.y], t, p.k);
    } else {
      adde(s, id[p.x][p.y], -p.k);
      adde(id[p.x][p.y], t, 0);
    }
    if (a[p.x][p.y] > 0) {
      if (a[p.x][p.y] <= st[mid])
        adde(id[p.x][p.y], t, inf);
      else
        adde(s, id[p.x][p.y], inf);
    }
    for (int d = 0; d < 2; d++) {
      int xx = p.x + dx[d], yy = p.y + dy[d];
      if (id[xx][yy] != -1) adde(id[p.x][p.y], id[xx][yy], 1, 1);
    }
  }
  dinic();
  memset(vis, 0, t + 1);
  getans(s);
  vector<dat> ls, rs;
  for (auto p : vec) {
    for (int d = 0; d < 4; d++) {
      int xx = p.x + dx[d], yy = p.y + dy[d];
      if (id[xx][yy] != -1) {
        if (!vis[id[p.x][p.y]] && vis[id[xx][yy]]) --p.k;
        if (vis[id[p.x][p.y]] && !vis[id[xx][yy]]) ++p.k;
      }
    }
    if (!vis[id[p.x][p.y]])
      ls.push_back(p);
    else
      rs.push_back(p);
  }
  for (auto p : vec) id[p.x][p.y] = -1;
  solve(l, mid, ls);
  solve(mid + 1, r, rs);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      if (~a[i][j]) st[++tp] = a[i][j];
    }
  sort(st + 1, st + tp + 1);
  tp = unique(st + 1, st + tp + 1) - st - 1;
  memset(id, -1, sizeof(id));
  vector<dat> vec;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j] != -1) vec.push_back((dat){i, j, 0});
  solve(1, tp, vec);
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j] != -1) {
        if (i < n && a[i + 1][j] != -1) ans += abs(a[i][j] - a[i + 1][j]);
        if (j < n && a[i][j + 1] != -1) ans += abs(a[i][j] - a[i][j + 1]);
      }
  cout << ans;
  return 0;
}
