// Slower C++ solution:
#include <iostream>

#include <fstream>

#include <set>

#include <map>

#include <string>

#include <vector>

#include <queue>

#include <stack>

#include <functional>

#include <algorithm>

#include <climits>

#include <cmath>

using namespace std;

#define ll long long int

#define rep(i,n) for( int i = 0; i < n; i++ )

#define rrep(i,n) for( int i = n; i >= 0; i-- )

#define REP(i,s,t) for( int i = s; i <= t; i++ )

#define RREP(i,s,t) for( int i = s; i >= t; i-- )

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF 2000000000

#define mod 1000000007

#define INF2 10000000000000

#define int long long

int d[310][310];

int d2[310][310];

int d3[310][310];

bool need[310][310];

signed main(void)

{

    cin.tie(0);

    ios::sync_with_stdio(false);

    int n; cin >> n;

    rep(i, 310) rep(j, 310) d[i][j] = INF2;

    rep(i, n) {

        rep(j, n) {

            cin >> d[i][j];

        }

    }

    rep(i, n) {

        rep(j, n) {

            d2[i][j] = d[i][j];

        }

    }

    rep(k, n) {

        rep(i, n) {

            rep(j, n) {

                d2[i][j] = min(d2[i][j], d[i][k] + d[k][j]); 

            }

        }

    }

    rep(i, n) {

        rep(j, n) {

            if(d[i][j] != d2[i][j]) {

                cout << -1 << endl;

                return 0;

            }

        }

    }

    rep(i, n) rep(j, n) need[i][j] = true;

    int ans = 0;

    rep(k, n) {

        rep(i, n) {

            rep(j, n) {

                if(i == k || j == k) continue;

                if (d[i][j] == d[i][k] + d[k][j]) need[i][j] = false;

            }

        }

    }

    rep(i, n) {

        rep(j, n) {

            if(need[i][j]) ans += d[i][j];

        }

    }

    cout << ans/2 << endl;



    



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.7096096569030195x):
#include <iostream>

#include <fstream>

#include <set>

#include <map>

#include <string>

#include <vector>

#include <queue>

#include <deque>

#include <stack>

#include <functional>

#include <algorithm>

#include <climits>

#include <cmath>

#include <iomanip>

using namespace std;

#define ll long long int

#define rep(i,n) for( int i = 0; i < n; i++ )

#define rrep(i,n) for( int i = n; i >= 0; i-- )

#define REP(i,s,t) for( int i = s; i <= t; i++ )

#define RREP(i,s,t) for( int i = s; i >= t; i-- )

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF 2000000000

#define mod 1000000007

#define INF2 1000000000000000000

#define int long long



signed main(void)

{

    cin.tie(0);

    ios::sync_with_stdio(false);

    int N; cin >> N;

    int d[310][310], d2[310][310];

    rep(i, N) {

        rep(j, N) {

            d[i][j] = INF;

        }

        d[i][i] = 0;

    }

    rep(i, N) {

        rep(j, N) {

            cin >> d[i][j];

        }

    }

    rep(i, N) rep(j, N) d2[i][j] = d[i][j];

    rep(k, N) {

        rep(i, N) {

            rep(j, N) {

                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

            }

        }

    }

    rep(i, N) {

        rep(j, N) {

            if(d[i][j] != d2[i][j]) {

                cout << -1 << endl;

                return 0;

            }

        }

    }

    int sum = 0;

    rep(i, N) rep(j, N) d2[i][j] = d[i][j];

    rep(k, N) {

        rep(i, N) {

            rep(j, N) {

                if(i == k || j == k) continue;

                if(d[i][j] == d[i][k] + d[k][j]) d2[i][j] = 0;

            }

        }

    }

    rep(i, N) rep(j, N) sum += d2[i][j];

    cout << sum / 2 << endl;







    return 0;

}