#include <bits/stdc++.h>
using namespace std;
template <class Type>
long long quick_pow(Type x, Type y, Type Mod) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % Mod;
    x = x * x % Mod, y >>= 1;
  }
  return res;
}
template <class Type>
long long inv(Type x, Type Mod) {
  return quick_pow(x, Mod - 2, Mod);
}
namespace IO {
template <class T>
inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
};  // namespace IO
using namespace IO;
const int N = 90015;
const int M = 2000015;
const int INF = 1e9;
struct E {
  int v, next, cap;
} e[M];
int ecnt = 1;
struct V {
  int head, cur;
} v[N];
int vcnt, dep[N], pre[N];
inline void add(int x, int y, int w, int t = 0) {
  e[++ecnt].v = y, e[ecnt].cap = w;
  e[ecnt].next = v[x].head, v[x].head = ecnt;
  e[++ecnt].v = x, e[ecnt].cap = t;
  e[ecnt].next = v[y].head, v[y].head = ecnt;
}
int bfs(int st, int ed) {
  for (int i = (1); i <= (vcnt); ++i) dep[i] = 0;
  queue<int> q;
  dep[st] = 1, q.push(st);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (x == ed) break;
    for (int i = v[x].head; i; i = e[i].next) {
      if (!dep[e[i].v] && e[i].cap > 0) {
        dep[e[i].v] = dep[x] + 1, q.push(e[i].v);
        pre[e[i].v] = i;
      }
    }
  }
  if (!dep[ed]) return 0;
  for (int x = ed, i; x != st; x = e[i ^ 1].v) {
    i = pre[x];
    e[i].cap--;
    e[i ^ 1].cap++;
  }
  return 1;
}
int maxflow(int st, int ed, int res = 4) {
  int flow = 0, tempflow;
  while (res) {
    tempflow = bfs(st, ed);
    if (!tempflow) return flow;
    flow += tempflow, res -= tempflow;
  }
  return flow;
}
vector<pair<int, int>> vec, dt;
int n;
const int Mtx = 305;
int a[Mtx][Mtx], id[Mtx][Mtx], head[N];
int main() {
  read(n);
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (n); ++j) {
      read(a[i][j]);
      if (a[i][j] != -1) id[i][j] = ++vcnt;
      if (a[i][j] > 0) vec.push_back(make_pair(a[i][j], id[i][j]));
    }
  int S, T;
  S = ++vcnt, T = ++vcnt;
  sort(vec.begin(), vec.end());
  for (auto y : vec) add(y.second, T, 4), head[y.second] = ecnt;
  dt.push_back(make_pair(1, 0)), dt.push_back(make_pair(0, 1));
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (n); ++j)
      if (id[i][j]) {
        for (auto y : dt)
          if (id[i + y.first][j + y.second]) {
            add(id[i][j], id[i + y.first][j + y.second], 1, 1);
          }
      }
  long long flow = 0, ans = 0;
  for (int i = (0); i <= ((int)vec.size() - 2); ++i) {
    int x = vec[i].second;
    long long y = e[head[x]].cap;
    e[head[x]].cap = 0, e[head[x] ^ 1].cap = 0;
    flow -= maxflow(x, S, y);
    add(S, x, 4);
    flow += maxflow(S, T);
    ans += flow * (vec[i + 1].first - vec[i].first);
  }
  cout << ans;
  return 0;
}
