#include<bits/stdc++.h>

using namespace std;

#define PF           printf
#define SF(n)        scanf("%d", &n)
#define SFF(a,b)     scanf("%d %d", &a, &b)
#define SFFF(a,b,c)  scanf("%d %d %d", &a, &b, &c)
#define INF_I        2147483647    //max limit
#define INF          1999999999
#define PB           push_back
#define MEM(n, val)  memset((n), val, sizeof(n))
#define F            first
#define S            second
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ALL(c)       c.begin(), c.end()
#define BOOST        std::ios_base::sync_with_stdio(false);
#define INP          freopen("in.txt", "r", stdin);
#define OUT          freopen("out.txt", "w", stdout);
#define MP           make_pair
#define INIT_RAND    srand(time(NULL))
#define MOD          1000000007
#define MX           50010
#define PI           acos(-1.0)
#define eps          1e-9

#define cnd tree[node]
#define lnd tree[(node * 2)]
#define rnd tree[((node * 2) + 1)]

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef priority_queue<int> P_Q;
typedef queue<int> Q;
typedef stringstream SS;

#define infL 1000000000000000000LL

int n;
int ara[310][310];


vector<int> adj_list[310];
vector<int> cost[310];
long long dis[310];
int par[310];

class node  {
public:
    int u;
    long long w;
    node(int a, long long b):u(a), w(b) {}
    bool operator < (const node &a) const{
        return w > a.w;
    }
};

long long run_dijkstra(int src, int dest)    {   //dest = destination
    memset(dis, 63, sizeof(dis));
    priority_queue<node> pq;
    pq.push(node(src, 0));
    dis[src] = 0;
    while (!pq.empty()) {
        int u = pq.top().u;
        if (u == dest) {
            return dis[dest];
        }
        pq.pop();
        int u_len = adj_list[u].size();
        for (int i = 0; i < u_len; i++) {
            int v = adj_list[u][i];
            if (dis[v] > (dis[u] + cost[u][i])) {
                dis[v] = dis[u] + cost[u][i];
                pq.push(node(v, dis[v]));
                par[v] = u;
            }
        }
    }
    return -1;
}

class nn  {
public:
    int u, v;
    long long w;
    nn()    {}
    nn(int a, int b, long long c):u(a), v(b), w(c)  {}
    bool operator < (const nn &a) const{
        return w < a.w;
    }
};
vector<nn> edge;

int main()  {
    ///INP;
    BOOST;
    cin >> n;
    FOR(i, 1, n)   {
        FOR(j, 1, n)   {
            cin >> ara[i][j];
        }
    }
    FOR(i, 1, n)   {
        FOR(j, i + 1, n)    {
            edge.PB(nn(i, j, ara[i][j]));
        }
    }
    sort(edge.begin(), edge.end());
    LL res = 0;
    FOR(i, 0, (int)edge.size() - 1)   {
        int u = edge[i].u;
        int v = edge[i].v;
        LL w = edge[i].w;

        LL ans = run_dijkstra(u, v);
        //cout << u << " " << v << " : " << ans << "\n";
        if((ans == -1) || (ans > w))    {
            adj_list[u].PB(v);
            adj_list[v].PB(u);
            cost[u].PB(w);
            cost[v].PB(w);
            res += w;
        }
        else if(ans == w)   {

        }
        else    {
            cout << -1 << "\n";
            return 0;
        }
    }
    cout << res;
    return 0;
}
/**
*/


Measured memory_bytes: 1784