#include <bits/stdc++.h>
using namespace std;
int cnt2 = 0;
int en = 0;
struct Dinic {
  static const int MXN = 100000;
  struct Edge {
    int v, f, re;
    Edge(int a, int b, int c) : v(a), f(b), re(c) {}
  };
  int n, s, t, level[MXN];
  vector<Edge> E[MXN];
  void init(int _n, int _s, int _t) {
    n = _n;
    s = _s;
    t = _t;
    for (int i = 0; i <= n; i++) E[i].clear();
  }
  void add_edge(int u, int v, int f, int flag = 0) {
    en += 2;
    if (flag) {
      E[u].push_back(Edge(v, f, E[v].size()));
      E[v].push_back(Edge(u, f, E[u].size() - 1));
    } else {
      E[u].push_back(Edge(v, f, E[v].size()));
      E[v].push_back(Edge(u, 0, E[u].size() - 1));
    }
  }
  bool BFS() {
    memset(level, -1, sizeof(level));
    queue<int> que;
    que.push(s);
    level[s] = 0;
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (auto &it : E[u]) {
        cnt2++;
        if (it.f > 0 && level[it.v] == -1) {
          level[it.v] = level[u] + 1;
          que.push(it.v);
        }
      }
    }
    return level[t] != -1;
  }
  int DFS(int u, int nf) {
    if (u == t) return nf;
    int res = 0;
    for (auto &it : E[u]) {
      cnt2++;
      if (it.f > 0 && level[it.v] == level[u] + 1) {
        int tf = DFS(it.v, min(nf, it.f));
        res += tf;
        nf -= tf;
        it.f -= tf;
        E[it.v][it.re].f += tf;
        if (nf == 0) return res;
      }
    }
    if (!res) level[u] = -1;
    return res;
  }
  int flow(int res = 0) {
    while (BFS()) {
      res += DFS(s, 2147483647);
    }
    return res;
  }
} flow;
int vis[100005];
bool dfs(int first) {
  if (vis[first]) return false;
  if (first == 0) return true;
  vis[first] = 1;
  for (auto &it : flow.E[first]) {
    cnt2++;
    if (it.f && dfs(it.v)) {
      it.f--;
      flow.E[it.v][it.re].f++;
      return true;
    }
  }
  return false;
}
bool dfs2(int first) {
  if (vis[first]) return false;
  if (first == flow.t) return true;
  vis[first] = 1;
  for (auto &it : flow.E[first]) {
    cnt2++;
    if (it.f && dfs2(it.v)) {
      it.f--;
      flow.E[it.v][it.re].f++;
      return true;
    }
  }
  return false;
}
void solve() {
  int n;
  scanf("%d", &n);
  int a[205][205];
  flow.init(n * n * 2 + 2, 0, n * n * 2 + 1);
  int X[4] = {0, 0, -1, 1};
  int Y[4] = {1, -1, 0, 0};
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] != -1) {
        flow.add_edge(i * n + j + 1, i * n + j + n * n + 1, 1000000, 1);
        for (int k = 0; k < 4; k++) {
          int first = X[k] + i, second = Y[k] + j;
          if (first >= 0 && first < n && second >= 0 && second < n &&
              a[first][second] != -1 &&
              (a[i][j] == 0 || a[first][second] == 0)) {
            flow.add_edge(i * n + j + 1, first * n + second + n * n + 1, 1);
          }
        }
      }
    }
  }
  vector<pair<int, pair<int, int> > > v;
  for (int i = 1; i < n - 1; i++) {
    v.push_back(make_pair(a[0][i], make_pair(0, i)));
    flow.add_edge(n * n + i + 1, n * n * 2 + 1, 1);
    v.push_back(make_pair(a[n - 1][i], make_pair(n - 1, i)));
    flow.add_edge(n * n + i + (n - 1) * n + 1, n * n * 2 + 1, 1);
    v.push_back(make_pair(a[i][0], make_pair(i, 0)));
    flow.add_edge(n * n + i * n + 1, n * n * 2 + 1, 1);
    v.push_back(make_pair(a[i][n - 1], make_pair(i, n - 1)));
    flow.add_edge(n * n + i * n + n - 1 + 1, n * n * 2 + 1, 1);
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans += abs(a[0][i + 1] - a[0][i]);
    ans += abs(a[i][0] - a[i + 1][0]);
    ans += abs(a[n - 1][i] - a[n - 1][i + 1]);
    ans += abs(a[i][n - 1] - a[i + 1][n - 1]);
  }
  sort(v.begin(), v.end());
  long long now = 0;
  long long last = 0;
  int tot = 0;
  int cnt = 0;
  for (auto it : v) {
    fill(vis, vis + 100005, 0);
    while (dfs2(0)) {
      now++;
      fill(vis, vis + 100005, 0);
    }
    ans += now * (it.first - last);
    last = it.first;
    int first = it.second.first * n + it.second.second + 1;
    flow.add_edge(0, first, 1);
    for (auto &it : flow.E[first + n * n]) {
      if (it.v == n * n * 2 + 1) {
        if (it.f == 1) {
          it.f = 0;
        } else {
          flow.E[it.v][it.re].f = 0;
          now--;
          fill(vis, vis + 100005, 0);
          dfs(first + n * n);
        }
      }
    }
  }
  printf("%lld\n", ans);
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
