#include <bits/stdc++.h>
const int P = 998244353;
using namespace std;
template <typename T>
inline void ckmax(T &a, T b) {
  (a < b) && (a = b);
}
template <typename T>
inline void ckmin(T &a, T b) {
  (a > b) && (a = b);
}
inline int mul(int a, int b) { return 1ll * a * b % P; }
inline int add(int a, int b) { return a + b >= P ? a + b - P : a + b; }
inline int sub(int a, int b) { return a - b >= 0 ? a - b : a - b + P; }
inline void mulmod(int &a, int b) { a = mul(a, b); }
inline void addmod(int &a, int b) { ((a += b) >= P) && (a -= P); }
inline void submod(int &a, int b) { ((a -= b) < 0) && (a += P); }
inline int ksm(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1) {
    if (b & 1) ans = 1ll * ans * a % P;
    a = 1ll * a * a % P;
  }
  return ans;
}
inline int inv(int a) { return ksm(a, P - 2); }
namespace FastIO {
const int SIZE = 1 << 16;
char buf[SIZE], obuf[SIZE], str[64];
int bi = SIZE, bn = SIZE, opt;
int read(char *s) {
  while (bn) {
    for (; bi < bn && buf[bi] <= ' '; bi++)
      ;
    if (bi < bn) break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  int sn = 0;
  while (bn) {
    for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
    if (bi < bn) break;
    bn = fread(buf, 1, SIZE, stdin), bi = 0;
  }
  s[sn] = 0;
  return sn;
}
bool read(int &x) {
  if (x) x = 0;
  int bf = 0, n = read(str);
  if (!n) return 0;
  int i = 0;
  if (str[i] == '-') bf = 1, i = 1;
  for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
  if (bf) x = -x;
  return 1;
}
void write(int x) {
  if (!x)
    obuf[opt++] = '0';
  else {
    if (x < 0) obuf[opt++] = '-', x = -x;
    int sn = 0;
    while (x) str[sn++] = x % 10 + '0', x /= 10;
    for (int i = sn - 1; i >= 0; i--) obuf[opt++] = str[i];
  }
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void write(char x) {
  obuf[opt++] = x;
  if (opt >= (SIZE >> 1)) {
    fwrite(obuf, 1, opt, stdout);
    opt = 0;
  }
}
void Fflush() {
  if (opt) fwrite(obuf, 1, opt, stdout);
  opt = 0;
}
};  // namespace FastIO
inline int read() {
  int x;
  FastIO::read(x);
  return x;
}
const int dx[] = {0, 1}, dy[] = {1, 0};
const int MN = 205 + 5;
int a[MN][MN], disc[MN * MN], t, n, m, id[MN * MN];
vector<int> pos[MN * MN];
struct Edge {
  int v, nxt, w;
};
struct Dinic {
  static const int N = MN * MN;
  static const int M = (MN * MN * 4 + MN * 4) * 3;
  int lst[N], tot, dep[N], cur[N], S, T, n;
  Edge e[M << 1];
  Dinic() { tot = 1; }
  void init(int tn, int ts, int tt) {
    S = ts, T = tt, n = tn;
    tot = 1;
    memset(lst, 0, sizeof(lst));
  }
  inline void addedge(int u, int v, int w) {
    e[++tot] = (Edge){v, lst[u], w};
    lst[u] = tot;
    e[++tot] = (Edge){u, lst[v], 0};
    lst[v] = tot;
  }
  inline void addedge2(int u, int v, int w) {
    e[++tot] = (Edge){v, lst[u], w};
    lst[u] = tot;
    e[++tot] = (Edge){u, lst[v], w};
    lst[v] = tot;
  }
  bool bfs(int s, int t) {
    queue<int> q;
    memset(dep, 0, sizeof(dep));
    dep[s] = 1, q.push(s);
    while (q.size()) {
      int u = q.front();
      q.pop();
      for (int i = lst[u]; i; i = e[i].nxt) {
        int v = e[i].v;
        if (!dep[v] && e[i].w) {
          dep[v] = dep[u] + 1, q.push(v);
          if (v == t) return 1;
        }
      }
    }
    return dep[t];
  }
  int dfs(int s, int t, int u, int nowflow) {
    if (u == t) return nowflow;
    int flow = 0;
    for (int &i = cur[u]; i; i = e[i].nxt) {
      int v = e[i].v, di = 0;
      if (dep[v] == dep[u] + 1 && (di = dfs(s, t, v, min(nowflow, e[i].w))))
        nowflow -= di, flow += di, e[i].w -= di, e[i ^ 1].w += di;
      if (!nowflow) break;
    }
    return flow;
  }
  int solve(int s, int t, int fl = ((1 << 30) - 1)) {
    int maxflow = 0, di;
    while (bfs(s, t)) {
      memcpy(cur, lst, sizeof(cur));
      while (di = dfs(s, t, s, fl)) maxflow += di, fl -= di;
      if (!fl) break;
    }
    return maxflow;
  }
  void print() {
    cerr << "?";
    for (int u = (0); u <= (n); u++) {
      for (int i = lst[u]; i; i = e[i].nxt) {
        int v = e[i].v;
        cerr << u << ' ' << v << ' ' << e[i].w << endl;
      }
    }
  }
} d;
inline int Hash(int i, int j) { return (i - 1) * m + j; }
signed main() {
  n = read(), m = n;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) {
      a[i][j] = read();
      if (a[i][j] != -1 && a[i][j] != 0) {
        disc[++t] = a[i][j];
      }
    }
  sort(disc + 1, disc + 1 + t);
  t = unique(disc + 1, disc + 1 + t) - disc - 1;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++)
      if (a[i][j] != -1 && a[i][j] != 0)
        a[i][j] = lower_bound(disc + 1, disc + 1 + t, a[i][j]) - disc;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) pos[a[i][j]].push_back(Hash(i, j));
  long long ans = 0;
  d.init(n * m + 1, 0, n * m + 1);
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) {
      if ((i == 1) + (j == 1) + (i == n) + (j == m) == 0) continue;
      d.addedge(Hash(i, j), n * m + 1, ((1 << 30) - 1));
      id[Hash(i, j)] = d.tot;
    }
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) {
      if (a[i][j] == -1) continue;
      for (int k = (0); k <= (1); k++) {
        int x = i + dx[k], y = j + dy[k];
        if (x < 1 || y < 1 || x > n || y > m || a[x][y] == -1) continue;
        d.addedge2(Hash(x, y), Hash(i, j), 1);
      }
    }
  int now = 0;
  for (int v = (1); v <= (t - 1); v++) {
    for (auto it : pos[v]) {
      int tmp;
      now -= (tmp = d.e[id[it]].w);
      d.e[id[it]].w = d.e[id[it] ^ 1].w = 0;
    }
    for (auto it : pos[v]) d.addedge(0, it, ((1 << 30) - 1));
    now += d.solve(0, n * m + 1);
    ans += 1ll * (disc[v + 1] - disc[v]) * now;
  }
  cout << ans << endl;
  return FastIO::Fflush(), 0;
}
