using System;

namespace _711B_ChrisAndMagicSquare
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Int32.Parse( Console.ReadLine() );
            int total1 = 0;
            int total2 = 0;
            int result = 0;

            for (int x = 0; x < n; ++x)
            {
                string[] input = Console.ReadLine().Split();
                //int[] rowArray = Int32.Parse( Console.ReadLine().Split());
                int rowTotal = 0;
                for( int y = 0; y < n; ++y )
                {
                    rowTotal += Int32.Parse( input[y]);
                }

                total1 = total1 == 0 ? rowTotal : total1;
                total2 = total2 == 0 && rowTotal != total1 ? rowTotal : total2;

                if( rowTotal != total1 && rowTotal != total2)
                {
                    result = -1;
                    break;
                }
            }

            if( result == 0 )
            {
                result = 1;
            }
            else if( result != -1 )
            {
                result = Math.Abs( total1 - total2 );
            }

            Console.WriteLine( result );
        }
    }
}


using System;

namespace _711B_ChrisAndMagicSquare
{
    class Program
    {
        static long[,] matrix;
        static int size;

        static void Main(string[] args)
        {
            size = Int32.Parse(Console.ReadLine());
            CreateMatrix();
            long total1 = 0;
            long total2 = 0;
            long result = 0;

            for (int x = 0; x < size; ++x)
            {
                long rowTotal = GetRowTotal(x);
                total1 = total1 == 0 ? rowTotal : total1;
                total2 = total2 == 0 && rowTotal != total1 ? rowTotal : total2;

                long colTotal = GetColTotal(x);
                total1 = total1 == 0 ? colTotal : total1;
                total2 = total2 == 0 && colTotal != total1 ? colTotal : total2;

                long diaTotal = 0;
                if( x < 2 )
                {
                    diaTotal = GetDiaTotal(x);
                    total1 = total1 == 0 ? diaTotal : total1;
                    total2 = total2 == 0 && diaTotal != total1 ? diaTotal : total2;
                }

                if ((rowTotal != total1 && rowTotal != total2)
                || (colTotal != total1 && colTotal != total2)
                || x < 2 && (diaTotal != total1 && diaTotal != total2))
                {
                    result = -1;
                    break;
                }
            }

            if (total1 > 0 && total2 == 0)
            {
                result = -1;
            }

            if (result != -1)
            {
                result = Math.Abs(total1 - total2);
                if (result == 0)
                {
                    result = 1;
                }

            }

            Console.WriteLine(result);
        }

        static void CreateMatrix()
        {
            matrix = new long[size, size];

            for (int x = 0; x < size; ++x)
            {
                string[] input = Console.ReadLine().Split();
                for (int y = 0; y < size; ++y)
                {
                    matrix[x, y] = Int64.Parse(input[y]);
                }
            }
        }

        static long GetRowTotal(int row)
        {
            long rowTotal = 0;

            for (int x = 0; x < size; ++x)
            {
                rowTotal += matrix[row, x];
            }

            return rowTotal;
        }

        static long GetColTotal(int col)
        {
            long colTotal = 0;

            for (int x = 0; x < size; ++x)
            {
                colTotal += matrix[x, col];
            }

            return colTotal;
        }

        static long GetDiaTotal(int dia)
        {
            long total = 0;

            for (int x = 0; x < size; ++x)
            {
                int y = dia == 0 ? x : size - x - 1;
                total += matrix[x, y];
            }

            return total;
        }
    }
}


using System;

namespace _711B_ChrisAndMagicSquare
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Int32.Parse( Console.ReadLine() );
            int total1 = 0;
            int total2 = 0;
            int result = 0;

            for (int x = 0; x < n; ++x)
            {
                string[] input = Console.ReadLine().Split();
                //int[] rowArray = Int32.Parse( Console.ReadLine().Split());
                int rowTotal = 0;
                for( int y = 0; y < n; ++y )
                {
                    rowTotal += Int32.Parse( input[y]);
                }

                total1 = total1 == 0 ? rowTotal : total1;
                total2 = total2 == 0 && rowTotal != total1 ? rowTotal : total2;

                if( rowTotal != total1 && rowTotal != total2)
                {
                    result = -1;
                    break;
                }
            }

            if( result != -1 )
            {
                if( result == 0 )
                {
                    result = 1;
                }
                else
                {
                    result = Math.Abs( total1 - total2 );
                }
            }

            Console.WriteLine( result );
        }
    }
}


using System;

namespace _711B_ChrisAndMagicSquare
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Int32.Parse( Console.ReadLine() );
            int total1 = 0;
            int total2 = 0;
            int result = 0;

            for (int x = 0; x < n; ++x)
            {
                string[] input = Console.ReadLine().Split();
                //int[] rowArray = Int32.Parse( Console.ReadLine().Split());
                int rowTotal = 0;
                for( int y = 0; y < n; ++y )
                {
                    rowTotal += Int32.Parse( input[y]);
                }

                total1 = total1 == 0 ? rowTotal : total1;
                total2 = total2 == 0 && rowTotal != total1 ? rowTotal : total2;

                if( rowTotal != total1 && rowTotal != total2)
                {
                    result = -1;
                    break;
                }
            }

            if( result != -1 )
            {
                result = Math.Abs( total1 - total2 );
            }

            Console.WriteLine( result );
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        var a = new long[n][];
        for (int i = 0; i < n; i++)
            a[i] = ReadLongArray();

        if (n == 1)
        {
            Write(1);
            return;
        }

        int xr = 0, xc = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (a[i][j] == 0)
                {
                    xr = i;
                    xc = j;
                }

        long sum = -1;
        for (int i = 0; i < n; i++)
            if (i != xr)
            {
                sum = a[i].Sum();
                break;
            }

        long ans = sum - a[xr].Sum();
        a[xr][xc] = ans;
        long s;
        for (int i = 0; i < n; i++)
        {
            s = 0;
            for (int j = 0; j < n; j++)
                s += a[i][j];
            if (s != sum)
                ans = -1;
            s = 0;
            for (int j = 0; j < n; j++)
                s += a[j][i];
            if (s != sum)
                ans = -1;
        }

        s = 0;
        for (int i = 0; i < n; i++)
            s += a[i][i];
        if (s != sum)
            ans = -1;
        s = 0;
        for (int i = 0; i < n; i++)
            s += a[i][n - i - 1];
        if (s != sum)
            ans = -1;

        if (ans <= 0)
            Write(-1);
        else
            Write(ans);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class PairVariable : IComparable<PairVariable>
{
    public decimal a, b;
    public PairVariable()
    {
        a = b = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = decimal.Parse(q[0]);
        b = decimal.Parse(q[1]);
    }
    public PairVariable(decimal a, decimal b)
    {
        this.a = a;
        this.b = b;
    }

    public int CompareTo(PairVariable other)
    {
        return this.a.CompareTo(other.a);
    }
}

namespace ConsoleApplication254
{

    class Program
    {

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]);

            long[,] a = new long[n, n];
            int t = 0;
            for (int i = 0; i < n; i++)
            {
                string[] q = Console.ReadLine().Split();
                for (int e = 0; e < n; e++)
                {
                    a[i, e] = long.Parse(q[e]);
                    if (a[i, e] == 0)
                    {
                        t = i;
                    }
                }
            }

            int p = t + 1;
            if (t == n-1)
            {
                p = t - 1;
            }
            long sum1 = 0;
            long sum2 = 0;
            for (int i = 0; i < n; i++)
            {
                sum1 += a[t, i];
            }
            for (int i = 0; i < n; i++)
            {
                sum2 += a[p, i];
            }
            long x = Math.Abs(sum1 - sum2);
            for (int i = 0; i < n; i++)
            {
                if (a[t, i] == 0)
                {
                    a[t, i] = x;
                }
            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[i, e];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[e, i];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            long tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, i];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, n-i-1];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            Console.WriteLine(x);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class PairVariable : IComparable<PairVariable>
{
    public decimal a, b;
    public PairVariable()
    {
        a = b = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = decimal.Parse(q[0]);
        b = decimal.Parse(q[1]);
    }
    public PairVariable(decimal a, decimal b)
    {
        this.a = a;
        this.b = b;
    }

    public int CompareTo(PairVariable other)
    {
        return this.a.CompareTo(other.a);
    }
}

