local n=io.read("n")
local a={}
for i=1,n do
    a[i]={}
    for j=1,n do
        a[i][j]=io.read("n")
    end
end

local d={}
for i=1,n do
    d[i]={}
end

for k=1,n do
    for i=1,n do
        for j=1,n do
            if a[i][j]>a[i][k]+a[k][j] then
                print(-1)
                return
            elseif a[i][j]==a[i][k]+a[k][j] and a[i][k]>0 and a[k][j]>0 then
                d[i][j]=true
            end
        end
    end
end

local total=0
for i=1,n do
    for j=1,i do
        total=total+(not d[i][j] and a[i][j] or 0)
    end
end
print(total)