#include <bits/stdc++.h>   using namespace std;   using ll = long long;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   namespace mincost {   const int N = 1e5 + 10, M = 5e5 + 10;   int vis[N], dfn, head[N], cnt, Cnt, S, T, inf = 1e9, dis[N], cur[N];   ll Dis[N], INF = 1e18;   struct Edge {     int v, next;     ll f, c;   } e[M << 1];   inline void init(int _s, int _t, int _n) {     S = _s;     T = _t;     Cnt = _n;     for (register int i = 0; i < Cnt; ++i) head[i] = -1;     cnt = 0;   }   inline void add(int u, int v, ll ff, ll f, ll c, ll f2 = 0) {     e[cnt] = {v, head[u], f, c};     head[u] = cnt++;     e[cnt] = {u, head[v], f2, -c};     head[v] = cnt++;   }   inline void spfa() {     static int que[N];     static bool vis[N];     int p = 0, q = 0;     for (register int i = 0; i < Cnt; ++i) Dis[i] = inf;     Dis[S] = 0;     que[q++] = S;     vis[S] = 1;     while (p != q) {       int u = que[p++];       if (p == N) p = 0;       vis[u] = 0;       for (register int i = head[u]; ~i; i = e[i].next) {         int v = e[i].v, w = e[i].c;         if (e[i].f && Dis[v] > Dis[u] + w) {           Dis[v] = Dis[u] + w;           if (!vis[v]) {             vis[v] = 1;             que[q++] = v;             if (q == N) q = 0;           }         }       }     }   }   inline void Dijkstra() {     static pair<ll, ll> que[M];     static ll dis[N];     for (register int i = 0; i < Cnt; ++i) {       dis[i] = inf;       cur[i] = head[i];     }     dis[S] = 0;     int Head = 0;     que[Head++] = make_pair(0, S);     push_heap(que, que + Head);     while (Head > 0) {       auto x = que[0];       pop_heap(que, que + Head);       --Head;       int u = x.second;       if (dis[u] != -x.first) continue;       for (register int i = head[u]; ~i; i = e[i].next) {         int v = e[i].v, w = dis[u] + e[i].c + Dis[u] - Dis[v];         if (e[i].f && w < dis[v]) {           dis[v] = w;           que[Head++] = make_pair(-dis[v], v);           push_heap(que, que + Head);         }       }     }     for (register int i = 0; i < Cnt; ++i) Dis[i] += dis[i];   }   ll dinic(int u, ll flow) {     if (u == T) return flow;     ll ret = 0, f;     vis[u] = dfn;     for (register int i = cur[u]; ~i; cur[u] = i = e[i].next)       if (e[i].f && vis[e[i].v] != dfn && Dis[u] + e[i].c == Dis[e[i].v]) {         f = dinic(e[i].v, min(flow, e[i].f));         e[i].f -= f;         e[i ^ 1].f += f;         flow -= f;         ret += f;         if (!flow) break;       }     if (ret) vis[u] = -1;     return ret;   }   inline ll MCMF() {     for (register int i = 0; i < Cnt; ++i) Dis[i] = 0;     spfa();     ll res = 0, Flow = 0, f;     while (dfn++, Dijkstra(), f = dinic(S, INF)) {       Flow += f;       res += Dis[T] * f;     }     return res;   }   }  // namespace mincost   const int N = 210;   int n, a[N][N];   int main() {     n = read();     ll Res = 0, ans = 0;     for (register int i = 0; i < n; ++i)       for (register int j = 0; j < n; ++j) a[i][j] = read();     int S = (n - 2) * (n - 2), T = (n - 2) * (n - 2) + 1;     mincost::init(S, T, T + 1);     for (register int i = 1; i < n - 1; ++i)       for (register int j = 0; j < n - 1; ++j)         if (a[i][j] == 0 && a[i][j + 1] == 0)           mincost::add((i - 1) * (n - 2) + j - 1, (i - 1) * (n - 2) + j, 0, 1, 0,                        1);         else if (a[i][j] == 0 && a[i][j + 1] > 0) {           mincost::add((i - 1) * (n - 2) + j - 1, T, 0, 2, a[i][j + 1]);           Res += a[i][j + 1];           mincost::add(S, (i - 1) * (n - 2) + j - 1, 0, 1, 0);         } else if (a[i][j] > 0 && a[i][j + 1] == 0) {           mincost::add((i - 1) * (n - 2) + j, T, 0, 2, a[i][j]);           Res += a[i][j];           mincost::add(S, (i - 1) * (n - 2) + j, 0, 1, 0);         }     for (register int i = 0; i < n - 1; ++i)       for (register int j = 1; j < n - 1; ++j)         if (a[i][j] == 0 && a[i + 1][j] == 0)           mincost::add((i - 1) * (n - 2) + j - 1, i * (n - 2) + j - 1, 0, 1, 0,                        1);         else if (a[i][j] == 0 && a[i + 1][j] > 0) {           mincost::add((i - 1) * (n - 2) + j - 1, T, 0, 2, a[i + 1][j]);           Res += a[i + 1][j];           mincost::add(S, (i - 1) * (n - 2) + j - 1, 0, 1, 0);         } else if (a[i][j] > 0 && a[i + 1][j] == 0) {           mincost::add(i * (n - 2) + j - 1, T, 0, 2, a[i][j]);           Res += a[i][j];           mincost::add(S, i * (n - 2) + j - 1, 0, 1, 0);         }     for (register int i = 0; i < n - 1; ++i) ans += abs(a[0][i] - a[0][i + 1]);     for (register int i = 0; i < n - 1; ++i)       ans += abs(a[n - 1][i] - a[n - 1][i + 1]);     for (register int i = 0; i < n - 1; ++i) ans += abs(a[i][0] - a[i + 1][0]);     for (register int i = 0; i < n - 1; ++i)       ans += abs(a[i][n - 1] - a[i + 1][n - 1]);     printf( %lld n , ans - mincost::MCMF() + Res);     return 0;   }