#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

#define rep(i,n) for(i=1;i<=n;i++)
#define Rep(i,n) for(i=0;i<n;i++)
#define For(i,a,b) for(i=a;i<=b;i++)

#define pb(x) push_back(x)
#define sz(x) x.size()

#define mem(ara,val) memset(ara,val,sizeof(ara))
#define eps 1e-9

#define si(x) scanf("%d",&x)
#define sii(x,y) scanf("%d %d",&x,&y)
#define siii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sll(x,y) scanf("%lld %lld",&x,&y)
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define ss(ch) scanf("%s",ch)
#define pi(x) printf("%d",x)
#define pii(x,y) printf("%d %d",x,y)
#define piii(x,y,z) printf("%d %d %d",x,y,z)
#define pl(x) printf("%lld",x)
#define pll(x,y) printf("%lld %lld",x,y)
#define plll(x,y,z) printf("%lld %lld %lld",x,y,z)
#define ps(ch) printf("%s",ch)
#define Afridi 0
#define NL printf("\n")
#define debug(x) printf("wow  %d !!\n",x)
#define Max 1000005
#define INF (LL)1e15
//#define mod 1000000007
#define un(x) x.erase(unique( x.begin() , x.end() ), x.end())

#define FI freopen("in.txt","r",stdin)

#define D(x) cout << #x << " = " << x << endl
#define DD(x,y) cout << #x << " = " << x << "   " << #y << " = " << y << endl

typedef long long LL;
typedef unsigned long long ULL;

using namespace std;

LL bigmod(LL b,LL p,LL m)
{
    if(p == 0)return 1;
    LL my = bigmod(b,p/2,m);
    my*=my;my%=m;
    if(p & 1)my*=b,my%=m;
    return my;
}
LL setb(LL n,LL pos){return n=n | (1 << pos);}
LL resb(LL n,LL pos){return n=n & ~(1 << pos);}
bool checkb(LL n,LL pos){return (bool)(n & (1 << pos));}

LL n,ara[305][305],cost[305][305],g[305][305];

void update(LL u,LL v,LL x)
{
	cost[u][v] = cost[v][u] = x;
	for(int k = 1; k <= n; k++)
	{
		if( k == u || k == v )
		{
			for(int i = 1; i <= n; i++)
			{
				for(int j = 1; j <= n; j++)
				{
					cost[i][j] = min( cost[i][j] , cost[i][k] + cost[k][j] );
				}
			}
		}
	}
}

void Floyd()
{
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			cost[i][j] = ara[i][j];
		}
	}
	for(int k = 1; k <= n; k++)
	{
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				cost[i][j] = min( cost[i][j] , cost[i][k] + cost[k][j] );
			}
		}
	}
	/*for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
			DD( ara[i][j] , cost[i][j] );
		}
	}*/
}

struct info
{
	LL u,v,w;
	info() {}
	info(LL uu,LL vv,LL ww) { u = uu; v = vv; w = ww; }
	bool operator < (const info &p) const
	{
		return w < p.w;
	}
};
vector <info> lol;

int main()
{
	//FI;
	LL i,j;
	sl(n);
	rep(i,n) rep(j,n) sl(ara[i][j]);
	Floyd();
	LL flag = 1;
	rep(i,n)
	{
		rep(j,n)
		{
			if( ara[i][j] != cost[i][j] ) flag = -1;
		}
	}
	if( flag == -1 )
	{
		puts("-1");
		return 0;
	}

	rep(i,n)
	{
		rep(j,n) cost[i][j] = INF;
		cost[i][i] = 0;
	}
	LL ret = 0;
	rep(i,n)
	{
		rep(j,n)
		{
			lol.pb( info(i,j,ara[i][j]) );
		}
	}
	sort(lol.begin(),lol.end());
	Rep(i,sz(lol))
	{
		LL u = lol[i].u;
		LL v = lol[i].v;
		LL w = lol[i].w;
		if( cost[u][v] != w )
		{
			update(u,v,w);
			ret += w;
			g[u][v] = g[v][u] = w;
		}
	}
	pl(ret); NL;
	/*rep(i,n)
	{
		rep(j,n)
		{
			printf("%lld ",g[i][j]);
		}
		NL;
	}*/
	return 0;
}


Measured memory_bytes: 6260