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

#include<queue>

#include<string>

#include<cmath>

#include<cstdio>

#include<cctype>

#include<cstring>

#include<iomanip>

#include<cstdlib>

#include<ctime>

#include<set>

#include<map>

#include<utility>

#include<vector>

#include<stack>

#include<sstream>

#include<algorithm>

using namespace std;

/****************************/

#define ll long long

#define vi vector<int>

#define pii pair<int,int>

#define vpii vector<pair<int,int> >

#define qi queue<int>

#define mii map<int,int>

#define msi map<string,int>

/****************************/

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

#define rep2(i,n) for(ll i=(n)-1;i>=0;i--)

#define rep3(i,a,n) for(ll i=(a);i<(n);i++)

#define mp make_pair

#define pb push_back

#define st first

#define nd second

#define pi 3.1415926535897932384626433832795

#define zero(n) memset(n,0,sizeof(n));

#define SZ(n) n.size()

#define ss st.st

#define sn st.nd

#define mod 100000007

#define INF 1e9

/****************************/

using namespace std;

int a[310][310];

bool can[310][310];

int main()

{

	int n;

	cin>>n;

	rep(i,n)

	{

		rep(j,n)

		{

			cin>>a[i][j];

		}

	}

	memset(can,true,sizeof(can)); 

	rep(k,n)

	{

		rep(i,n)

		{

			rep(j,n)

			{

				if(k!=i&&k!=j&&i!=j)

				{

					if(a[i][j]==a[k][i]+a[k][j])

					{

						can[i][j]=false;

					}

					if(a[i][j]>a[k][i]+a[k][j])

					{

						cout<<"-1";

						return 0;

					} 

				}

			} 

		}

	}

	long long ans=0;

	rep(i,n)

	{

		rep(j,n)

		{

			if(can[i][j])

			{

				ans+=a[i][j];

			}

		}

	}

	cout<<ans/2;

	return 0;

}

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

#include<queue>

#include<string>

#include<cmath>

#include<cstdio>

#include<cctype>

#include<cstring>

#include<iomanip>

#include<cstdlib>

#include<ctime>

#include<set>

#include<map>

#include<utility>

#include<vector>

#include<stack>

#include<sstream>

#include<algorithm>

using namespace std;

/****************************/

#define ll long long

#define vi vector<int>

#define pii pair<int,int>

#define vpii vector<pair<int,int> >

#define qi queue<int>

#define mii map<int,int>

#define msi map<string,int>

/****************************/

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

#define rep2(i,n) for(ll i=(n)-1;i>=0;i--)

#define rep3(i,a,n) for(ll i=(a);i<(n);i++)

#define mp make_pair

#define pb push_back

#define st first

#define nd second

#define pi 3.1415926535897932384626433832795

#define zero(n) memset(n,0,sizeof(n));

#define SZ(n) n.size()

#define ss st.st

#define sn st.nd

#define mod 100000007

#define INF 1e9

/****************************/

 

const int N = 303;

int n;

ll a[N][N];

ll ans = 0;

 

int main()

{

//	freopen("input.txt", "r", stdin);

//	freopen("output.txt", "w", stdout);

 

	scanf("%d", &n);

	for (int i = 0; i < n; i++)

		for (int j = 0; j < n; j++) {

			scanf("%lld", &a[i][j]);

		}

	for (int i = 0; i < n; i++)

		for (int j = 0; j < i; j++) {

			ll w = a[i][j] + 1;

			for (int k = 0; k < n; k++) {

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

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

			}

			if (w < a[i][j]) {

				printf("-1\n");

				return 0;

			}

			if (w > a[i][j]) ans += a[i][j];

		}

	printf("%lld\n", ans);

 

	return 0;

}