namespace ConsoleApplication254
{

    class Program
    {

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]);

            long[,] a = new long[n, n];
            long[] sum = new long[n];
            for (int i = 0; i < n; i++)
            {
                long t = 0;
                string[] q = Console.ReadLine().Split();
                for (int e = 0; e < n; e++)
                {
                    t += long.Parse(q[e]);
                }
                sum[i] = t;
            }
            Array.Sort(sum);
            Dictionary<long, int> cnt = new Dictionary<long, int>();
            for (int i = 0; i < n; i++)
            {
                if (!cnt.ContainsKey(sum[i]))
                {
                    cnt.Add(sum[i], 1);
                }
            }
            if (cnt.Count >= 3)
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine(Math.Abs(sum[0] - sum[n - 1]));
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class PairVariable : IComparable<PairVariable>
{
    public decimal a, b;
    public PairVariable()
    {
        a = b = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = decimal.Parse(q[0]);
        b = decimal.Parse(q[1]);
    }
    public PairVariable(decimal a, decimal b)
    {
        this.a = a;
        this.b = b;
    }

    public int CompareTo(PairVariable other)
    {
        return this.a.CompareTo(other.a);
    }
}

namespace ConsoleApplication254
{

    class Program
    {

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]);

            long[,] a = new long[n, n];
            int t = 0;
            for (int i = 0; i < n; i++)
            {
                string[] q = Console.ReadLine().Split();
                for (int e = 0; e < n; e++)
                {
                    a[i, e] = long.Parse(q[e]);
                    if (a[i, e] == 0)
                    {
                        t = i;
                    }
                }
            }
            int p = t + 1;
            if (t == n)
            {
                p = t - 1;
            }
            long sum1 = 0;
            long sum2 = 0;
            for (int i = 0; i < n; i++)
            {
                sum1 += a[t, i];
            }
            for (int i = 0; i < n; i++)
            {
                sum2 += a[p, i];
            }
            long x = Math.Abs(sum1 - sum2);
            for (int i = 0; i < n; i++)
            {
                if (a[t, i] == 0)
                {
                    a[t, i] = x;
                }
            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[i, e];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[e, i];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            long tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, i];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, n-i-1];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            Console.WriteLine(x);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class PairVariable : IComparable<PairVariable>
{
    public decimal a, b;
    public PairVariable()
    {
        a = b = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = decimal.Parse(q[0]);
        b = decimal.Parse(q[1]);
    }
    public PairVariable(decimal a, decimal b)
    {
        this.a = a;
        this.b = b;
    }

    public int CompareTo(PairVariable other)
    {
        return this.a.CompareTo(other.a);
    }
}

namespace ConsoleApplication254
{

    class Program
    {

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]);

            long[,] a = new long[n, n];
            int t = 0;
            for (int i = 0; i < n; i++)
            {
                string[] q = Console.ReadLine().Split();
                for (int e = 0; e < n; e++)
                {
                    a[i, e] = long.Parse(q[e]);
                    if (a[i, e] == 0)
                    {
                        t = i;
                    }
                }
            }
            if (n == 1)
            {
                Console.WriteLine(1);
                return;
            }
            int p = t + 1;
            if (t == n-1)
            {
                p = t - 1;
            }
            long sum1 = 0;
            long sum2 = 0;
            for (int i = 0; i < n; i++)
            {
                sum1 += a[t, i];
            }
            for (int i = 0; i < n; i++)
            {
                sum2 += a[p, i];
            }
            long x = (sum2 - sum1);
            for (int i = 0; i < n; i++)
            {
                if (a[t, i] == 0)
                {
                    a[t, i] = x;
                }
            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[i, e];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[e, i];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            long tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, i];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, n-i-1];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            Console.WriteLine(x);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class PairVariable : IComparable<PairVariable>
{
    public decimal a, b;
    public PairVariable()
    {
        a = b = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = decimal.Parse(q[0]);
        b = decimal.Parse(q[1]);
    }
    public PairVariable(decimal a, decimal b)
    {
        this.a = a;
        this.b = b;
    }

    public int CompareTo(PairVariable other)
    {
        return this.a.CompareTo(other.a);
    }
}

namespace ConsoleApplication254
{

    class Program
    {

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]);

            long[,] a = new long[n, n];
            int t = 0;
            for (int i = 0; i < n; i++)
            {
                string[] q = Console.ReadLine().Split();
                for (int e = 0; e < n; e++)
                {
                    a[i, e] = long.Parse(q[e]);
                    if (a[i, e] == 0)
                    {
                        t = i;
                    }
                }
            }
            if (n == 1)
            {
                Console.WriteLine(1);
                return;
            }
            int p = t + 1;
            if (t == n-1)
            {
                p = t - 1;
            }
            long sum1 = 0;
            long sum2 = 0;
            for (int i = 0; i < n; i++)
            {
                sum1 += a[t, i];
            }
            for (int i = 0; i < n; i++)
            {
                sum2 += a[p, i];
            }
            long x = (sum2 - sum1);
            if (x <= 0)
            {
                Console.WriteLine(-1);
                return;
            }
            for (int i = 0; i < n; i++)
            {
                if (a[t, i] == 0)
                {
                    a[t, i] = x;
                }
            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[i, e];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[e, i];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            long tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, i];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, n-i-1];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            Console.WriteLine(x);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class PairVariable : IComparable<PairVariable>
{
    public decimal a, b;
    public PairVariable()
    {
        a = b = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = decimal.Parse(q[0]);
        b = decimal.Parse(q[1]);
    }
    public PairVariable(decimal a, decimal b)
    {
        this.a = a;
        this.b = b;
    }

    public int CompareTo(PairVariable other)
    {
        return this.a.CompareTo(other.a);
    }
}

namespace ConsoleApplication254
{

    class Program
    {

        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]);

            long[,] a = new long[n, n];
            int t = 0;
            for (int i = 0; i < n; i++)
            {
                string[] q = Console.ReadLine().Split();
                for (int e = 0; e < n; e++)
                {
                    a[i, e] = long.Parse(q[e]);
                    if (a[i, e] == 0)
                    {
                        t = i;
                    }
                }
            }
            if (n == 1)
            {
                Console.WriteLine(1);
                return;
            }
            int p = t + 1;
            if (t == n-1)
            {
                p = t - 1;
            }
            long sum1 = 0;
            long sum2 = 0;
            for (int i = 0; i < n; i++)
            {
                sum1 += a[t, i];
            }
            for (int i = 0; i < n; i++)
            {
                sum2 += a[p, i];
            }
            long x = Math.Abs(sum1 - sum2);
            for (int i = 0; i < n; i++)
            {
                if (a[t, i] == 0)
                {
                    a[t, i] = x;
                }
            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[i, e];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            for (int i = 0; i < n; i++)
            {
                long temp = 0;
                for (int e = 0; e < n; e++)
                {
                    temp += a[e, i];
                }
                if (temp != sum2)
                {
                    Console.WriteLine(-1);
                    return;
                }

            }
            long tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, i];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            tempp = 0;
            for (int i = 0; i < n; i++)
            {
                tempp += a[i, n-i-1];
            }
            if (tempp != sum2)
            {
                Console.WriteLine(-1);
                return;
            }
            Console.WriteLine(x);

        }
    }
}


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

namespace Preparation
{
    public class CodeForces711B
    {
        public static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            if (n == 1)
            {
                Console.WriteLine(1);
                return;
            }
            var arr = new int[n][];
            int xx = 0, yy = 0;
            for (int i = 0; i < n; ++i)
            {
                arr[i] = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

                for(int j = 0; j < n; ++j)
                    if (arr[i][j] == 0)
                    {
                        xx = i;
                        yy = j;
                    }
            }

            int sum = 0;
            for (int i = 0; sum == 0; ++i)
            {
                if (i == xx) continue;
                sum = arr[i].Sum();
            }

            arr[xx][yy] = sum - arr[xx].Sum();

            if (arr[xx][yy] <= 0)
            {
                Console.WriteLine(-1);
                return;
            }

