import qualified Data.Array.IO as A
import Data.Array as U
import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as B

main = do
    n <- read <$> getLine :: IO Int
    as <- warshall_fluyd =<< A.newListArray ((1,1),(n,n)) . map Just . concat =<< 
            replicateM n (map (fst . fromJust . B.readInt) . B.words <$> B.getLine) 

    if isJust as 
        then print $ sum [fromMaybe 0 $ fromJust as!(i,j)|i<-[1..n], j<-[1..n], i<j, i/=j]
        else putStrLn "-1"

warshall_fluyd :: A.IOArray (Int,Int) (Maybe Int) -> IO (Maybe (U.Array (Int,Int) (Maybe Int)))
warshall_fluyd arr = do
    ((s,_),(l,_)) <- A.getBounds arr
    f <- and <$> (forM [(k,i,j)|k<-[s..l], i<-[s..l], j<-[s..l], i/=j, k/=j, i/=k] $ \(k,i,j) -> do
        ik <- A.readArray arr (i,k)
        kj <- A.readArray arr (k,j)
        ij <- A.readArray arr (i,j)
        if all isJust [ik,kj,ij]
            then if fromJust ik+ fromJust kj == fromJust ij
                then do A.writeArray arr (i,j) Nothing
                        return True
                else if fromJust ik+ fromJust kj < fromJust ij
                    then return False
                    else return True
            else return True)
    if f then Just <$> A.freeze arr else return Nothing
