#include <bits/stdc++.h>
using namespace std;
const int N = 200 + 7, INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;
int n;
bool S[N][N], T[N][N], vis[N][N], ok[N][N][4];
int d[] = {0, 1, 0, -1};
int val[N][N];
int dfs(int x, int y, bool to[N][N]) {
  vis[x][y] = 1;
  if (to[x][y]) {
    return 1;
  }
  for (int i = 0; i < 4; ++i) {
    if (!ok[x][y][i]) {
      continue;
    }
    int nx = x + d[i], ny = y + d[i ^ 1];
    if (to == S && ok[nx][ny][i ^ 2]) {
      continue;
    }
    if (nx < 0 || nx >= n || ny < 0 || ny >= n || vis[nx][ny]) {
      continue;
    }
    if (dfs(nx, ny, to)) {
      if (ok[nx][ny][i ^ 2]) {
        ok[x][y][i] = 0;
      } else {
        ok[nx][ny][i ^ 2] = 1;
      }
      return 1;
    }
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> val[i][j];
      if (val[i][j] > 0) {
        v.push_back({i, j});
        T[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (val[i][j] == -1) {
        continue;
      }
      for (int k = 0; k < 4; ++k) {
        int nx = i + d[k], ny = j + d[k ^ 1];
        if (nx < 0 || nx >= n || ny < 0 || ny >= n || val[nx][ny] == -1) {
          continue;
        }
        ok[i][j][k] = 1;
      }
    }
  }
  sort(v.begin(), v.end(), [&](pair<int, int> &x, pair<int, int> &y) {
    return val[x.first][x.second] < val[y.first][y.second];
  });
  long long ans = 0;
  int flow = 0;
  for (int i = 0; i < v.size() - 1; ++i) {
    int x = v[i].first, y = v[i].second;
    while (dfs(x, y, S)) {
      memset(vis, 0, sizeof(vis));
      flow--;
    }
    memset(vis, 0, sizeof(vis));
    T[x][y] = 0;
    S[x][y] = 1;
    for (int j = 0; j < v.size(); j++) {
      int sx = v[j].first, sy = v[j].second;
      if (S[sx][sy]) {
        while (dfs(sx, sy, T)) {
          memset(vis, 0, sizeof(vis));
          flow++;
        }
      }
    }
    memset(vis, 0, sizeof(vis));
    long long cost =
        val[v[i + 1].first][v[i + 1].second] - val[v[i].first][v[i].second];
    ans += cost * flow;
  }
  cout << ans << endl;
}
