#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 + 5;
int vals[MAXN][MAXN];
int available[MAXN][MAXN][4];
int dt[4] = {0, 1, 0, -1};
int N;
bool sources[MAXN][MAXN];
bool sinks[MAXN][MAXN];
bool visited[MAXN][MAXN];
void reset_visited() {
  for (int x = 0; x < N; x++)
    for (int y = 0; y < N; y++) visited[x][y] = false;
}
bool dfs(int x, int y, bool is_goal[MAXN][MAXN]) {
  visited[x][y] = true;
  if (is_goal[x][y]) return true;
  for (int t = 0; t < 4; t++) {
    if (!available[x][y][t]) continue;
    int x1 = x + dt[t];
    int y1 = y + dt[t ^ 1];
    if (is_goal == sources and available[x1][y1][t ^ 2]) continue;
    if (visited[x1][y1]) continue;
    if (dfs(x1, y1, is_goal)) {
      if (available[x1][y1][t ^ 2])
        available[x][y][t] = false;
      else
        available[x1][y1][t ^ 2] = true;
      return true;
    }
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<pair<int, int>> tiles;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) cin >> vals[i][j];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      if (vals[i][j] >= 1) {
        tiles.push_back({i, j});
        sinks[i][j] = true;
      }
    }
  for (int x = 0; x < N; x++)
    for (int y = 0; y < N; y++) {
      if (vals[x][y] == -1) continue;
      for (int t = 0; t < 4; t++) {
        int x1 = x + dt[t];
        int y1 = y + dt[t ^ 1];
        if (x1 < 0 or x1 >= N or y1 < 0 or y1 >= N) continue;
        if (vals[x1][y1] == -1) continue;
        available[x][y][t] = true;
      }
    }
  sort(tiles.begin(), tiles.end(), [&](pair<int, int> A, pair<int, int> B) {
    return vals[A.first][A.second] < vals[B.first][B.second];
  });
  long long res = 0;
  int flow = 0;
  for (int it = 0; it < ((int)((tiles).size())) - 1; it++) {
    int x = tiles[it].first, y = tiles[it].second;
    while (dfs(x, y, sources)) {
      reset_visited();
      flow--;
    }
    reset_visited();
    sinks[x][y] = false;
    sources[x][y] = true;
    for (auto pp : tiles) {
      int i = pp.first, j = pp.second;
      if (sources[i][j] and !visited[i][j]) {
        while (dfs(i, j, sinks)) {
          reset_visited();
          flow++;
        }
      }
    }
    reset_visited();
    long long cost =
        vals[tiles[it + 1].first][tiles[it + 1].second] - vals[x][y];
    res += cost * flow;
  }
  cout << res << endl;
}
