void main()
{
    long n = rdElem;
    long[][] a = rdMat(n);

    bool[][] need = new bool[][](n, n);
    foreach (i; 0 .. n)
    {
        need[i][] = true;
    }
    bool ok = true;
    foreach (i; 0 .. n)
    {
        foreach (j; 0 .. n)
        {
            foreach (k; 0 .. n)
            {
                if (j == k || j == i || i == k) continue;
                if (a[j][k] > a[j][i] + a[i][k])
                {
                    ok = false;
                }
                if (a[j][k] == a[j][i] + a[i][k] && need[j][i] && need[i][k])
                {
                    need[j][k] = false;
                }
            }
        }
    }

    if (ok)
    {
        long len;
        foreach (i; 0 .. n-1)
        {
            foreach (j; i+1 .. n)
            {
                if (need[i][j]) len += a[i][j];
            }
        }
        len.writeln;
    }
    else
    {
        writeln(-1);
    }
}

T rdElem(T = long)()
{
    //import std.stdio : readln;
    //import std.string : chomp;
    //import std.conv : to;

    return readln.chomp.to!T;
}

alias rdStr = rdElem!string;

dchar[] rdDchar()
{
    //import std.conv : to;

    return rdStr.to!(dchar[]);
}

T[] rdRow(T = long)()
{
    //import std.stdio : readln;
    //import std.array : split;
    //import std.conv : to;

    return readln.split.to!(T[]);
}

T[] rdCol(T = long)(long col)
{
    //import std.range : iota;
    //import std.algorithm : map;
    //import std.array : array;

    return iota(col).map!(x => rdElem).array;
}

T[][] rdMat(T = long)(long col)
{
    //import std.range : iota;
    //import std.algorithm : map;
    //import std.array : array;

    return iota(col).map!(x => rdRow).array;
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;