ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.

Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid ($\sum a_{r, i}$), each column of the grid ($\sum a_{i, c}$), and the two long diagonals of the grid (the main diagonal — $\sum a_{i, i}$ and the secondary diagonal — $\sum a_{i, n - i + 1}$) are equal. 

Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?


-----Input-----

The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes a_{i}, j (1 ≤ a_{i}, j ≤ 10^9 or a_{i}, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, a_{i}, j will be equal to 0. Otherwise, a_{i}, j is positive.

It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that a_{i}, j = 0.


-----Output-----

Output a single integer, the positive integer x (1 ≤ x ≤ 10^18) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

If there are multiple solutions, you may print any of them.


-----Examples-----
Input
3
4 0 2
3 5 7
8 1 6

Output
9

Input
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1

Output
1

Input
4
1 1 1 1
1 1 0 1
1 1 2 1
1 1 1 1

Output
-1



-----Note-----

In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed, 

The sum of numbers in each row is:

4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.

The sum of numbers in each column is:

4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.

The sum of numbers in the two diagonals is:

4 + 5 + 6 = 2 + 5 + 8 = 15.

In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.

Difficulty: interview

Starter code:

Solutions:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def func():
    N = int(input())
    cells = [0] * N

    if N == 1:
        return 1

    mx = 0
    for n in range(N):
        cells[n] = list(map(int,input().split()))
        mx = max(mx, sum(cells[n]))

    ans = None
    for j in range(N):
        for i in range(N):
            if cells[j][i] == 0:
                ans = mx - sum(cells[j])
                cells[j][i] = ans
                if ans <= 0:
                    return -1

    # validation
    for j in range(N):
        if sum(cells[j]) != mx:
            return -1
    for i in range(N):
        if mx != sum([cells[j][i] for j in range(N)]):
            return -1
    if mx != sum([cells[j][j] for j in range(N)]):
        return -1
    if mx != sum([cells[j][N-1-j] for j in range(N)]):
        return -1
    
    return ans

print(func())



n = int(input())
s = -1
s1 = -1
x = -1
y = -1
mat = []
if n == 1:
    input()
    print(1)
    return
for i in range(n):
    arr = list(map(int, input().split()))
    if s == -1 and 0 not in arr:
        s = sum(arr)
    if 0 in arr:
        x = i
        s1 = sum(arr)
        y = arr.index(0)
    mat.append(arr)
mat[x][y] = s - s1
if mat[x][y] <= 0:
    print(-1)
    return
for i in range(n):
    if sum(mat[i]) != s:
        print(-1)
        return
    s2 = 0
    for j in range(n):
        s2 += mat[j][i]
    if s2 != s:
        print(-1)
        return
s2 = 0
s3 = 0
for i in range(n):
    s2 += mat[i][i]
    s3 += mat[i][n-i-1]
if s2 != s or s3 != s:
    print(-1)
    return
print(mat[x][y])

#!/usr/bin/env python3

n = int(input())
s = [[int(x) for x in input().split()] for _ in range(n)]

p, q = 0, 0

for i in range(n):
    for j in range(n):
        if s[i][j] == 0:
            p, q = i, j

if n > 1:
    m = sum(s[(p+1)%n])
    s[p][q] = m - sum(s[p])
else:
    m = 1
    s[p][q] = 1

f = True

for i in range(n):
    if sum(s[i]) != m:
        f = False
    if sum(s[j][i] for j in range(n)) != m:
        f = False
        
if sum(s[i][i] for i in range(n)) != m:
    f = False
if sum(s[i][n-i-1] for i in range(n)) != m:
    f = False

if f and s[p][q] > 0:
    print(s[p][q])
else:
    print("-1")


n = int(input())
grid = [[int(x) for x in input().split()] for _ in range(n)]

if n == 1:
	print(1)
	return

sm = sum(grid[1]) if 0 in grid[0] else sum(grid[0])

answer = -1
for i, line in enumerate(grid):
	if 0 in line:
		j = line.index(0)
		answer = sm - sum(line)
		grid[i][j] = answer
		break


for line in grid:
	if sum(line) != sum(grid[0]):
		print(-1)
		return

for i in range(n):
	row_count = 0
	for j in range(n):
		row_count += grid[j][i]

	if row_count != sum(grid[0]):
		print(-1)
		return

count = 0
for i in range(n):
	count += grid[i][i]

if count != sum(grid[0]):
	print(-1)
	return

count = 0
for i in range(n):
	count += grid[n-1-i][i]

if count != sum(grid[0]):
	print(-1)
	return

print(answer if answer > 0 else -1)


N = int(input())
if N == 1:
    print(1)
    return
mat = []
for i in range(N):
    mat.append(list(map(int, input().split())))
    if 0 in mat[i]:
        zero_pos = (i, mat[i].index(0))

# 和を求めておく
if zero_pos[0] == 0:
    v_sum = sum(mat[1])
else:
    v_sum = sum(mat[0])

# 和から0を埋める
zero_row_sum = sum(mat[zero_pos[0]])
if v_sum - zero_row_sum <= 0:
    print(-1)
    return
mat[zero_pos[0]][zero_pos[1]] = v_sum - zero_row_sum

# row check
for i in range(N):
    row_sum = sum(mat[i])
    if row_sum != v_sum:
        print(-1)
        return

# col check
for j in range(N):
    col_sum = sum([mat[i][j] for i in range(N)])
    if col_sum != v_sum:
        print(-1)
        return

# diag check
diag_sum = sum([mat[i][i] for i in range(N)])
if diag_sum != v_sum:
    print(-1)
    return

# inv diag check
inv_diag_sum = sum([mat[i][N - i - 1] for i in range(N)])
if inv_diag_sum != v_sum:
    print(-1)
    return

print(mat[zero_pos[0]][zero_pos[1]])


n = int(input())

grid = []

for i in range(n):
    grid.append(list(map(int, input().split())))

if n == 1:
    print(1)
    return

checksum = -1;

left = 0
mis = -1;
for i, col in enumerate(grid):
    tmp = sum(col)
    if 0 in col:
        left = i
        mis = tmp
    else:
        if checksum == -1:
            checksum = tmp
        elif checksum != tmp:
            print(-1)
            return

if mis >= checksum:
    print(-1)
    return

grid[left] = [x if x else checksum - mis for x in grid[left]]

diag1 = 0
diag2 = 0
for i in range(n):
    colsum = 0
    rowsum = 0
    diag1 += grid[i][i]
    diag2 += grid[n-1-i][i]
    for j in range(n):
        colsum += grid[i][j]
        rowsum += grid[j][i]

    if colsum != checksum or rowsum != checksum:
        print(-1)
        return

if diag1 != checksum or diag2 != checksum:
    print(-1)
    return

print(checksum - mis)


n = int(input()) 
if n == 1:
	print('1')
	return
A = []
C = [0 for i in range(n)]
diag1 = 0
diag2 = 0
for i in range(n):
	B = list(map(int, input().split()))
	for j in range(n):
		el = B[j]
		C[j] += el
		if i == j:
			diag1 += el
		if i + j == n - 1:
			diag2 += el	
		if el == 0:
			x = i
			y = j
	A.append(B)
if x == 0:
	s = sum(A[1])
else:
	s = sum(A[0])	
for i in range(n):
	if i != x:
		if s != sum(A[i]):
			print('-1')
			return
for i in range(n):
	if i != y:
		if s != C[i]:
			print('-1')
			return
ans = s - C[y]
if ans + sum(A[x]) != s:
	print('-1')
	return
if x == y:
	diag1 += ans
if x + y == n - 1:
	diag2 += ans
if diag1 != s:
	print('-1')
	return
if diag2 != s:
	print('-1')
	return							
if ans < 1:
	print('-1')
	return
print(ans)


n = int(input())
line = []
for i in range(n):
    line += [list(map(int, input().split()))]
list_str = []
if line == [[0]]:
    print(1)
else:    
    for i in range(n):
        plus = False
        answer = 0
        for j in range(n):
            element = line[i][j]
            if element == 0:
                plus = True
            answer += element
        if plus:
            osob = answer
        else:
            list_str += [answer]
    if len(list_str) != 0 and min(list_str) != max(list_str):
        print(-1)
    else:
        chislo1 = min(list_str) - osob
        s1 = min(list_str)    
        list_str = []    
        for i in range(n):
            plus = False
            answer = 0
            for j in range(n):
                element = line[j][i]
                if element == 0:
                    plus = True
                answer += element
            if plus:
                osob = answer
            else:
                list_str += [answer]
        if len(list_str) == 0 or min(list_str) != max(list_str):
            print(-1)
        else:
            chislo2 = min(list_str) - osob
            s2 = min(list_str)    
            answer = 0
            plus = False
            for i in range(n):
                element = line[i][i]
                answer += element
                if element == 0:
                    plus = True
            if not plus:
                s3 = answer
                chislo3 = chislo2
            else:    
                s3 = s2
                chislo3 = s2 - answer
            answer = 0
            plus = False
            for i in range(n):
                element = line[i][n - i - 1]
                answer += element
                if element == 0:
                    plus = True
            if not plus:
                s4 = answer
                chislo4 = chislo2
            else:    
                chislo4 = s2 - answer 
                s4 = s2 
            if s1 == s2 and s1 == s3 and s1 == s4 and chislo1 == chislo2 and chislo1 == chislo3 and chislo1 == chislo4 and s1 > 0 and chislo1 > 0:
                print(chislo1)
            else:
                print(-1)


n = int(input())
arr = []
for i in range(n):
    arr.append([int(x) for x in input().split()])
if(n==1):
    print(1)
    return

ii,jj = 0,0
for i in range(n):
    for j in range(n):
        if(arr[i][j]==0):
            ii,jj = i,j

prr = []
qrr = []
for i in range(n):
    if(i==ii):
        qrr.append(sum(arr[i]))
    else:
        prr.append(sum(arr[i]))
for j in range(n):
    s = 0
    for i in range(n):
        s += arr[i][j]
    if(j==jj):
        qrr.append(s)
    else:
        prr.append(s)
s = 0
for i in range(n):
    s += arr[i][i]
if(ii==jj):
    qrr.append(s)
else:
    prr.append(s)
s = 0
for i in range(n):
    s += arr[i][n-i-1]
if(ii+jj==n-1):
    qrr.append(s)
else:
    prr.append(s)

if(len(prr)==prr.count(prr[0]))and(len(qrr)==qrr.count(qrr[0])):
    x = prr[0]-qrr[0]
    if(x>0):
        print(x)
    else:
        print(-1)
else:
    print(-1)

n = int(input())
k = []
for i in range(n):
	a = [int(i) for i in input().split()]
	k.append(a)
if n == 1:
	print(1)
else:
	zer = []
	for i in range(n):
		for j in range(n):
			if 0 == k[i][j]:
				zer = [i, j]
	if zer[0] == 0:
		m = sum(k[1])
	else:
		m = sum(k[0])
	k[zer[0]][zer[1]] = m-sum(k[zer[0]])
	if k[zer[0]][zer[1]] > 0:
		y = 1
	else:
		y = 0
	for i in range(n):
		if sum(k[i]) != m:
			y = 0
	for j in range(n):
		u = 0
		for i in range(n):
			u+= k[i][j]
		if u!= m:
			y = 0
	u = 0
	for i in range(n):
		u+=k[i][i]
	if u!= m:
		y = 0
	u = 0
	for i in range(n):
		u+=k[i][n-i-1]
	if u!= m:
		y = 0
	if y == 1:
		print(k[zer[0]][zer[1]])
	else:
		print(-1)


def main():
    def magic(sq, n):
        s1 = sum(sq[0])
        for i in range(1, n):
            if s1 != sum(sq[i]):
                return False
        s2 = 0
        for i in range(n):
            s2 += sq[i][i]
        if s2 != s1:
            return False
        s2 = 0
        for i in range(n):
            s2 += sq[i][n - i - 1]
        if s2 != s1:
            return False
        for i in range(n):
            s2 = 0
            for j in range(n):
                s2 += sq[j][i]
            if s2 != s1:
                return False
        return True
    n = int(input())
    sq = [list(map(int, input().split())) for i in range(n)] + [[1]]
    for i in range(n):
        if 0 in sq[i]:
            cors = (i, sq[i].index(0))
    if cors[0] != 0:
        s = sum(sq[0])
    else:
        s = sum(sq[1])
    sq[cors[0]][cors[1]] = s - sum(sq[cors[0]])
    if magic(sq, n) and sq[cors[0]][cors[1]] >= 1:
        print(sq[cors[0]][cors[1]])
    else:
        print(-1)

main()

#B
n = int(input())
a = [list(map(int,input().split())) for j in range(n)]

if n == 1:
    print(1)
    return

for j in range(n):
    for i in range(n):
        if a[j][i] == 0:
            y = j
            x = i
            break

slist = []
for j in range(n):
    if j == y:
        continue
    ss = 0
    for i in range(n):
        ss += a[j][i]
    slist.append(ss)

for i in range(n):
    if i == x:
        continue
    ss = 0
    for j in range(n):
        ss += a[j][i]
    slist.append(ss)

if x != y:
    ss = 0
    for i in range(n):
        ss += a[i][i]
    slist.append(ss)

if x != n - 1 - y:
    ss = 0
    for i in range(n):
        ss += a[n - 1 - i][i]
    slist.append(ss)

slist.sort()

if slist[0] != slist[-1]:
    print(-1)
    return

s = slist[0]
s1 = s
s2 = s
s3 = s
s4 = s

for i in range(n):
    s1 -= a[y][i]

for i in range(n):
    s2 -= a[i][x]

if x == y:
    for i in range(n):
        s3 -= a[i][i]
else:
    s3 = s1

if x == n - 1 - y:
    for i in range(n):
        s4 -= a[n - 1 - i][i]
else:
    s4 = s1

if s1 == s2 == s3 == s4 and s1 > 0:
    print(s1)
else:
    print(-1)

#!/usr/bin/env pypy3

NO_SOL = -1


def main():
    n = int(input())
    if n == 1:
        print(1)
        return
    zero = None
    grid = []
    for i in range(n):
        grid.append(list(map(int, input().split())))
        if zero is None:
            try:
                zero = (i, grid[i].index(0))
            except Exception:
                zero = None
    sum_sample = None
    for i in range(n):
        if i == zero[0]:
            continue
        else:
            sum_sample = sum(grid[i])
            break
    zero_cand = sum_sample - sum(grid[zero[0]])
    if zero_cand <= 0:
        print(NO_SOL)
        return
    grid[zero[0]][zero[1]] = zero_cand
    for row in grid:
        if sum(row) != sum_sample:
            print(NO_SOL)
            return
    for column in zip(*grid):
        if sum(column) != sum_sample:
            print(NO_SOL)
            return
    if sum(grid[i][i] for i in range(n)) != sum_sample:
        print(NO_SOL)
        return
    if sum(grid[i][n - i - 1] for i in range(n)) != sum_sample:
        print(NO_SOL)
        return
    print(zero_cand)


def __starting_point():
    main()

__starting_point()

#!/usr/bin/env python3

def check(a, t):
    n = len(a)
    for i in range(n):
        if sum(a[i]) != t:
            return False
    for j in range(n):
        if sum(a[i][j] for i in range(n)) != t:
            return False
    return sum(a[i][i] for i in range(n)) == sum(a[i][-i - 1] for i in range(n)) == t

def main():
    try:
        while True:
            n = int(input())
            a = [list(map(int, input().split())) for i in range(n)]
            if n == 1:
                print(1)
            else:
                try:
                    j = a[0].index(0)
                    i = 0
                    t = sum(a[1])
                    x = t - sum(a[0])
                except ValueError:
                    t = sum(a[0])
                    for i in range(1, n):
                        try:
                            j = a[i].index(0)
                            x = t - sum(a[i])
                            break
                        except ValueError:
                            pass
                a[i][j] = x
                print(x if x > 0 and check(a, t) else -1)

    except EOFError:
        pass

main()


from sys import stdin

n=int(stdin.readline())
carre=[list(map(int,stdin.readline().split())) for i in range(n)]
sommesCol=[0]*n
sommesLig=[0]*n
if n==1:
    print(1)
    return
    
for lig in range(n):
    for col in range(n):
        if carre[lig][col]==0:
            ligManque=lig
            colManque=col
        else:
            sommesCol[col]+=carre[lig][col]
            sommesLig[lig]+=carre[lig][col]

sommeDiagonale=0
sommeDiagonaleInv=0
for i in range(n):
    sommeDiagonale+=carre[i][i]
    sommeDiagonaleInv+=carre[i][n-i-1]
reference=sommesCol[(colManque+1)%n]
remplacement=reference-sommesCol[colManque]

sommesCol[colManque]+=remplacement
sommesLig[ligManque]+=remplacement
if ligManque==colManque:
    sommeDiagonale+=remplacement
if ligManque==n-colManque-1:
    sommeDiagonaleInv+=remplacement

possible=True
if sommeDiagonale!=reference:
    possible=False
if sommeDiagonaleInv!=reference:
    possible=False
for col in range(n):
    if sommesCol[col]!=reference:
        possible=False
for lig in range(n):
    if sommesLig[lig]!=reference:
        possible=False

if not possible or remplacement<=0:
    print(-1)
else:
    print(remplacement)



n = int(input())

v = [list(map(int, input().split())) for _ in range(n)]

if n == 1:
    print(1)
    return

def sum_diag1(v):
    s = 0
    for i in range(len(v)):
        s += v[i][i]
    return s

def sum_diag2(v):
    s = 0
    for i in range(len(v)):
        s += v[i][len(v) - 1 - i]
    return s

def sum_row(v, r):
    return sum(v[r])

def sum_col(v, c):
    s = 0
    for r in range(len(v)):
        s += v[r][c]
    return s

row = 0
col = 0

needed_sum = 0
row_sum = 0
col_sum = 0
diag1_sum = 0
diag2_sum = 0

for r in range(len(v)):
    if 0 in v[r]:
        row = r
        col = v[r].index(0)
        if needed_sum != 0:
            break
    elif needed_sum == 0:
        needed_sum = sum(v[r])

t = needed_sum - sum(v[row])
v[row][col] = t

l = [sum_row(v, r) for r in range(n)]
l += [sum_col(v, c) for c in range(n)]
l += [sum_diag1(v), sum_diag2(v)]

if len(set(l)) == 1 and t > 0:
    print(t)
else:
    print(-1)
    
    


n=int(input())
rs=[]
cs=[0 for i in range(n)]
d1=0
d2=0
for i in range(n):
    ip=list(map(int,input().split()))
    rs.append(sum(ip))
    if 0 in ip:
        l=i
        k=ip.index(0)
    for j in range(n):
        cs[j]+=ip[j]
    d1+=ip[i]
    d2+=ip[n-i-1]
if n==1:
    print(1)
else:
    if l==0:
        r=rs[1]
    else:
        r=rs[0]
    ans=r-rs[l]
    if l==k:
        d1+=ans
    if l==n-k-1:
        d2+=ans
    rc=0
    cc=0
    for i in rs:
        if i!=r:
            rc+=1
    for i in cs:
        if i!=r:
            cc+=1
    if rc!=1 or cc!=1:
        print(-1)
    elif d1!=r or d2!=r:
        print(-1)
    elif ans<=0:
        print(-1)
    else:
        print(ans)



lines = int(input())
if lines == 1:
    print(1)
    return
grid = []
number_with_zero = set()
impossible = False
no_zero = -1

for x in range(lines):
    num = list(map(int, input().split()))
    grid.append(num)

for line in grid:
    have_zero = False
    s = 0
    for n in line:
        if n ==0:
            have_zero = True
        else:
            s += n
    if have_zero:
        number_with_zero.add(s)
    elif no_zero == -1:
        no_zero = s
    elif no_zero != s:
        impossible = True

# print(impossible, 1)
for x in range(lines):
    have_zero = False
    s = 0
    for y in range(lines):
        n =  grid[y][x]
        if n ==0:
            have_zero = True
        else:
            s += n
    if have_zero:
        number_with_zero.add(s)
    elif no_zero == -1:
        no_zero = s
    elif no_zero != s:
        impossible = True
# print(impossible, 2)

s = 0
have_zero = False
for x in range(lines):
    n =  grid[x][x]
    if n ==0:
        have_zero = True
    else:
        s += n

# print(s, no_zero)
if have_zero:
    number_with_zero.add(s)
elif no_zero == -1:
    no_zero = s
elif no_zero != s:
    impossible = True
# print(impossible, 3)

s = 0
have_zero = False
for x in range(lines):
    n =  grid[x][lines -1 - x]
    if n ==0:
        have_zero = True
    else:
        s += n

if have_zero:
    # print("COME")
    number_with_zero.add(s)
elif no_zero == -1:
    no_zero = s
elif no_zero != s:
    impossible = True
# print(impossible, 4)

if impossible:
    print(-1)
else:
    if len(number_with_zero) == 1:
        num = list(number_with_zero)[0]
        # print(num)
        if (no_zero - num <= 0):
            print(-1)
        else:
            print(no_zero - num)
    else:
        print(-1)


from collections import defaultdict
import sys, os, math

def __starting_point():
    #n, m = list(map(int, input().split()))
    n = int(input())
    s = [list(map(int, input().split())) for i in range(n)]
    if n == 1:
        print(1)
        return
    pos = []
    for i in range(n):
        for j in range(n):
            if s[i][j] == 0:
                pos = [i, j]
                break;
    val = sum(s[(pos[0] + 1) % n])
    s[pos[0]][pos[1]] = max(val - sum(s[pos[0]]), 1)
    #check
    for i in range(n):
        if sum(s[i]) != val:
            print(-1)
            return
    for j in range(n):
        t = 0
        for i in range(n):
            t += s[i][j]
        if t != val:
            print(-1)
            return
    t, tt = 0, 0
    for i in range(n):
        t += s[i][i]
        tt += s[i][n - 1 - i]
    if t != val or tt != val:
        print(-1)
        return
           
    print(s[pos[0]][pos[1]])
    '''
    for i in range(n):
        print(' '.join(map(str, s[i])))
    '''
__starting_point()

import sys
n = int(input())
mat = []
empty = (None, None)
if n == 1:
    print(1)
    return
for i in range(n):
    row = [int(x) for x in input().strip().split()]
    if 0 in row:
        col = row.index(0)
        empty = (i, col)
    mat.append(row)
i = 0
if 0 in mat[i]:
    i += 1
rowsum = sum(mat[i])
row = empty[0]
for i in range(n):
    if i == row:
        continue
    cursum = sum(mat[i])
    # print(cursum)
    if cursum != rowsum:
        print(-1)
        return
tmat = list(zip(*mat))
col = empty[1]
for i in range(n):
    if i == col:
        continue
    else:
        #print(cursum)
        cursum = sum(tmat[i])
        if cursum != rowsum:
            print(-1)
            return
row = empty[0]
irowsum = sum(mat[row])
colsum = sum(tmat[col])
#print(irowsum, colsum)
if irowsum != colsum:
    print(-1)
    return
left = 0
right = 0
tochange = rowsum - irowsum
if tochange <= 0:
    print(-1)
    return
mat[row][col] = tochange
if True:
    for i in range(n):
        #print(mat[i][i], mat[i][n - i - 1])
        left += mat[i][i]
        right += mat[i][n - i - 1]
    #print(left, right)
    #print(row, col, left, rowsum)
if left != right or left != rowsum:
    print(-1)
    return
print(tochange)


#imports
import sys
sys.setrecursionlimit(30000)

from collections import Counter, defaultdict, deque
from math import ceil, floor, factorial, fsum, isinf, exp, log, log10, log2, isfinite, sqrt
from math import pi as PI, e as E
from math import sin, cos, tan, sinh, cosh, tanh, asin, acos, atan
from fractions import Fraction
from itertools import starmap, tee, chain, filterfalse, combinations as combos
from itertools import permutations as perms, product as prod, combinations_with_replacement as rcombos
from functools import reduce, partial
import operator as ops #lt, le, eq, ne, ge, gt,  xor  notit, lshift, rshift,      neg, add, mul, sub,
from operator import __not__ as notit, __abs__ as absit, __or__ as orit
from bisect import insort_left, bisect_left, bisect_right
from copy import deepcopy


#PI, E, PHI, INF
PHI, PHI2 = (1 + 5 ** 0.5) / 2, (5 ** 0.5 - 1) / 2
INF = float('inf')

#structures
class TreeNode:
    def __init__(self, v):
        self.val = v
        self.left = None
        self.right = None

#Bit Manipulation
#<<, >>, bin(), int(s, 2)
def setBit(x, offset):
    return x | 1 << offset #RHS: mask
def clearBit(x, offset):
    return x & ~(1 << offset) #RHS: mask
def getBit(x, offset):
    return 1 if testBit(x, offset) > 0 else 0
def testBit(x, offset):
    return x & 1 << offset #RHS: mask
def flipBitAt(x, offset):
    return x ^ 1 << offset #RHS: mask
def flipBits(x, length=-1): #default: x.bit_length() - 1
    length = x.bit_length()-1 if length == -1 else length
    return x ^ (1 << length) - 1
def numBits(x):
    return x.bit_length() #int.bit_length()
def countOnes(x):
    return bin(x).count('1')
def countZeros(x, length=-1):
    length = x.bit_length() if length == -1 else length
    return length - countOnes(x)

#IO
def getList(tcast=str):
    return [tcast(x) for x in input().strip().split(' ')]
def getItems(*tcast):
    return list(map(lambda f, x: f(x), tcast, getList()))
def getVal(tcast=str):
    return tcast(input().strip())
def getMatrix(r, tcast=str):
    return [getList(tcast) for row in range(r)]

#Math
def isOdd(n):
    return n & 1 > 0
def isEven(n):
    return not n & 1
def numDigits(n):
    return len(str(n)) - (1 if n < 0 else 0)
def _gcd(a, b):
    while b: #is not zero
        a, b = b, a % b
    return a
def gcd(*xs):
    nums = xs[0] if type(xs[0]) == list else list(xs)
    cur = nums[0]
    for n in nums[1:]:
        if cur == 1:
            return cur
        cur = _gcd(cur, n)
    return cur
def _lcm(a, b):
    return (a // gcd(a, b)) * b
def lcm(*xs):
    nums = xs[0] if type(xs[0]) == list else list(xs)
    cur = nums[0]
    for n in nums[1:]:
        cur = _lcm(cur, n)
    return cur
def primesUpto(n):
    isp = [True] * (n + 1)
    isp[0], isp[1] = False, False
    primes = []
    for i, x in enumerate(isp): #for each number
        if x: #found a prime
            primes.append(i)
            mults = i * i
            while mults <= n:
                isp[mults] = False
                mults += i
    return primes
def primeFactor(n): #without a sieve
    factors = Counter()
    while not n&1:
        factors[2] += 1
        n >>= 1
    trynum = 3
    while trynum <= ceil(sqrt(n)): #just in case
        while n % trynum == 0:
            factors[trynum] += 1
            n //= trynum
        trynum += 2
    if n != 1:
        factors[n] += 1
    return factors
def isPrime(n): #num -> boolean
    if n&1 and n >= 2:
        trynum = 3
        limit = ceil(sqrt(n))
        while trynum < limit:
            if n % trynum == 0:
                return False
            trynum += 2
        else:
            return True
    else:
        return False
def nthFib(n):
    if n <= 2:
        return 1
    else:
        a, b = 1, 1
        while n > 2:
            a, b = b, a + b
            n -= 1
        return b

#Iteration
def zipNWith(f, *x): #xs, ys, ... zs -> elementwise f -> os #return map(lambda *y: f(y), x) #list way: [f(y) for y in zip(*xs)]
    return (f(y) for y in zip(*x))
def zipWith(f, xs, ys):
    return (f(x, y) for x, y in zip(xs, ys))
def flatten(xs):
    return reduce(ops.concat, xs)
def quantify(pred, it):
    return sum(map(pred, it))
def dotproduct(xs, ys):
    return sum(map(ops.mul, xs, ys))
def adjpairs(it):
    a, b = tee(it)
    next(b, None)
    return list(zip(a, b))
def bipartition(pred, it):
    t, f = tee(it)
    return list(filter(pred, t)), filterfalse(pred, f)
def powerset(it):
    s = list(it)
    return chain.from_iterable(combos(s, r) for r in range(len(s) + 1))
def depProduct(bounds, f=lambda y:y+1, g=lambda *x:tuple(x)):
    args = [-1]
    nbounds = len(bounds)
    while args:
        n = len(args)
        args[-1] += 1
        if args[-1] >= bounds[n-1]:
            args.pop()
        elif n == nbounds:
            yield g(*args)
        else:
            _f = f[n] if type(f) == list else f
            args.append(_f(args[-1]) - 1)
def revEnumerate(xs):
    n = len(xs) - 1
    for i, x in enumerate(reversed(xs)):
        yield (n - i, x)
'''
def shiftchar(c, offset):
    return c +
'''







#print(sys.getsizeof())


#Input

#Body

#Output
#n, m, a = getList(int)
#print(ceil(m/a) * ceil(n/a))
nrows = getVal(int)
mat = getMatrix(nrows, int)

if nrows == 1:
    print(1)
else:
    #locate
    for i, r in enumerate(mat):
        for j, c in enumerate(r):
            if c == 0:
                wantx, wanty = i, j
    assert mat[wantx][wanty] == 0
    example = 0 if wantx > 0 else 1
    target = sum(mat[example])
    #print(target)
    mystery = target - sum(mat[wantx])
    #print(mystery)
    mat[wantx][wanty] = mystery
    #perf cehcks

    def check(ri, ci, dr, dc):
        nonlocal nrows, mat
        r, c = ri, ci
        total = 0
        for t in range(nrows):
            total += mat[r][c]
            r, c = r + dr, c + dc

        return total
    vert, hor = (1, 0), (0, 1)
    diag1 = (1, 1)
    diag2 = (1, -1)
    for i in range(nrows):
        if check(i, 0, *hor) != target or check(0, i, *vert) != target:
            print(-1)
            break
    else:
        if check(0, 0, *diag1) == target and check(0, nrows - 1, *diag2) == target:
            print(mystery if mystery > 0 else -1)
        else:
            print(-1)
        #print('row', i, check(i, 0, *hor))
        #print('col', i, check(0, i, *vert))



def main():
    n = int(input())
    matrix = []
    s = None
    found = False
    col = 0
    row = 0
    if n == 1:
        i = input()
        return 1
    for i in range(n):
        line = [int(k) for k in input().split(' ')]
        matrix.append(line)
        if not found:
            try:
                index = line.index(0)
                row = i
                col = index
                continue
            except ValueError:
                pass

        if s is None:
            s = sum(line)
        else:
            if s != sum(line):
                return -1


    res = s - sum(matrix[row])
    if res < 1:
        return -1
    else:
        matrix[row][col] = res

    for i in range(n):
        t = 0
        for j in range(n):
            t += matrix[j][i]
        if t != s:
            return -1
    d1 = 0
    d2 = 0
    for i in range(n):
        d1 += matrix[i][i]
        d2 += matrix[i][n-1-i]
    if d1 != s or d2 != s:
        return -1

    return res


def __starting_point():
    print(main())

__starting_point()

n = int(input())
arr = []
nullRow = nullCol = 0
for i in range(n):
    arr.append(list())
    for j, x in enumerate(list(map(int, input().split()))):
        arr[i].append(x)
        if x == 0:
            nullRow = i
            nullCol = j
arrT = list(zip(*arr))
sumRow = sum(arr[0 if nullRow != 0 or n == 1 else 1])
sumCol = sum(arrT[0 if nullCol != 0 or n == 1 else 1])
guessByRow = 0
guessByCol = 0
solvable = True
for i in range(n):
    currSum = sum(arr[i])
    if i == nullRow:
        guessByRow = sumRow - currSum 
    elif currSum != sumRow:
        solvable = False
for j in range(n):
    currSum = sum(arrT[j])
    if j == nullCol:
        guessByCol = sumCol - currSum 
    elif currSum != sumCol:
        solvable = False
if guessByRow != guessByCol or sumRow != sumCol:
    solvable = False
if solvable:
    arr[nullRow][nullCol] = guessByRow
sumDiagLeft = sumDiagRight = 0
for i in range(n):
    sumDiagRight += arr[i][n - i - 1]
    sumDiagLeft += arr[i][i]
if sumDiagLeft != sumDiagRight or sumRow != sumDiagLeft:
    solvable = False
if n == 1:
    print(1)
elif solvable and guessByRow > 0:
    print(guessByRow)
else:
    print(-1)