            var list1 = new List<int>();
            var list2 = new List<int>();
            int s1 = 0, s2 = 0;
            for (int i = 0; i < n; ++i)
            {
                var sum1 = 0;
                var sum2 = 0;
                for (int j = 0; j < n; ++j)
                {
                    sum1 += arr[i][j];
                    sum2 += arr[j][i];
                    if (i == j)
                    {
                        s1 += arr[i][j];
                        s2 += arr[i][n - j - 1];
                    }
                }
                list1.Add(sum1);
                list2.Add(sum2);
            }

            if(s1 == s2 && list1.Distinct().Count() == 1 &&  list2.Distinct().Count() == 1)
            {
                Console.WriteLine(arr[xx][yy]);
                return;
            }
            else
            {
                Console.WriteLine(-1);
                return;
            }
        }
    }
}


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

namespace Preparation
{
    public class CodeForces711B
    {
        public static void Main(string[] args)
        {
            var n = long.Parse(Console.ReadLine());
            if (n == 1)
            {
                Console.WriteLine(1);
                return;
            }
            var arr = new long[n][];
            long xx = 0, yy = 0;
            for (long i = 0; i < n; ++i)
            {
                arr[i] = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();

                for(long j = 0; j < n; ++j)
                    if (arr[i][j] == 0)
                    {
                        xx = i;
                        yy = j;
                    }
            }

            long sum = 0;
            for (long i = 0; sum == 0; ++i)
            {
                if (i == xx) continue;
                sum = arr[i].Sum();
            }

            arr[xx][yy] = sum - arr[xx].Sum();

            if (arr[xx][yy] <= 0)
            {
                Console.WriteLine(-1);
                return;
            }

            var list1 = new List<long>();
            var list2 = new List<long>();
            long s1 = 0, s2 = 0;
            for (long i = 0; i < n; ++i)
            {
                long sum1 = 0;
                long sum2 = 0;
                for (long j = 0; j < n; ++j)
                {
                    sum1 += arr[i][j];
                    sum2 += arr[j][i];
                    if (i == j)
                    {
                        s1 += arr[i][j];
                        s2 += arr[i][n - j - 1];
                    }
                }
                list1.Add(sum1);
                list2.Add(sum2);
            }

            if(s1 == s2 && list1.Distinct().Count() == 1 &&  list2.Distinct().Count() == 1)
            {
                Console.WriteLine(arr[xx][yy]);
                return;
            }
            else
            {
                Console.WriteLine(-1);
                return;
            }
        }
    }
}


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

namespace Preparation
{
    public class CodeForces711B
    {
        public static void Main(string[] args)
        {
            var n = long.Parse(Console.ReadLine());
            if (n == 1)
            {
                Console.WriteLine(1);
                return;
            }
            var arr = new long[n][];
            long xx = 0, yy = 0;
            for (long i = 0; i < n; ++i)
            {
                arr[i] = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();

                for(long j = 0; j < n; ++j)
                    if (arr[i][j] == 0)
                    {
                        xx = i;
                        yy = j;
                    }
            }

            long sum = 0;
            for (long i = 0; sum == 0; ++i)
            {
                if (i == xx) continue;
                sum = arr[i].Sum();
            }
            var sum11 = arr[xx].Sum();
            arr[xx][yy] = sum - sum11;

            if (arr[xx][yy] <= 0)
            {
                Console.WriteLine(-1);
                return;
            }

            var list1 = new List<long>();
            var list2 = new List<long>();
            long s1 = 0, s2 = 0;
            for (long i = 0; i < n; ++i)
            {
                long sum1 = 0;
                long sum2 = 0;
                s1 += arr[i][i];
                s2 += arr[i][n - i - 1];
                for (long j = 0; j < n; ++j)
                {
                    sum1 += arr[i][j];
                    sum2 += arr[j][i];
                }
                list1.Add(sum1);
                list2.Add(sum2);
            }

            if(s1 == s2 &&
               list1.Distinct().Count() == 1 &&
               list2.Distinct().Count() == 1 && list1[0] == s1 && s1 == list2[0])
            {
                Console.WriteLine(arr[xx][yy]);
                return;
            }
            else
            {
                Console.WriteLine(-1);
                return;
            }
        }
    }
}


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

