using System;
using System.Collections.Generic;
using System.Linq;

namespace ABC074D
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());

            long[,] A = new long[N, N];
            for (int i = 0; i < N; i++)
            {
                var input = ReadLong();
                for (int j = 0; j < N; j++)
                {
                    A[i, j] = input[j];
                }
            }

            bool negativeCycle = false;
            WarshallFloyd.Init(N, 0, A);
            long[,] ret = WarshallFloyd.Start(out negativeCycle);

            bool exist = true;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (A[i, j] != ret[i, j]) exist = false;
                }
            }

            long[,] anss = new long[N, N];
            for (int u = 0; u < N; u++)
            {
                for (int v = 0; v < N; v++)
                {
                    if (u == v) continue;
                    for (int w = 0; w < N; w++)
                    {
                        if (u == w || v == w) continue;

                        if (ret[u, v] == ret[u, w] + ret[w, v]) anss[u, v] = 1;
                    }
                }
            }

            long ans = 0;
            for (int u = 0; u < N; u++)
            {
                for (int v = u + 1; v < N; v++)
                {
                    if (anss[u, v] == 0) ans += ret[u, v];
                }
            }

            if (!exist) Console.WriteLine("-1");
            else Console.WriteLine(ans);
            Console.ReadKey();
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }

    /// <summary>
    /// ワーシャルフロイド、負の辺に対応、O(V^3)、全頂点間距離
    /// </summary>
    static class WarshallFloyd
    {
        static long INF = long.MaxValue;

        static int _v;
        static int _e;
        static long[,] dp;

        static public void Init(int V, int E, long[,] Edge)
        {
            _v = V; _e = E;

            dp = new long[V, V];
            for (int i = 0; i < V; i++)
            {
                for (int j = 0; j < V; j++)
                {
                    if (i != j) dp[i, j] = INF;
                    if (Edge[i, j] != 0) dp[i, j] = Edge[i, j];
                }
            }
        }

        static public long[,] Start(out bool negativeCycle)
        {
            negativeCycle = false;
            for (int k = 0; k < _v; k++)
            {
                for (int i = 0; i < _v; i++)
                {
                    for (int j = 0; j < _v; j++)
                    {
                        if (dp[i, k] == INF || dp[k, j] == INF) continue;
                        dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k, j]);
                    }

                    if (dp[i, i] < 0) negativeCycle = true;
                }
            }

            return dp;
        }
    }
}


Measured memory_bytes: 23000