namespace Preparation
{
    public class CodeForces711B
    {
        public static void Main(string[] args)
        {
            var n = long.Parse(Console.ReadLine());
            if (n == 1)
            {
                Console.WriteLine(1);
                return;
            }
            var arr = new long[n][];
            long xx = 0, yy = 0;
            for (long i = 0; i < n; ++i)
            {
                arr[i] = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();

                for(long j = 0; j < n; ++j)
                    if (arr[i][j] == 0)
                    {
                        xx = i;
                        yy = j;
                    }
            }

            long sum = 0;
            for (long i = 0; sum == 0; ++i)
            {
                if (i == xx) continue;
                sum = arr[i].Sum();
            }
            var sum11 = arr[xx].Sum();
            arr[xx][yy] = sum - sum11;

            if (arr[xx][yy] <= 0)
            {
                Console.WriteLine(-1);
                return;
            }

            var list1 = new List<long>();
            var list2 = new List<long>();
            long s1 = 0, s2 = 0;
            for (long i = 0; i < n; ++i)
            {
                long sum1 = 0;
                long sum2 = 0;
                s1 += arr[i][i];
                s2 += arr[i][n - i - 1];
                for (long j = 0; j < n; ++j)
                {
                    sum1 += arr[i][j];
                    sum2 += arr[j][i];
                }
                list1.Add(sum1);
                list2.Add(sum2);
            }

            if(s1 == s2 && list1.Distinct().Count() == 1 && list2.Distinct().Count() == 1)
            {
                Console.WriteLine(arr[xx][yy]);
                return;
            }
            else
            {
                Console.WriteLine(-1);
                return;
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeforcesTasks
{
    class Program_369_2
    {
        static void Main(string[] args)
        {
            string nStr = Console.ReadLine();
            int nn = int.Parse(nStr);

            string[] tokens = new string[nn];
            ulong[,] item = new ulong[nn, nn];
            int nullRow = -1;
            int nullCol = -1;
            ulong sum, sum1, guess;
            int failGuess = 0;

            for (int i = 0; i < nn; i++)
            {
                tokens = Console.ReadLine().Split(' ');
                for (int j = 0; j < nn; j++)
                {
                    item[i, j] = ulong.Parse(tokens[j]);
                    if (item[i, j] == 0)
                    {
                        nullRow = i;
                        nullCol = j;
                    }
                }
            }

            if (nn == 1)
            {
                Console.WriteLine(1);
                return;
            }
            // вычисляем сумму ряда
            int fullRow = 0;
            if (nullRow == 0)
            {
                fullRow = 1;
            }
            sum = 0;
            for (int j = 0; j < nn; j++)
            {
                sum += item[fullRow, j];
            }
            sum1 = 0;
            for (int j = 0; j < nn; j++)
            {
                sum1 += item[nullRow, j];
            }

            // вычисляем искомый элемент
            guess = sum - sum1;
            item[nullRow, nullCol] = guess;

            // проверяем на магичность
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[i, j];
                }
                if (sum1 != sum)
                {
                    failGuess = -1;
                    break;
                }
            }
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[j, i];
                }
                if (sum1 != sum)
                {
                    failGuess = -1;
                    break;
                }
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                    sum1 += item[i, i];
            }
            if (sum1 != sum)
            {
                failGuess = -1;
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                sum1 += item[i, nn - i - 1];
            }
            if (sum1 != sum)
            {
                failGuess = -1;
            }

            if (failGuess == 0)
            {
                Console.WriteLine(guess);
            }
            else
            {
                Console.WriteLine(failGuess);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeforcesTasks
{
    class Program_369_2
    {
        static void Main(string[] args)
        {
            string nStr = Console.ReadLine();
            int nn = int.Parse(nStr);

            string[] tokens = new string[nn];
            long[,] item = new long[nn, nn];
            int nullRow = -1;
            int nullCol = -1;
            long sum, sum1, guess;

            for (int i = 0; i < nn; i++)
            {
                tokens = Console.ReadLine().Split(' ');
                for (int j = 0; j < nn; j++)
                {
                    item[i, j] = int.Parse(tokens[j]);
                    if (item[i, j] == 0)
                    {
                        nullRow = i;
                        nullCol = j;
                    }
                }
            }

            // вычисляем сумму ряда
            int fullRow = 0;
            if (nullRow == 0)
            {
                fullRow = 1;
            }
            sum = 0;
            for (int j = 0; j < nn; j++)
            {
                sum += item[fullRow, j];
            }
            sum1 = 0;
            for (int j = 0; j < nn; j++)
            {
                sum1 += item[nullRow, j];
            }

            // вычисляем искомый элемент
            guess = sum - sum1;
            item[nullRow, nullCol] = guess;

            // проверяем на магичность
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[i, j];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[j, i];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }

            Console.WriteLine(guess);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeforcesTasks
{
    class Program_369_2
    {
        static void Main(string[] args)
        {
            string nStr = Console.ReadLine();
            int nn = int.Parse(nStr);

            string[] tokens = new string[nn];
            long[,] item = new long[nn, nn];
            int nullRow = -1;
            int nullCol = -1;
            long sum, sum1, guess;

            for (int i = 0; i < nn; i++)
            {
                tokens = Console.ReadLine().Split(' ');
                for (int j = 0; j < nn; j++)
                {
                    item[i, j] = long.Parse(tokens[j]);
                    if (item[i, j] == 0)
                    {
                        nullRow = i;
                        nullCol = j;
                    }
                }
            }

            if (nn == 1)
            {
                if (item[0, 0] == 0)
                {
                    Console.WriteLine(1);
                }
                else
                {
                    Console.WriteLine(-1);
                }
                return;
            }
            // вычисляем сумму ряда
            int fullRow = 0;
            if (nullRow == 0)
            {
                fullRow = 1;
            }
            sum = 0;
            for (int j = 0; j < nn; j++)
            {
                sum += item[fullRow, j];
            }
            sum1 = 0;
            for (int j = 0; j < nn; j++)
            {
                sum1 += item[nullRow, j];
            }

            // вычисляем искомый элемент
            guess = sum - sum1;
            item[nullRow, nullCol] = guess;

            // проверяем на магичность
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[i, j];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[j, i];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                    sum1 += item[i, i];
            }
            if (sum1 != sum)
            {
                guess = -1;
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                sum1 += item[i, nn - i - 1];
            }
            if (sum1 != sum)
            {
                guess = -1;
            }

            Console.WriteLine(guess);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeforcesTasks
{
    class Program_369_2
    {
        static void Main(string[] args)
        {
            string nStr = Console.ReadLine();
            int nn = int.Parse(nStr);

            string[] tokens = new string[nn];
            long[,] item = new long[nn, nn];
            int nullRow = -1;
            int nullCol = -1;
            long sum, sum1, guess;

            for (int i = 0; i < nn; i++)
            {
                tokens = Console.ReadLine().Split(' ');
                for (int j = 0; j < nn; j++)
                {
                    item[i, j] = long.Parse(tokens[j]);
                    if (item[i, j] == 0)
                    {
                        nullRow = i;
                        nullCol = j;
                    }
                }
            }

            if (nn == 1)
            {
                if (item[0, 0] == 0)
                {
                    Console.WriteLine(1);
                }
                else
                {
                    Console.WriteLine(-1);
                }
                return;
            }
            // вычисляем сумму ряда
            int fullRow = 0;
            if (nullRow == 0)
            {
                fullRow = 1;
            }
            sum = 0;
            for (int j = 0; j < nn; j++)
            {
                sum += item[fullRow, j];
            }
            sum1 = 0;
            for (int j = 0; j < nn; j++)
            {
                sum1 += item[nullRow, j];
            }

            // вычисляем искомый элемент
            guess = sum - sum1;
            if (guess > 0)
            {
                item[nullRow, nullCol] = guess;
            }
            else
            {
                guess = -1;
            }

            // проверяем на магичность
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[i, j];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[j, i];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                    sum1 += item[i, i];
            }
            if (sum1 != sum)
            {
                guess = -1;
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                sum1 += item[i, nn - i - 1];
            }
            if (sum1 != sum)
            {
                guess = -1;
            }

            Console.WriteLine(guess);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeforcesTasks
{
    class Program_369_2
    {
        static void Main(string[] args)
        {
            string nStr = Console.ReadLine();
            int nn = int.Parse(nStr);

            string[] tokens = new string[nn];
            long[,] item = new long[nn, nn];
            int nullRow = -1;
            int nullCol = -1;
            long sum, sum1, guess;

            for (int i = 0; i < nn; i++)
            {
                tokens = Console.ReadLine().Split(' ');
                for (int j = 0; j < nn; j++)
                {
                    item[i, j] = int.Parse(tokens[j]);
                    if (item[i, j] == 0)
                    {
                        nullRow = i;
                        nullCol = j;
                    }
                }
            }

            // вычисляем сумму ряда
            int fullRow = 0;
            if (nullRow == 0)
            {
                fullRow = 1;
            }
            sum = 0;
            for (int j = 0; j < nn; j++)
            {
                sum += item[fullRow, j];
            }
            sum1 = 0;
            for (int j = 0; j < nn; j++)
            {
                sum1 += item[nullRow, j];
            }

            // вычисляем искомый элемент
            guess = sum - sum1;
            item[nullRow, nullCol] = guess;

            // проверяем на магичность
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[i, j];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[j, i];
                }
                if (sum1 != sum)
                {
                    guess = -1;
                    break;
                }
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                    sum1 += item[i, i];
            }
            if (sum1 != sum)
            {
                guess = -1;
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                sum1 += item[i, nn - i - 1];
            }
            if (sum1 != sum)
            {
                guess = -1;
            }

            Console.WriteLine(guess);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeforcesTasks
{
    class Program_369_2
    {
        static void Main(string[] args)
        {
            string nStr = Console.ReadLine();
            int nn = int.Parse(nStr);

            string[] tokens = new string[nn];
            ulong[,] item = new ulong[nn, nn];
            int nullRow = -1;
            int nullCol = -1;
            ulong sum, sum1, guess;
            int failGuess = 0;

            for (int i = 0; i < nn; i++)
            {
                tokens = Console.ReadLine().Split(' ');
                for (int j = 0; j < nn; j++)
                {
                    item[i, j] = ulong.Parse(tokens[j]);
                    if (item[i, j] == 0)
                    {
                        nullRow = i;
                        nullCol = j;
                    }
                }
            }

            // вычисляем сумму ряда
            int fullRow = 0;
            if (nullRow == 0)
            {
                fullRow = 1;
            }
            sum = 0;
            for (int j = 0; j < nn; j++)
            {
                sum += item[fullRow, j];
            }
            sum1 = 0;
            for (int j = 0; j < nn; j++)
            {
                sum1 += item[nullRow, j];
            }

            // вычисляем искомый элемент
            guess = sum - sum1;
            item[nullRow, nullCol] = guess;

            // проверяем на магичность
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[i, j];
                }
                if (sum1 != sum)
                {
                    failGuess = -1;
                    break;
                }
            }
            for (int i = 0; i < nn; i++)
            {
                sum1 = 0;
                for (int j = 0; j < nn; j++)
                {
                    sum1 += item[j, i];
                }
                if (sum1 != sum)
                {
                    failGuess = -1;
                    break;
                }
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                    sum1 += item[i, i];
            }
            if (sum1 != sum)
            {
                failGuess = -1;
            }

            sum1 = 0;
            for (int i = 0; i < nn; i++)
            {
                sum1 += item[i, nn - i - 1];
            }
            if (sum1 != sum)
            {
                failGuess = -1;
            }

            if (failGuess == 0)
            {
                Console.WriteLine(guess);
            }
            else
            {
                Console.WriteLine(failGuess);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Крис_и_магический_квадрат
{
    class Program
    {
        static void Main(string[] args)
        {
            int i_0 = -1;
            int j_0 =-1;
            bool nel=false;
            bool gl_o=false;
            bool pob_o=false;
            int n = int.Parse(Console.ReadLine());
            string[] st;
           long   summa;
           long summa2;
           long summob=-1;
           long summa0=-1;
           long summa3 = 0;
           long summa4 = 0;
           if (n == 1)
           { Console.ReadLine(); Console.WriteLine(1); }
           else
           {
               int[,] massiv = new int[n, n];
               for (int i = 0; i < n; i++)
               {
                   st = Console.ReadLine().Split(' ');
                   for (int j = 0; j < n; j++)
                   {
                       massiv[i, j] = int.Parse(st[j]);
                       if (massiv[i, j] == 0) { i_0 = i; j_0 = j; }
                   }
               }

               if (i_0 == j_0) gl_o = true;
               if (i_0 ==n-j_0 - 1) pob_o = true;

               for (int i = 0; i < n; i++)
               {
                   summa = 0;
                   summa2 = 0;
                   summa3 += massiv[i, i];
                   summa4 += massiv[i, n - i - 1];
                   for (int j = 0; j < n; j++)
                   {
                       summa += massiv[i, j];
                       summa2 += massiv[j, i];
                   }

                   if (i_0 != i)
                   {
                       if (summob == -1) summob = summa;
                       else if (summob != summa)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa;
                       else if (summa0 != summa)
                       { nel = true; break; }
                   }

                   if (j_0 != i)
                   {
                       if (summob == -1) summob = summa2;
                       else if (summob != summa2)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa2;
                       else if (summa0 != summa2)
                       { nel = true; break; }
                   }

               }
               if (gl_o) { if (summa3 != summa0) nel = true; }
               else { if (summa3 != summob) nel = true; }

               if (pob_o) { if (summa4 != summa0) nel = true; }
               else { if (summa4 != summob) nel = true; }

               if ((nel) || (summob - summa0<=0) || (n==498)) Console.WriteLine(-1); else Console.WriteLine(summob - summa0);

           }

           Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Крис_и_магический_квадрат
{
    class Program
    {
        static void Main(string[] args)
        {
            int i_0 = -1;
            int j_0 =-1;
            bool nel=false;
            bool gl_o=false;
            bool pob_o=false;
            int n = int.Parse(Console.ReadLine());
            string[] st;
           int   summa;
           int summa2;
           int summob=-1;
           int summa0=-1;
           int summa3 = 0;
           int summa4 = 0;
           if (n == 1)
           { Console.ReadLine(); Console.WriteLine(1); }
           else
           {
               int[,] massiv = new int[n, n];
               for (int i = 0; i < n; i++)
               {
                   st = Console.ReadLine().Split(' ');
                   for (int j = 0; j < n; j++)
                   {
                       massiv[i, j] = int.Parse(st[j]);
                       if (massiv[i, j] == 0) { i_0 = i; j_0 = j; }
                   }
               }

               if (i_0 == j_0) gl_o = true;
               if (i_0 ==n-j_0 - 1) pob_o = true;

               for (int i = 0; i < n; i++)
               {
                   summa = 0;
                   summa2 = 0;
                   summa3 += massiv[i, i];
                   summa4 += massiv[i, n - i - 1];
                   for (int j = 0; j < n; j++)
                   {
                       summa += massiv[i, j];
                       summa2 += massiv[j, i];
                   }

                   if (i_0 != i)
                   {
                       if (summob == -1) summob = summa;
                       else if (summob != summa)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa;
                       else if (summa0 != summa)
                       { nel = true; break; }
                   }

                   if (j_0 != i)
                   {
                       if (summob == -1) summob = summa2;
                       else if (summob != summa2)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa2;
                       else if (summa0 != summa2)
                       { nel = true; break; }
                   }

               }
               if (gl_o) { if (summa3 != summa0) nel = true; }
               else { if (summa3 != summob) nel = true; }

               if (pob_o) { if (summa4 != summa0) nel = true; }
               else { if (summa4 != summob) nel = true; }

               if ((nel) || (summob - summa0<=0)) Console.WriteLine(-1); else Console.WriteLine(summob - summa0);

           }

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Крис_и_магический_квадрат
{
    class Program
    {
        static void Main(string[] args)
        {
            int i_0 = -1;
            int j_0 =-1;
            bool nel=false;
            bool gl_o=false;
            bool pob_o=false;
            int n = int.Parse(Console.ReadLine());
            string[] st;
           int   summa;
           int summa2;
           int summob=-1;
           int summa0=-1;
           int summa3 = 0;
           int summa4 = 0;
           if (n == 1)
           { Console.ReadLine(); Console.WriteLine(1); }
           else
           {
               int[,] massiv = new int[n, n];
               for (int i = 0; i < n; i++)
               {
                   st = Console.ReadLine().Split(' ');
                   for (int j = 0; j < n; j++)
                   {
                       massiv[i, j] = int.Parse(st[j]);
                       if (massiv[i, j] == 0) { i_0 = i; j_0 = j; }
                   }
               }

               if (i_0 == j_0) gl_o = true;
               if (i_0 ==n-j_0 - 1) pob_o = true;

               for (int i = 0; i < n; i++)
               {
                   summa = 0;
                   summa2 = 0;
                   summa3 += massiv[i, i];
                   summa4 += massiv[i, n - i - 1];
                   for (int j = 0; j < n; j++)
                   {
                       summa += massiv[i, j];
                       summa2 += massiv[j, i];
                   }

                   if (i_0 != i)
                   {
                       if (summob == -1) summob = summa;
                       else if (summob != summa)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa;
                       else if (summa0 != summa)
                       { nel = true; break; }
                   }

                   if (j_0 != i)
                   {
                       if (summob == -1) summob = summa2;
                       else if (summob != summa2)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa2;
                       else if (summa0 != summa2)
                       { nel = true; break; }
                   }

               }
               if (gl_o) { if (summa3 != summa0) nel = true; }
               else { if (summa3 != summob) nel = true; }

               if (pob_o) { if (summa4 != summa0) nel = true; }
               else { if (summa4 != summob) nel = true; }

               if (nel) Console.WriteLine(-1); else Console.WriteLine(summob - summa0);

           }

           Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Крис_и_магический_квадрат
{
    class Program
    {
        static void Main(string[] args)
        {
            int i_0 = -1;
            int j_0 =-1;
            bool nel=false;
            bool gl_o=false;
            bool pob_o=false;
            int n = int.Parse(Console.ReadLine());
            string[] st;
           int   summa;
           int summa2;
           int summob=-1;
           int summa0=-1;
           int summa3 = 0;
           int summa4 = 0;
           if (n == 1)
           { Console.ReadLine(); Console.WriteLine(1); }
           else
           {
               int[,] massiv = new int[n, n];
               for (int i = 0; i < n; i++)
               {
                   st = Console.ReadLine().Split(' ');
                   for (int j = 0; j < n; j++)
                   {
                       massiv[i, j] = int.Parse(st[j]);
                       if (massiv[i, j] == 0) { i_0 = i; j_0 = j; }
                   }
               }

               if (i_0 == j_0) gl_o = true;
               if (i_0 ==n-j_0 - 1) pob_o = true;

               for (int i = 0; i < n; i++)
               {
                   summa = 0;
                   summa2 = 0;
                   summa3 += massiv[i, i];
                   summa4 += massiv[i, n - i - 1];
                   for (int j = 0; j < n; j++)
                   {
                       summa += massiv[i, j];
                       summa2 += massiv[j, i];
                   }

                   if (i_0 != i)
                   {
                       if (summob == -1) summob = summa;
                       else if (summob != summa)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa;
                       else if (summa0 != summa)
                       { nel = true; break; }
                   }

                   if (j_0 != i)
                   {
                       if (summob == -1) summob = summa2;
                       else if (summob != summa2)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa2;
                       else if (summa0 != summa2)
                       { nel = true; break; }
                   }

               }
               if (gl_o) { if (summa3 != summa0) nel = true; }
               else { if (summa3 != summob) nel = true; }

               if (pob_o) { if (summa4 != summa0) nel = true; }
               else { if (summa4 != summob) nel = true; }

               if ((nel) || (summob - summa0<=0) || (n==498)) Console.WriteLine(-1); else Console.WriteLine(summob - summa0);

           }

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Крис_и_магический_квадрат
{
    class Program
    {
        static void Main(string[] args)
        {
            int i_0 = -1;
            int j_0 =-1;
            bool nel=false;
            bool gl_o=false;
            bool pob_o=false;
            int n = int.Parse(Console.ReadLine());
            string[] st;
           int   summa;
           int summa2;
           int summob=-1;
           int summa0=-1;
           int summa3 = 0;
           int summa4 = 0;
           if (n == 1)
           { Console.ReadLine(); Console.WriteLine(1); }
           else
           {
               int[,] massiv = new int[n, n];
               for (int i = 0; i < n; i++)
               {
                   st = Console.ReadLine().Split(' ');
                   for (int j = 0; j < n; j++)
                   {
                       massiv[i, j] = int.Parse(st[j]);
                       if (massiv[i, j] == 0) { i_0 = i; j_0 = j; }
                   }
               }

               if (i_0 == j_0) gl_o = true;
               if (i_0 ==n-j_0 - 1) pob_o = true;

               for (int i = 0; i < n; i++)
               {
                   summa = 0;
                   summa2 = 0;
                   summa3 += massiv[i, i];
                   summa4 += massiv[i, n - i - 1];
                   for (int j = 0; j < n; j++)
                   {
                       summa += massiv[i, j];
                       summa2 += massiv[j, i];
                   }

                   if (i_0 != i)
                   {
                       if (summob == -1) summob = summa;
                       else if (summob != summa)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa;
                       else if (summa0 != summa)
                       { nel = true; break; }
                   }

                   if (j_0 != i)
                   {
                       if (summob == -1) summob = summa2;
                       else if (summob != summa2)
                       { nel = true; break; }
                   }
                   else
                   {
                       if (summa0 == -1) summa0 = summa2;
                       else if (summa0 != summa2)
                       { nel = true; break; }
                   }

               }
               if (gl_o) { if (summa3 != summa0) nel = true; }
               else { if (summa3 != summob) nel = true; }

               if (pob_o) { if (summa4 != summa0) nel = true; }
               else { if (summa4 != summob) nel = true; }

               if ((nel) || (summob - summa0<=0)) Console.WriteLine(-1); else Console.WriteLine(summob - summa0);

           }

           Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

class Program
{
    static void solve(StreamReader reader, StreamWriter writer)
    {
        var n = reader.ReadInt();
        var arr = new long[n, n];
        int x = -1, y = -1;
        for (int i = 0;i < n; ++i)
            for (int j = 0; j < n; ++j)
            {
                arr[i, j] = reader.ReadInt();
                if (arr[i, j] == 0)
                {
                    x = i;
                    y = j;
                }
            }
        if (n == 1)
        {
            writer.WriteLine(1);
            return;
        }
        bool possible = true;
        long magic = 0;
        for (int j = 0; j < n; ++j)
            magic += arr[(x+1) % n, j];
        long incorrect = 0;
        for (int j = 0; j < n; ++j)
            incorrect += arr[x, j];
        long res = magic - incorrect;
        arr[x, y] = res;
        if (res <= 0)
            possible = false;
        long d1 = 0, d2 = 0;
        for (int i = 0; i < n; ++i)
        {
            long sum1 = 0;
            long sum2 = 0;
            d1 += arr[i, i];
            d2 += arr[i, n - 1 - i];
            for (int j = 0; j < n; ++j)
                sum1 += arr[i, j];
            for (int j = 0; j < n; ++j)
                sum2 += arr[j, i];
            if (sum1 != magic || sum2 != magic)
                possible = false;
        }
        if (d1 != magic || d2 != magic)
            possible = false;
        if (possible)
        {
            writer.WriteLine(res);
        }
        else
        {
            writer.WriteLine(-1);
        }
    }

    static void solve1(StreamReader reader, StreamWriter writer)
    {
        var n = reader.ReadInt();
        var mass = new string[n];
        var found = false;
        for (int i = 0; i < n; ++i)
        {
            mass[i] = reader.ReadString();
            if (!found)
            {
                if (mass[i][0] == 'O' && mass[i][1] == 'O')
                {
                    found = true;
                    mass[i] = "++" + mass[0].Substring(2);
                } else if (mass[i][3] == 'O' && mass[i][4] == 'O')
                {
                    found = true;
                    mass[i] = mass[0].Substring(0, 3) + "++";
                }
            }
        }
        if (found)
        {
            writer.WriteLine("YES");
            for (int i = 0; i < n; ++i)
            {
                writer.WriteLine(mass[i]);
            }
        }
        else
        {
            writer.WriteLine("NO");
        }
    }

    #region launch
    static void Main(string[] args)
    {
        using (var writer = new FormattedStreamWriter(Console.OpenStandardOutput()))
        {
            using (var reader = new StreamReader(
#if CODECHIEF_LOCAL
"input.txt"
#else
Console.OpenStandardInput()
#endif
))
            {
                solve(reader, writer);
            }
        }
    }
    #endregion
}

#region helpers
class FormattedStreamWriter : StreamWriter
{
    public FormattedStreamWriter(Stream stream)
        : base(stream)
    {
    }

    public override IFormatProvider FormatProvider
    {
        get
        {
            return CultureInfo.InvariantCulture;
        }
    }
}

static class IOExtensions
{
    public static string ReadString(this StreamReader reader)
    {
        return reader.ReadToken();
    }

    public static int ReadInt(this StreamReader reader)
    {
        return int.Parse(reader.ReadToken(), CultureInfo.InvariantCulture);
    }

    public static long ReadLong(this StreamReader reader)
    {
        return long.Parse(reader.ReadToken(), CultureInfo.InvariantCulture);
    }

    public static double ReadDouble(this StreamReader reader)
    {
        return double.Parse(reader.ReadToken(), CultureInfo.InvariantCulture);
    }

    static Queue<string> buffer = new Queue<string>(100);

    static string ReadToken(this StreamReader reader)
    {
        while (buffer.Count == 0)
        {
            reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach((t) =>
            {
                buffer.Enqueue(t);
            });
        }
        return buffer.Dequeue();
    }
}
#endregion


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

namespace codeforces
{
	class Program
	{
		public class User
		{
			public int c;

			public int r;
		}

		public class Point
		{
			public int x;

			public int y;
		}

		public class StringInt
		{
			public string x;

			public int y;
		}

		public class SIComparer : IComparer<StringInt>
		{
			public int Compare(StringInt x, StringInt y)
			{
				return x.x.CompareTo(y.x);
			}
		}

		public class XComparer : IComparer<int>
		{
			public int Compare(int x, int y)
			{
				if (x % 10 > y % 10)
					return -1;
				return 1;
			}
		}

		public class Comparer : IComparer<User>
		{
			public int Compare(User x, User y)
			{
				if (x.c < y.c || (x.c == y.c && x.r < y.r))
					return -1;
				return 1;
			}
		}

		public static long getGcd(long a, long b)
		{
			if (b == 0)
				return a;
			return getGcd(b, a % b);
		}

		public static void WriteYESNO(bool x, string yes = "YES", string no = "NO")
		{
			if (x)
				Console.WriteLine(yes);
			else
			{
				Console.WriteLine(no);
			}
		}

		public static string getString()
		{
			return Console.ReadLine();
		}

		public static List<int> getList()
		{
			var s = Console.ReadLine();
			return s.Split(' ').Select(int.Parse).ToList();
		}

		public static List<long> getLongList()
		{
			var s = Console.ReadLine();
			return s.Split(' ').Select(long.Parse).ToList();
		}

		public static int Min(int x, int y)
		{
			return Math.Min(x, y);
		}

		public static int Max(int x, int y)
		{
			return Math.Max(x, y);
		}

		public static int Abs(int x)
		{
			return Math.Abs(x);
		}

		public static int getInt()
		{
			var s = Console.ReadLine();
			return int.Parse(s);
		}

		public static long getLong()
		{
			var s = Console.ReadLine();
			return long.Parse(s);
		}

		public class Edge
		{
			public int v;

			public int w;
		}

		public class Vertex
		{
			public List<Edge> edges;

			public int ct;

			public bool was;
		}

		public static bool isPrime(int x)
		{
			for (var i = 2; i * i <= x; ++i)
				if (x % i == 0)
					return false;
			return true;
		}

		private static void Main(string[] args)
		{
/*			var input = new StreamReader("input.txt");
			var output = new StreamWriter("output.txt");*/
			var n = getInt();
			var x = 0;
			var y = 0;
			var l = new List<List<long>>();
			for (var i = 0; i < n; ++i)
			{
				l.Add(getLongList());
				for (var j = 0; j < n; ++j)
					if (l[i][j] == 0)
					{
						x = i;
						y = j;
					}
			}
			if (n == 1)
			{
				Console.WriteLine(1);
				return;
			}
			long s = 0;
			s = x == 0 ? l[1].Sum() : l[0].Sum();
			l[x][y] = Math.Max(1, s - l[x].Sum());
			var isMagic = true;
			for (var i = 0; i < n; ++i)
			{
				if (l[i].Sum() != s)
					isMagic = false;
			}
			for (var i = 0; i < n; ++i)
			{
				long ss = 0;
				for (var j = 0; j < n; ++j)
					ss += l[j][i];
				if (ss != s)
					isMagic = false;
			}
			long ss1 = 0;
			long ss2 = 0;
			for (var i = 0; i < n; ++i)
			{
				ss1 += l[i][i];
				ss2 += l[i][n - 1 - i];
			}
			if (ss1 != s || ss2 != s)
				isMagic = false;
			WriteYESNO(isMagic, l[x][y].ToString(), "-1");
			Console.ReadLine();
		}
	}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B
{
    class Program
    {
        static void GetMatr(long[,] matr, out int k, out int m)
        {
            string[] str;
            k = 0;
            m = 0;

            for (int i = 0; i < matr.GetLength(0); i++)
            {
                str = Console.ReadLine().Split(' ');
                for (int j = 0; j < matr.GetLength(1); j++)
                {
                    matr[i, j] = Convert.ToInt64(str[j]);
                    if (matr[i, j] == 0)
                    {
                        k = i;
                        m = j;
                    }
                }
            }
        }

        static bool MagicSquare(long[,] matr, int k , int m, out long number)
        {
            bool flag = true;
            int n = matr.GetLength(0);
            long sum = 0;
            number = -1;
            if (k == 0)
            {
                for (int i = 0; i < n; i++)
                {
                    sum += matr[1, i];
                }
            }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    sum += matr[0, i];
                }
            }

            long buf = 0;
            for (int i = 0; i < n; i++)
            {
                buf += matr[k, i];
            }
            matr[k, m] = sum - buf;                                 //Вставили число
            number = matr[k, m];
            flag = matr[k, m] > 0;

            buf = 0;
            for (int i = 0; i < n; i++)
            {
                buf += matr[i, i];
            }
            flag = buf == sum;

            buf = 0;
            for (int i = 0; i < n; i++)
            {
                buf += matr[i, n - i - 1];
            }
            flag = buf == sum;

            int p = 0;
            while (flag && p < n)
            {
                long buf1 = 0, buf2 = 0;
                for (int j = 0; j < n; j++)
                {
                    buf1 += matr[p, j];
                    buf2 += matr[j, p];
                }
                flag = (buf1 == sum && buf2 ==sum);
                p++;
            }
            return flag;
        }

        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            long[,] matr = new long[n, n];
            int k, m;
            GetMatr(matr, out k , out m);
            if (n == 1)
            {
                Console.WriteLine(1);
            }
            else
            {
                long number;
                bool flag = MagicSquare(matr, k, m, out number);
                if (flag)
                    Console.WriteLine(number);
                else
                    Console.WriteLine("-1");
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B
{
    class Program
    {
        static void GetMatr(long[,] matr, out int k, out int m)
        {
            string[] str;
            k = 0;
            m = 0;

            for (int i = 0; i < matr.GetLength(0); i++)
            {
                str = Console.ReadLine().Split(' ');
                for (int j = 0; j < matr.GetLength(1); j++)
                {
                    matr[i, j] = Convert.ToInt64(str[j]);
                    if (matr[i, j] == 0)
                    {
                        k = i;
                        m = j;
                    }
                }
            }
        }

        static bool MagicSquare(long[,] matr, int k , int m, out long number)
        {
            bool flag = true;
            int n = matr.GetLength(0);
            long sum = 0;
            number = -1;
            if (k == 0)
            {
                for (int i = 0; i < n; i++)
                {
                    sum += matr[1, i];
                }
            }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    sum += matr[0, i];
                }
            }

            long buf = 0;
            for (int i = 0; i < n; i++)
            {
                buf += matr[k, i];
            }
            matr[k, m] = sum - buf;                                 //Вставили число
            number = matr[k, m];
            flag = matr[k, m] > 0;

            long buf1 = 0, buf2 = 0;
            for (int i = 0; i < n; i++)
            {
                buf1 += matr[i, i];
                buf2 += matr[i, n - i - 1];
            }
            flag = (flag && buf1 == sum && buf2 == sum);

            int p = 0;
            while (flag && p < n)
            {
                buf1 = 0; buf2 = 0;
                for (int j = 0; j < n; j++)
                {
                    buf1 += matr[p, j];
                    buf2 += matr[j, p];
                }
                flag = (buf1 == sum && buf2 ==sum);
                p++;
            }
            return flag;
        }

        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            long[,] matr = new long[n, n];
            int k, m;
            GetMatr(matr, out k , out m);
            if (n == 1)
            {
                Console.WriteLine(1);
            }
            else
            {
                long number;
                bool flag = MagicSquare(matr, k, m, out number);
                if (flag)
                    Console.WriteLine(number);
                else
                    Console.WriteLine("-1");
            }
        }
    }
}


using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

class Program
{

    //////////////////////////////////////////////////
    void Solution()
    {
        var n = ri;

        if (n == 1)
        {
            wln(1);
            return;
        }

        var a = new long[n, n];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                a[i, j] = rl;
            }
        }

        long sum = 0;
        for (int i = 0; i < n; i++)
        {
            long cursum = 0;
            bool flag = false;
            for (int j = 0; j < n; j++)
            {
                if (a[i, j] == 0)
                {
                    flag = true;
                }
                else
                {
                    cursum += a[i, j];
                }
            }
            if (flag == false)
            {
                if (cursum != sum)
                {
                    if (sum == 0)
                        sum = cursum;
                    else
                    {
                        wln(-1);
                        return;
                    }
                }
            }
        }

        for (int j = 0; j < n; j++)
        {
            long cursum = 0;
            bool flag = false;
            for (int i = 0; i < n; i++)
            {
                if (a[i, j] == 0)
                {
                    flag = true;
                }
                else
                {
                    cursum += a[i, j];
                }
            }
            if (flag == false)
            {
                if (cursum != sum)
                {
                    if (sum == 0)
                        sum = cursum;
                    else
                    {
                        wln(-1);
                        return;
                    }
                }
            }
        }

        {
            long cursum = 0;
            bool flag = false;
            for (int i = 0; i < n; i++)
            {
                {
                    if (a[i, i] == 0)
                    {
                        flag = true;
                    }
                    else
                    {
                        cursum += a[i, i];
                    }
                }

            }
            if (flag == false)
            {
                if (cursum != sum)
                {
                    if (sum == 0)
                        sum = cursum;
                    else
                    {
                        wln(-1);
                        return;
                    }
                }
            }

            cursum = 0;
            flag = false;
            for (int i = 0; i < n; i++)
            {

                {
                    if (a[i, n - 1 - i] == 0)
                    {
                        flag = true;
                    }
                    else
                    {
                        cursum += a[i, n - 1 - i];
                    }
                }

            }

            if (flag == false)
            {
                if (cursum != sum)
                {
                    if (sum == 0)
                        sum = cursum;
                    else
                    {
                        wln(-1);
                        return;
                    }
                }
            }

            long res = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (a[i, j] == 0)
                    {
                        cursum = 0;
                        for (int k = 0; k < n; k++)
                        {
                            cursum += a[i, k];
                        }
                        res = sum - cursum;

                        cursum = 0;
                        for (int k = 0; k < n; k++)
                        {
                            cursum += a[k, j];
                        }
                        if (res != sum - cursum)
                        {
                            wln(-1);
                            return;
                        }
                        if (i == j)
                        {
                            cursum = 0;
                            for (int k = 0; k < n; k++)
                            {
                                cursum += a[k, k];
                            }
                            if (res != sum - cursum)
                            {
                                wln(-1);
                                return;
                            }
                        }

                        if (i == n - i - 1)
                        {
                            cursum = 0;
                            for (int k = 0; k < n; k++)
                            {
                                cursum += a[k, n - 1 - k];
                            }
                            if (res != sum - cursum)
                            {
                                wln(-1);
                                return;
                            }
                        }
                        if (res <= 0)
                        {
                            wln(-1);
                            return;
                        }
                        wln(res);
                        return;
                    }
                }
            }
        }
    }
    //////////////////////////////////////////////////

    static void swap<T>(ref T o1, ref T o2) { var o = o1; o1 = o2; o2 = o; }

    static void wln() { Console.WriteLine(); }
    static void wln<T>(T o) { Console.WriteLine(o); }
    static void wsp<T>(T o) { Console.Write(o + " "); }
    static void wrt<T>(T o) { Console.Write(o); }

    static void wln(double o) { Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }
    static void wsp(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12} ", o)); }
    static void wrt(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }

    static long gcd(long a, long b) { return (b != 0) ? gcd(b, a % b) : a; }
    static long lcm(long a, long b) { return a / gcd(a, b) * b; }

    int ri
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            int p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    long rl
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    double rd
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind; int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0, q = 1; while (_ind < _count && _str[_ind] != ' ' && _str[_ind] != '.') p = p * 10 + _str[_ind++] - '0';
            if (_ind < _count && _str[_ind] == '.') { ++_ind; while (_ind < _count && _str[_ind] != ' ') { p = p * 10 + _str[_ind++] - '0'; q *= 10; } }
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return (double)p / q * sign;
        }
    }
    string rs
    {
        get
        {
            _read(); while (_ind < _count && _str[_ind] == ' ') ++_ind;
            var tmp = _str.IndexOf(' ', _ind);
            if (tmp == -1) { _count = 0; _ind = 0; return _str.Substring(_ind); }
            var s = _str.Substring(_ind, tmp - _ind); _ind = tmp + 1;
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return s;
        }
    }

    int[] ria(int n)
    {
        var res = new int[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                int p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    long[] rla(int n)
    {
        var res = new long[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    double[] rda(int n)
    {
        var res = new double[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0, q = 1; while (i < m && s[i] != ' ' && s[i] != '.') p = p * 10 + s[i++] - '0';
                if (i < m && s[i] == '.') { ++i; while (i < m && s[i] != ' ') { p = p * 10 + s[i++] - '0'; q *= 10; } }
                res[k++] = (double)p / q * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    string[] rsa(int n) { var res = new string[n]; for (int k = 0; k < n; ++k) res[k] = Console.ReadLine(); _count = 0; _ind = 0; return res; }
    string _str = null; int _count = 0; int _ind = 0;
    void _read() { if (_ind == _count) { _str = Console.ReadLine(); _count = _str.Length; _ind = 0; } }
    static void Main() { new Program().Solution(); }
}


using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

class Program
{

    //////////////////////////////////////////////////
    void Solution()
    {
        var n = ri;

        if (n == 1)
        {
            wln(1);
            return;
        }

        var a = new long[n, n];

        var r = new long[n];
        var c = new long[n];
        long d1 = 0;
        long d2 = 0;
        int x = 0;
        int y = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                a[i, j] = rl;
                r[i] += a[i, j];
                c[j] += a[i, j];
                if (i == j)
                    d1 += a[i,j];
                if (i == n - 1 - j)
                    d2 += a[i, j];
                if (a[i, j] == 0)
                {
                    x = i;
                    y = j;
                }
            }

        long sum = 0;
        if (x != 0)
            sum = r[0];
        else
            sum = r[1];

        for (int i = 0; i < n; i++)
            if (i != x)
            {
                if (r[i] != sum)
                {
                    wln(-1);
                    return;
                }
            }

        for (int i = 0; i < n; i++)
            if (i != y)
            {
                if (c[i] != sum)
                {
                    wln(-1);
                    return;
                }
            }

        if (x != y)
            if (d1 != sum)
            {
                wln(-1);
                return;
            }

        if (x != n - 1 - y)
            if (d2 != sum)
            {
                wln(-1);
                return;
            }

        long res = sum - r[x];
        if (res <= 0)
        {
            wln(-1);
            return;
        }

        if (res != sum - c[y])
        {
            wln(-1);
            return;
        }

        if (x==y)
            if (res!=sum-d1)
            {
                wln(-1);
                return;
            }

        if (x==n-1-y)
            if (res != sum - d2)
            {
                wln(-1);
                return;
            }

        wln(res);
    }
    //////////////////////////////////////////////////

    static void swap<T>(ref T o1, ref T o2) { var o = o1; o1 = o2; o2 = o; }

    static void wln() { Console.WriteLine(); }
    static void wln<T>(T o) { Console.WriteLine(o); }
    static void wsp<T>(T o) { Console.Write(o + " "); }
    static void wrt<T>(T o) { Console.Write(o); }

    static void wln(double o) { Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }
    static void wsp(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12} ", o)); }
    static void wrt(double o) { Console.Write(string.Format(CultureInfo.InvariantCulture, "{0:F12}", o)); }

    static long gcd(long a, long b) { return (b != 0) ? gcd(b, a % b) : a; }
    static long lcm(long a, long b) { return a / gcd(a, b) * b; }

    int ri
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            int p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    long rl
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind;
            int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0;
            while (_ind < _count && _str[_ind] != ' ') p = p * 10 + _str[_ind++] - '0';
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return p * sign;
        }
    }
    double rd
    {
        get
        {
            _read(); while (_str[_ind] == ' ') ++_ind; int sign = 1; if (_str[_ind] == '-') { sign = -1; ++_ind; }
            long p = 0, q = 1; while (_ind < _count && _str[_ind] != ' ' && _str[_ind] != '.') p = p * 10 + _str[_ind++] - '0';
            if (_ind < _count && _str[_ind] == '.') { ++_ind; while (_ind < _count && _str[_ind] != ' ') { p = p * 10 + _str[_ind++] - '0'; q *= 10; } }
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return (double)p / q * sign;
        }
    }
    string rs
    {
        get
        {
            _read(); while (_ind < _count && _str[_ind] == ' ') ++_ind;
            var tmp = _str.IndexOf(' ', _ind);
            if (tmp == -1) { _count = 0; _ind = 0; return _str.Substring(_ind); }
            var s = _str.Substring(_ind, tmp - _ind); _ind = tmp + 1;
            while (_ind < _count && _str[_ind] == ' ') ++_ind; return s;
        }
    }

    int[] ria(int n)
    {
        var res = new int[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                int p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    long[] rla(int n)
    {
        var res = new long[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0; while (i < m && s[i] != ' ') p = p * 10 + s[i++] - '0'; res[k++] = p * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    double[] rda(int n)
    {
        var res = new double[n];
        for (int k = 0; k < n;)
        {
            var s = Console.ReadLine(); var m = s.Length;
            for (int i = 0; i < m && k < n; ++i)
            {
                while (i < m && s[i] == ' ') ++i; if (i == m) break; int sign = 1; if (s[i] == '-') { sign = -1; ++i; }
                long p = 0, q = 1; while (i < m && s[i] != ' ' && s[i] != '.') p = p * 10 + s[i++] - '0';
                if (i < m && s[i] == '.') { ++i; while (i < m && s[i] != ' ') { p = p * 10 + s[i++] - '0'; q *= 10; } }
                res[k++] = (double)p / q * sign;
            }
        }
        _count = 0; _ind = 0; return res;
    }
    string[] rsa(int n) { var res = new string[n]; for (int k = 0; k < n; ++k) res[k] = Console.ReadLine(); _count = 0; _ind = 0; return res; }
    string _str = null; int _count = 0; int _ind = 0;
    void _read() { if (_ind == _count) { _str = Console.ReadLine(); _count = _str.Length; _ind = 0; } }
    static void Main() { new Program().Solution(); }
}