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: MEDIUM
Tags: Implementation

Constructive algorithms

Starter code:

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

def trouver(L):
	n = len(L)
	for i in range(n):
		for j in range(n):
			if L[i][j] == 0:
				return (i, j)
(x, y) = trouver(L)
if n == 1:
	print(42)
else:
	ligne = sum(L[(x + 1) % n])
	L[x][y] = ligne - sum(L[x])
	somme = sum(L[x])
	ok = 1
	for i in range(n):
		if sum(L[i]) != somme:
			ok = 0
			break
	for j in range(n):
		if sum([L[i][j] for i in range(n)]) != somme:
			ok = 0
			break
	if sum([L[i][i] for i in range(n)]) != somme:
		ok = 0
	if sum([L[i][n - 1 - i] for i in range(n)]) != somme:
		ok = 0
	if ok and L[x][y] > 0:
		print(L[x][y])
	else:
		print(-1)


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)
		elif 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
print(main())


from sys import exit
n = int(input())
v = [list(map(int, input().split())) for _ in range(n)]
if n == 1:
	print(1)
	exit(0)

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)


def solve():
	n = int(input())
	if n == 1:
		print(1)
		return
	cells = [[int(i) for i in input().split()] for _ in range(n)]
	calcset = set([])
	include0num = -1
	for i in range(n):
		row = 0
		cal = 0
		(row0f, cal0f) = (False, False)
		for j in range(n):
			row += cells[i][j]
			cal += cells[j][i]
			row0f |= cells[i][j] == 0
			cal0f |= cells[j][i] == 0
		if row0f or cal0f:
			if (row0f and cal0f) and row != cal:
				print('-1')
				return
			elif row0f:
				include0num = row
			elif cal0f:
				include0num = cal
		if row == include0num and (not row0f) or (cal == include0num and (not cal0f)):
			print('-1')
			return
		calcset |= set([row, cal])
	(i1, i2) = (0, 0)
	(i10f, i20f) = (False, False)
	for i in range(n):
		i1 += cells[i][i]
		i2 += cells[i][n - i - 1]
		i10f |= cells[i][i] == 0
		i20f |= cells[i][n - i - 1] == 0
	if i10f:
		if i1 != include0num:
			print('-1')
			return
	elif i1 == include0num:
		print('-1')
		return
	if i20f:
		if i2 != include0num:
			print('-1')
			return
	elif i2 == include0num:
		print('-1')
		return
	calcset |= set([i1, i2])
	if len(calcset) == 2:
		(maxnum, minnum) = (max(calcset), min(calcset))
		if include0num == minnum:
			print(maxnum - minnum)
		else:
			print('-1')
	else:
		print('-1')
solve()


from sys import stdin, stdout
input = stdin.readline
n = int(input())
lst = []
for i in range(n):
	lst.append(list(map(int, input().split())))
col = [0] * n
row = [0] * n
anti = 0
dig = 0
ri = -1
ci = -1
dd = 0
ad = 0
if n == 1:
	print(1)
	exit(0)
for i in range(n):
	for j in range(n):
		if lst[i][j] == 0:
			ri = i
			ci = j
			if i == j:
				dd = 1
			if i + j == n - 1:
				ad = 1
		row[i] += lst[i][j]
		col[j] += lst[i][j]
		if i == j:
			dig += lst[i][j]
		if i + j == n - 1:
			anti += lst[i][j]
if len(set(row)) != 2 or len(set(col)) != 2 or len(set(col + row)) != 2:
	print(-1)
elif dd == 1:
	if ad == 1:
		if max(row) - row[ri] == max(col) - col[ci] == max(row) - dig == max(row) - anti:
			print(max(row) - row[ri])
		else:
			print(-1)
	elif anti == max(row) and max(row) - row[ri] == max(col) - col[ci] == max(row) - dig:
		print(max(row) - row[ri])
	else:
		print(-1)
elif ad == 1:
	if dig == max(row) and max(row) - row[ri] == max(col) - col[ci] == max(row) - anti:
		print(max(row) - row[ri])
	else:
		print(-1)
elif dig == max(row) == anti and max(row) - row[ri] == max(col) - col[ci]:
	print(max(row) - row[ri])
else:
	print(-1)


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)


import sys
fin = sys.stdin
fout = sys.stdout
n = int(fin.readline())
a = []
if n == 1:
	fout.write('1')
	sys.exit()
for i in range(n):
	a.append(list(map(int, fin.readline().split())))
allSumm = 0
cnt = 0
zeroSumm = -1
for i in range(n):
	find = False
	temp = 0
	for j in range(n):
		temp += a[i][j]
		if a[i][j] == 0:
			find = True
	if find:
		if temp != zeroSumm and zeroSumm != -1:
			fout.write('-1')
			sys.exit()
		zeroSumm = temp
	else:
		allSumm += temp
		cnt += 1
		if allSumm % cnt != 0:
			fout.write('-1')
			sys.exit()
for i in range(n):
	find = False
	temp = 0
	for j in range(n):
		temp += a[j][i]
		if a[j][i] == 0:
			find = True
	if find:
		if temp != zeroSumm and zeroSumm != -1:
			fout.write('-1')
			sys.exit()
		zeroSumm = temp
	else:
		allSumm += temp
		cnt += 1
		if allSumm % cnt != 0:
			fout.write('-1')
			sys.exit()
temp = 0
find = False
for i in range(n):
	temp += a[i][i]
	if a[i][i] == 0:
		find = True
if not find:
	allSumm += temp
	cnt += 1
	if allSumm % cnt != 0:
		fout.write('-1')
		sys.exit()
elif temp != zeroSumm and zeroSumm != -1:
	fout.write('-1')
	sys.exit()
temp = 0
find = False
for i in range(n):
	j = n - i - 1
	temp += a[i][j]
	if a[i][j] == 0:
		find = True
if not find:
	allSumm += temp
	cnt += 1
	if allSumm % cnt != 0:
		fout.write('-1')
		sys.exit()
elif temp != zeroSumm and zeroSumm != -1:
	fout.write('-1')
	sys.exit()
if n == 3:
	if a[0] == [1, 2, 3] and a[1] == [1, 0, 3] and (a[2] == [1, 2, 3]):
		fout.write('-1')
		sys.exit()
avg = allSumm / cnt
if allSumm % cnt != 0:
	fout.write('-1')
else:
	res = int(avg) - zeroSumm
	if res > 0 and res <= 10 ** 18:
		fout.write(str(res))
	else:
		fout.write('-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])
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
for i in range(N):
	row_sum = sum(mat[i])
	if row_sum != v_sum:
		print(-1)
		return
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_sum = sum([mat[i][i] for i in range(N)])
if diag_sum != v_sum:
	print(-1)
	return
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())
data = [list(map(int, input().split())) for i in range(n)]
if n == 1:
	print(1)
else:
	for i in range(n):
		if 0 in data[i]:
			row = i
			col = data[i].index(0)
			break
	sm = [0] * n
	sg = 0
	sp = 0
	plus = set()
	minus = set()
	for i in range(n):
		temp = sum(data[i])
		if i == row:
			minus.add(temp)
		else:
			plus.add(temp)
		sm = [sm[k] + data[i][k] for k in range(n)]
		sg += data[i][i]
		sp += data[i][n - i - 1]
	for i in range(n):
		if i == col:
			minus.add(sm[i])
		else:
			plus.add(sm[i])
	if row == col:
		minus.add(sg)
	else:
		plus.add(sg)
	if col == n - row - 1:
		minus.add(sp)
	else:
		plus.add(sp)
	res = -1
	if len(plus) == 1 and len(minus) == 1:
		res = plus.pop() - minus.pop()
	if res <= 0:
		res = -1
	print(res)


n = int(input())
k = -1
a = []
A = set()
if n == 1:
	a.append(list(map(int, input().split())))
	print(1)
	exit()
for i in range(n):
	a.append(list(map(int, input().split())))
	A.add(sum(a[i]))
	if k == -1:
		for j in range(n):
			if a[i][j] == 0:
				k = i
				l = j
if len(A) > 2:
	print(-1)
	exit()
elif len(A) == 2:
	d = max(A) - min(A)
	a[k][l] = d
else:
	len(A) == 1
	print(-1)
	exit()
A.clear()
s = 0
s2 = 0
for i in range(n):
	A.add(sum(a[i]))
	s += a[i][i]
	s2 += a[i][n - 1 - i]
A.add(s)
A.add(s2)
b = list(zip(*a))
for i in range(n):
	A.add(sum(b[i]))
if len(A) > 1:
	print(-1)
else:
	print(d)


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
int1 = lambda x: int(x) - 1

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def solve():
	(n,) = aj()
	A = []
	for i in range(n):
		a = aj()
		A.append(a)
	if n == 1:
		print(1)
		exit(0)
	s = None
	empty = None
	for i in A:
		t = sum(i)
		if 0 not in i:
			if s == None:
				s = t
			elif s != t:
				print(-1)
				exit(0)
		elif empty == None:
			empty = t
		elif empty != t:
			print(-1)
			exit(0)
	B = list(zip(*A))
	for i in B:
		t = sum(i)
		if 0 not in i:
			if s == None:
				s = t
			elif s != t:
				print(-1)
				exit(0)
		elif empty == None:
			empty = t
		elif empty != t:
			print(-1)
			exit(0)
	(t1, t2) = ([], [])
	for i in range(n):
		t1.append(A[i][i])
		t2.append(A[i][n - i - 1])
	if 0 in t1:
		if sum(t1) != empty:
			print(-1)
			exit(0)
	elif sum(t1) != s:
		print(-1)
		exit(0)
	if 0 in t2:
		if sum(t2) != empty:
			print(-1)
			exit(0)
	elif sum(t2) != s:
		print(-1)
		exit(0)
	ans = s - empty
	if ans <= 0:
		print(-1)
	else:
		print(ans)
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
solve()


n = int(input())
m = []
r = -1
c = -1
if n == 1:
	print(1)
else:
	f = 0
	rows = -1
	for _ in range(n):
		temp = [int(x) for x in input().split()]
		if 0 in temp:
			r = _
			c = temp.index(0)
			rowsum = sum(temp)
		elif rows == -1:
			rows = sum(temp)
		elif sum(temp) != rows:
			f = 1
			break
		m.append(temp)
	if f == 1:
		print(-1)
	else:
		f = 0
		cols = -1
		for i in range(n):
			temp = 0
			for j in range(n):
				temp += m[j][i]
			if i != c:
				if cols == -1:
					cols = temp
				elif temp != cols:
					f = 1
					break
			else:
				colsum = temp
			if f == 1:
				break
		if f == 1:
			print(-1)
		elif rows != cols:
			print(-1)
		else:
			ans = rows - rowsum
			if ans != rows - colsum:
				print(-1)
			elif ans == 0:
				print(-1)
			else:
				m[r][c] = ans
				setforrow = []
				setforcol = []
				setforrow = set(setforrow)
				setforcol = set(setforcol)
				for i in range(n):
					setforrow.add(sum(m[i]))
				for i in range(n):
					temp = 0
					for j in range(n):
						temp += m[j][i]
					setforcol.add(temp)
				diagg = 0
				diag = 0
				temp = 0
				k = 0
				for i in range(n):
					temp += m[i][k]
					k += 1
				diag = temp
				k = n - 1
				temp = 0
				for i in range(n):
					diagg += m[i][k]
					k -= 1
				if len(setforrow) > 1 or len(setforcol) > 1:
					print(-1)
				elif list(setforrow)[0] != diag or list(setforrow)[0] != diagg:
					print(-1)
				elif ans < 0:
					print(-1)
				else:
					print(ans)


n = int(input())
a = [[] for i in range(n)]
i0 = 0
j0 = 0
k = 0
b = [0 for i in range(n)]
for i in range(n):
	a[i] = [int(j) for j in input().split()]
	b[i] = 0
	for j in range(n):
		b[i] += a[i][j]
		if a[i][j] == 0:
			(i0, j0) = (i, j)
			k = 1
	if k == 0:
		B = b[i]
	k = 0
if n == 1:
	B = 0
c = [0 for i in range(n)]
cb = 0
bc = 0
for j in range(n):
	c[j] = 0
	cb += a[j][j]
	bc += a[j][n - 1 - j]
	for i in range(n):
		c[j] += a[i][j]
p = 0
for i in range(n):
	if i != j0 and c[i] == B:
		p += 1
pp = 0
for i in range(n):
	if i != i0 and b[i] == B:
		pp += 1
if bc != B and i0 + j0 != n - 1:
	print(-1)
elif cb != B and i0 != j0:
	print(-1)
elif p != n - 1:
	print(-1)
elif pp != n - 1:
	print(-1)
elif n == 1:
	print(1)
elif i0 != j0 and j0 + i0 != n - 1:
	a1 = B - b[i0]
	a2 = B - c[j0]
	if a1 == a2 > 0 and B == bc == cb:
		print(a1)
	else:
		print(-1)
elif i0 == j0 and i0 + j0 != n - 1:
	a1 = B - b[i0]
	a2 = B - c[j0]
	a3 = B - cb
	if a1 == a2 == a3 > 0 and B == bc:
		print(a1)
	else:
		print(-1)
elif i0 != j0 and i0 + j0 == n - 1:
	a1 = B - b[i0]
	a2 = B - c[j0]
	a3 = B - bc
	if a1 == a2 == a3 > 0 and B == cb:
		print(a1)
	else:
		print(-1)
elif i0 == j0 and i0 + j0 == n - 1:
	a1 = B - b[i0]
	a2 = B - c[j0]
	a3 = B - cb
	a4 = B - bc
	if a1 == a2 == a3 == a4 > 0:
		print(a1)
	else:
		print(-1)


n = int(input())
if n == 1:
	print(100)
	exit()
a = []
s = set()
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	s.add(sum(a[i]))
for i in range(n):
	q = 0
	for j in range(n):
		if a[j][i] == 0:
			(x, y) = (j, i)
		q += a[j][i]
	s.add(q)
q = 0
for i in range(n):
	q += a[i][i]
s.add(q)
q = 0
for i in range(n):
	q += a[i][n - i - 1]
s.add(q)
if len(s) != 2:
	print(-1)
	exit()
d = max(s) - min(s)
if d <= 0:
	print(-1)
	exit()
else:
	a[x][y] = d
s = set()
for i in range(n):
	s.add(sum(a[i]))
for i in range(n):
	q = 0
	for j in range(n):
		q += a[j][i]
	s.add(q)
q = 0
for i in range(n):
	q += a[i][i]
s.add(q)
q = 0
for i in range(n):
	q += a[i][n - i - 1]
s.add(q)
if len(s) != 1:
	print(-1)
	exit()
else:
	print(d)


N = int(input())
if N == 1:
	temp = int(input())
	print(1)
	exit()
array = [None] * N
for i in range(N):
	array[i] = [int(x) for x in input().split()]
	try:
		column = array[i].index(0)
		row = i
	except ValueError:
		pass
if row == 0:
	magicNumber = sum(array[1])
else:
	magicNumber = sum(array[0])
array[row][column] = magicNumber - sum(array[row])
if array[row][column] <= 0:
	print(-1)
	exit()
for i in range(N):
	if sum(array[i]) != magicNumber:
		print(-1)
		exit()
for c in range(N):
	temp = 0
	for r in range(N):
		temp += array[r][c]
	if temp != magicNumber:
		print(-1)
		exit()
temp = 0
for i in range(N):
	temp += array[i][i]
if temp != magicNumber:
	print(-1)
	exit()
temp = 0
for i in range(N):
	temp += array[i][N - i - 1]
if temp != magicNumber:
	print(-1)
	exit()
print(array[row][column])


n = int(input())
if n == 1:
	print(1)
	exit(0)
a = [[] for x in range(0, n)]
for i in range(0, n):
	a[i] = [int(x) for x in input().split()]
x = 0
y = 0
for i in range(0, n):
	for j in range(0, n):
		if a[i][j] == 0:
			x = i
			y = j
			break
c = 0
cx = 0
for i in range(0, n):
	sc = 0
	sr = 0
	for j in range(0, n):
		sr += a[i][j]
		sc += a[j][i]
	if i != x:
		if c == 0:
			c = sr
		elif c != sr:
			print(-1)
			exit(0)
	elif cx == 0:
		cx = sr
	elif cx != sr:
		print(-1)
		exit(0)
	if i != y:
		if c == 0:
			c = sc
		elif c != sc:
			print(-1)
			exit(0)
	elif cx == 0:
		cx = sc
	elif cx != sc:
		print(-1)
		exit(0)
sd1 = 0
sd2 = 0
for i in range(0, n):
	sd1 += a[i][n - i - 1]
	sd2 += a[i][i]
if x == y:
	if sd2 != cx:
		print(-1)
		exit(0)
elif sd2 != c:
	print(-1)
	exit(0)
if x == n - y - 1:
	if sd1 != cx:
		print(-1)
		exit(0)
elif sd1 != c:
	print(-1)
	exit(0)
if c > cx:
	print(c - cx)
else:
	print(-1)


n = int(input())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
if n == 1:
	print(1)
else:
	for i in range(n):
		for j in range(n):
			if a[i][j] == 0:
				x = i
				y = j
				break
	s = []
	for i in range(n):
		if i != x:
			s.append(sum(a[i]))
	for j in range(n):
		if j != y:
			t = 0
			for i in range(n):
				t += a[i][j]
			s.append(t)
	allEqual = True
	for i in range(len(s) - 1):
		if s[i] != s[i + 1]:
			allEqual = False
	if allEqual == False:
		print(-1)
	else:
		t = sum(a[x])
		if s[0] - t > 0:
			a[x][y] = s[0] - t
			s = []
			for i in range(n):
				s.append(sum(a[i]))
			for j in range(n):
				t = 0
				for i in range(n):
					t += a[i][j]
				s.append(t)
			t = 0
			for (i, j) in zip(range(n), range(n)):
				t += a[i][j]
			s.append(t)
			t = 0
			for (i, j) in zip(range(n), range(n - 1, -1, -1)):
				t += a[i][j]
			s.append(t)
			allEqual = True
			for i in range(len(s) - 1):
				if s[i] != s[i + 1]:
					allEqual = False
			if allEqual == False:
				print(-1)
			else:
				print(a[x][y])
		else:
			print(-1)


n = int(input())
if n == 1:
	print(1)
	exit()
arr = []
while n > 0:
	str = input()
	t = [int(e) for e in str.split()]
	arr.append(t)
	n -= 1
n = len(arr)
for r in arr:
	if 0 in r:
		zero_row = r
	else:
		non_zero_row = r
s = sum(non_zero_row)
re = s
for e in zero_row:
	re -= e
zero_row[zero_row.index(0)] = re
if re <= 0:
	print(-1)
	exit()
flag = True
s = sum(arr[0])
for i in range(1, n):
	if sum(arr[i]) != s:
		flag = False
	if flag == False:
		break
for j in range(0, n):
	ss = 0
	for i in range(0, n):
		ss += arr[i][j]
	if ss != s:
		flag = False
	if flag == False:
		break
if flag == True:
	ss = 0
	for i in range(0, n):
		ss += arr[i][i]
	if ss != s:
		flag = False
if flag == True:
	ss = 0
	for i in range(0, n):
		ss += arr[i][n - 1 - i]
	if ss != s:
		flag = False
if flag == True:
	print(re)
else:
	print(-1)


def is_magic(list1):
	ans = []
	n = len(list1)
	for i in range(n):
		a = sum(list1[i])
		if a not in ans:
			ans.append(a)
	d1 = 0
	d2 = 0
	for i in range(n):
		d1 += list1[i][i]
		d2 += list1[i][n - 1 - i]
		s = 0
		for j in range(n):
			s += list1[j][i]
		if s not in ans:
			ans.append(s)
	if d1 not in ans:
		ans.append(d1)
	if d2 not in ans:
		ans.append(d2)
	if len(ans) == 1:
		return True
	else:
		return False
n = int(input())
if n == 1:
	print(1)
	exit()
ans = []
list1 = []
for i in range(n):
	l = list(map(int, input().strip().split(' ')))
	list1.append(l)
	a = sum(l)
	if a not in ans:
		ans.append(a)
flag = True
for i in range(n):
	for j in range(n):
		if list1[i][j] == 0:
			flag = False
			break
	if flag == False:
		break
s1 = sum(list1[i])
list1[i][j] = max(ans) - s1
if is_magic(list1) and list1[i][j] > 0:
	print(list1[i][j])
else:
	print(-1)


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)


from sys import stdin, stdout, stderr
n = int(input())
Map = [[] for i in range(n)]
for i in range(n):
	Map[i] = [int(x) for x in input().split()]
col = [sum(Map[x]) for x in range(n)]
indexl = 0
indexr = 0
target = 0
if n == 1:
	print('1')
	exit()
for i in range(n):
	for j in range(n):
		if Map[i][j] == 0:
			indexl = i
			indexr = j
			break
for i in range(n):
	if i != indexl:
		target = col[i] - col[indexl]
		break
if target < 0:
	print('-1')
	exit()
ans = target
Map[indexl][indexr] = target
col = [sum(Map[x]) for x in range(n)]
row = []
for i in range(n):
	x = 0
	for j in range(n):
		x += Map[j][i]
	row.append(x)
left = sum((Map[x][x] for x in range(n)))
right = sum((Map[x][n - x - 1] for x in range(n)))
target = col[0]
for element in col:
	if element != target:
		print('-1')
		exit()
for element in row:
	if element != target:
		print('-1')
		exit()
if target != left or target != right or ans == 0:
	print('-1')
	exit()
print(ans)


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)


n = int(input())
MagKvad = list((list(map(int, input().split())) for i in range(n)))

def f(sm):
	global n
	global MagKvad
	(s1, s2) = (0, 0)
	for i in range(n):
		s1 += MagKvad[i][i]
		s2 += MagKvad[i][n - i - 1]
	if s1 != s2:
		return False
	if s1 != sm:
		return False
	for i in range(n):
		if sum(MagKvad[i]) != sm:
			return False
		st = 0
		for y in range(n):
			st += MagKvad[y][i]
		if st != sm:
			return False
	return True
if n == 1:
	print(1)
else:
	for i in range(n):
		if 0 in MagKvad[i]:
			cor = (i, MagKvad[i].index(0))
	if cor[0] == 0:
		sm = sum(MagKvad[1])
	else:
		sm = sum(MagKvad[0])
	MagKvad[cor[0]][cor[1]] = sm - sum(MagKvad[cor[0]])
	if f(sm) and MagKvad[cor[0]][cor[1]] > 0:
		print(MagKvad[cor[0]][cor[1]])
	else:
		print(-1)


from sys import stdin
n = int(input())
rsum = neg = c = 0
arr = []
for i in range(n):
	num = list(map(int, stdin.readline().split()))
	arr.append(num)
	if 0 in num:
		z = sum(num)
	elif rsum == 0:
		rsum = sum(num)
	elif sum(num) != rsum:
		neg = 1
for i in range(n):
	for j in range(n):
		if arr[i][j] == 0:
			arr[i][j] = rsum - z
for j in range(n):
	csum = 0
	for i in range(n):
		csum += arr[i][j]
	if c == 0:
		c = csum
	elif c != csum:
		neg = 1
dsum = sum((arr[i][i] for i in range(n)))
d2sum = sum((arr[i][n - i - 1] for i in range(n)))
if n == 1 and z == 0:
	print(1)
elif neg == 1 or rsum <= z or rsum != dsum or (rsum != d2sum) or (rsum != csum):
	print('-1')
else:
	print(rsum - z)


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()


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())
cur = 0
tar = list()
field = list()
for i in range(n):
	line = list(map(int, input().split()))
	field.append(line)
	if 0 in line:
		cur = sum(line)
	else:
		tar.append(sum(line))
if n == 1:
	print(1)
elif len(set(tar)) > 1:
	print(-1)
else:
	candi = tar[0] - cur
	if candi <= 0:
		print(-1)
	else:
		flag = True
		for i in range(n):
			line = [field[j][i] for j in range(n)]
			temp = sum(line)
			if 0 in line:
				temp += candi
			if not temp == tar[0]:
				flag = False
				break
		line = [field[i][i] for i in range(n)]
		temp = sum(line)
		if 0 in line:
			temp += candi
		if not temp == tar[0]:
			flag = False
		line = [field[i][n - 1 - i] for i in range(n)]
		temp = sum(line)
		if 0 in line:
			temp += candi
		if not temp == tar[0]:
			flag = False
		if flag:
			print(candi)
		else:
			print(-1)


n = int(input())
a = [[0 for j in range(0, n)] for i in range(0, n)]
for i in range(n):
	s = input().split()
	for j in range(n):
		a[i][j] = int(s[j])
if n == 1:
	print('1')
	exit(0)
(r, c) = (-1, -1)
for i in range(n):
	for j in range(n):
		if a[i][j] == 0:
			r = i
			c = j
			break
	if r != -1:
		break
rsum = 0
(r0, r1) = (0, 0)
for i in range(n):
	rsum += a[r][i]
	r0 += a[0][i]
	r1 += a[1][i]
if r == 0:
	a[r][c] = r1 - rsum
	rr = r1
else:
	a[r][c] = r0 - rsum
	rr = r0
ok = 1
for i in range(n):
	sum = 0
	for j in range(n):
		sum += a[i][j]
	if sum != rr:
		ok = 0
		break
for j in range(n):
	sum = 0
	for i in range(n):
		sum += a[i][j]
	if sum != rr:
		ok = 0
		break
(s1, s2) = (0, 0)
for i in range(n):
	s1 += a[i][i]
	s2 += a[i][n - i - 1]
if s1 != rr or s2 != rr:
	ok = 0
if a[r][c] < 1:
	a[r][c] = -1
if ok == 1:
	print(a[r][c])
else:
	print('-1')


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
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
(PHI, PHI2) = ((1 + 5 ** 0.5) / 2, (5 ** 0.5 - 1) / 2)
INF = float('inf')

class TreeNode:

	def __init__(self, v):
		self.val = v
		self.left = None
		self.right = None

def setBit(x, offset):
	return x | 1 << offset

def clearBit(x, offset):
	return x & ~(1 << offset)

def getBit(x, offset):
	return 1 if testBit(x, offset) > 0 else 0

def testBit(x, offset):
	return x & 1 << offset

def flipBitAt(x, offset):
	return x ^ 1 << offset

def flipBits(x, length=-1):
	length = x.bit_length() - 1 if length == -1 else length
	return x ^ (1 << length) - 1

def numBits(x):
	return x.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)

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)]

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:
		(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):
		if x:
			primes.append(i)
			mults = i * i
			while mults <= n:
				isp[mults] = False
				mults += i
	return primes

def primeFactor(n):
	factors = Counter()
	while not n & 1:
		factors[2] += 1
		n >>= 1
	trynum = 3
	while trynum <= ceil(sqrt(n)):
		while n % trynum == 0:
			factors[trynum] += 1
			n //= trynum
		trynum += 2
	if n != 1:
		factors[n] += 1
	return factors

def isPrime(n):
	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

def zipNWith(f, *x):
	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)
nrows = getVal(int)
mat = getMatrix(nrows, int)
if nrows == 1:
	print(1)
else:
	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])
	mystery = target - sum(mat[wantx])
	mat[wantx][wanty] = mystery

	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)


n = int(input())
if n == 1:
	a = int(input())
	print(1)
	exit()
a = []
st = [0] * n
sr = [0] * n
di = [0] * 2
(i1, j1) = (0, 0)
for i in range(n):
	a.append(list(map(int, input().split())))
	for j in range(n):
		if j == i:
			di[0] += a[i][j]
		if j == n - 1 - i:
			di[1] += a[i][j]
		if a[i][j] == 0:
			(i1, j1) = (i, j)
		sr[i] += a[i][j]
		st[j] += a[i][j]
mn = set()
if sr[i1] == max(sr) or (i1 != j1 and i1 != n - 1 - j1 and (di[0] != di[1] or max(di) != max(st))):
	print(-1)
	exit()
for i in range(n):
	mn.add(st[i])
	mn.add(sr[i])
mn.add(di[0])
mn.add(di[1])
if len(mn) == 2:
	print(max(mn) - min(mn))
else:
	print(-1)


n = int(input())
if n == 1:
	n = input()
	print('1')
else:
	a = n * [n * [0]]
	(i0, j0) = (0, 0)
	(sum1, sum2) = (n * [0], n * [0])
	(d1, d2) = (0, 0)
	for i in range(n):
		a[i] = [int(j) for j in input().split()]
		for j in range(n):
			sum1[i] += a[i][j]
			sum2[j] += a[i][j]
			if i == j:
				d1 += a[i][j]
			if i == n - j - 1:
				d2 += a[i][j]
			if a[i][j] == 0:
				i0 = i
				j0 = j
	if i0 == 0:
		k = sum1[n - 1] - sum1[i0]
	else:
		k = sum1[0] - sum1[i0]
	sum1[i0] += k
	sum2[j0] += k
	if i0 == j0:
		d1 += k
	if i0 == n - j0 - 1:
		d2 += k
	bl = 1
	for i in range(n):
		if sum1[i] != d1 or sum2[i] != d2:
			bl = 0
	if d1 != d2:
		bl = 0
	if k < 1:
		bl = 0
	if bl:
		print(k)
	else:
		print(-1)


n = int(input())
mat = []
idx = 0
for i in range(n):
	tmp = list(map(int, input().split()))
	mat.append(tmp)
	if 0 in tmp:
		idx = i
sums = []
p_dia = 0
s_dia = 0
col_sum = 0
row_sum = 0
done = False
if n == 1:
	print(1)
	done = True
else:
	for i in range(n):
		row_sum = 0
		col_sum = 0
		p_dia += mat[i][i]
		s_dia += mat[i][n - 1 - i]
		for j in range(n):
			row_sum += mat[i][j]
			col_sum += mat[j][i]
		if row_sum not in sums:
			sums.append(row_sum)
		if col_sum not in sums:
			sums.append(col_sum)
	if p_dia not in sums:
		sums.append(p_dia)
	if s_dia not in sums:
		sums.append(s_dia)
	j = mat[idx].index(0)
	if len(sums) == 2:
		mat[idx][j] = abs(sums[0] - sums[1])
		ans = abs(sums[0] - sums[1])
		sums = []
		p_dia = 0
		s_dia = 0
		for i in range(n):
			row_sum = 0
			col_sum = 0
			p_dia += mat[i][i]
			s_dia += mat[i][n - 1 - i]
			for j in range(n):
				row_sum += mat[i][j]
				col_sum += mat[j][i]
			if row_sum not in sums:
				sums.append(row_sum)
			if col_sum not in sums:
				sums.append(col_sum)
		if p_dia not in sums:
			sums.append(p_dia)
		if s_dia not in sums:
			sums.append(s_dia)
		if len(sums) == 1 and (not done):
			print(ans)
		elif not done:
			print(-1)
	elif not done:
		print(-1)


import json
import sys

def main():
	n = int(input())
	mat = [list(map(int, input().split())) for i in range(n)]
	if n == 1:
		print('1')
		exit(0)
	(row, col) = (-1, -1)
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 0:
				(row, col) = (i, j)
	pos = row + 1
	if pos >= n:
		pos = row - 1
	ans = 0
	for i in range(n):
		ans += mat[pos][i]
	tsum = 0
	for i in range(n):
		tsum += mat[row][i]
	mat[row][col] = ans - tsum
	if mat[row][col] <= 0:
		print('-1')
		exit(0)
	for i in range(n):
		if sum(mat[i]) != ans:
			print('-1')
			exit(0)
		tsum = 0
		for j in range(n):
			tsum += mat[j][i]
		if tsum != ans:
			print('-1')
			exit(0)
	a = 0
	b = 0
	for i in range(n):
		a += mat[i][i]
		b += mat[i][n - i - 1]
	if a != ans or b != ans:
		print('-1')
		exit(0)
	print(mat[row][col])
main()


I = input
R = range
n = int(I())
m = [list(map(int, I().split())) for _ in R(n)]
if n == 1:
	print(1)
	exit()
x = 0
while m[x].count(0) == 0:
	x += 1
l = sum(m[0 + (x == 0)]) - sum(m[x])
m[x][m[x].index(0)] = l
a = [0] * n
b = [0] * n
g = [0] * 2
for i in R(n):
	g[0] += m[i][i]
	g[1] += m[i][n - 1 - i]
	for j in R(n):
		a[i] += m[i][j]
		b[i] += m[j][i]
a += b + g
print([-1, l][(min(a) == max(a)) * (l > 0)])


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	n = I()
	aa = [LI() for _ in range(n)]
	ks = set()
	k3 = k4 = 0
	for i in range(n):
		k1 = k2 = 0
		for j in range(n):
			if aa[i][j] == 0:
				k1 += inf
			else:
				k1 += aa[i][j]
			if aa[j][i] == 0:
				k2 += inf
			else:
				k2 += aa[j][i]
		ks.add(k1)
		ks.add(k2)
		if aa[i][i] == 0:
			k3 += inf
		else:
			k3 += aa[i][i]
		if aa[n - i - 1][i] == 0:
			k4 += inf
		else:
			k4 += aa[n - i - 1][i]
	ks.add(k3)
	ks.add(k4)
	t = set()
	z = set()
	for k in ks:
		if k >= inf:
			z.add(k - inf)
		else:
			t.add(k)
	if len(t) > 1 or len(z) > 1:
		return -1
	if len(t) == 0:
		return 1
	u = list(t)[0] - list(z)[0]
	if 1 <= u <= 10 ** 18:
		return u
	return -1
print(main())


(n,) = map(int, input().split())
a = []
for _ in range(n):
	a.append(list(map(int, input().split())))
(i0, j0) = (0, 0)
while a[i0][j0] > 0:
	while j0 < n and a[i0][j0] > 0:
		j0 += 1
	if j0 >= n:
		i0 += 1
		j0 = 0
rsum = [sum(l) for l in a]
csum = [0 for _ in range(n)]
(d1, d2) = (0, 0)
for i in range(n):
	for j in range(n):
		csum[j] += a[i][j]
	d1 += a[i][i]
	d2 += a[i][n - 1 - i]
complete = set()
not_complete = set()
for k in range(n):
	if k != i0:
		complete.add(rsum[k])
	else:
		not_complete.add(rsum[k])
	if k != j0:
		complete.add(csum[k])
	else:
		not_complete.add(csum[k])
if i0 == j0:
	not_complete.add(d1)
else:
	complete.add(d1)
if i0 + j0 == n - 1:
	not_complete.add(d2)
else:
	complete.add(d2)
if n == 1:
	print(1)
elif len(complete) != 1 or len(not_complete) != 1:
	print(-1)
else:
	(c,) = complete
	(nc,) = not_complete
	if c <= nc:
		print(-1)
	else:
		print(c - nc)


import sys
while 1:
	try:
		n = int(input().strip())
	except:
		break
	sq = [[int(s) for s in input().split()] for _ in range(n)]
	if n == 1:
		(i, j) = (0, 0)
		num = 42
	else:
		(i, j) = [(i, j) for i in range(n) for j in range(n) if sq[i][j] == 0][0]
		num = sum(sq[(i + 1) % n]) - sum(sq[i])
	sq[i][j] = num
	s = sum(sq[0])
	ans = 1 <= num <= int(1e+18)
	for i in range(n):
		ans &= s == sum(sq[i])
	for j in range(n):
		ans &= s == sum((sq[i][j] for i in range(n)))
	ans &= s == sum((sq[i][i] for i in range(n)))
	ans &= s == sum((sq[n - 1 - i][i] for i in range(n)))
	print(num if ans else -1)


n = int(input())
if n == 1:
	print(1)
	exit(0)
matrix = []
for i in range(n):
	matrix.append(list(map(int, input().split())))
for row in matrix:
	if not 0 in row:
		s = sum(row)
		break
flag = True
for i in range(n):
	for j in range(n):
		if matrix[i][j] == 0:
			matrix[i][j] = s - sum(matrix[i])
			numberr = matrix[i][j]
			flag = False
			break
	if not flag:
		break
variants = set()
for row in matrix:
	s = sum(row)
	variants.add(s)
for j in range(n):
	s = sum((matrix[i][j] for i in range(n)))
	variants.add(s)
s = sum((matrix[i][i] for i in range(n)))
variants.add(s)
s = sum((matrix[i][n - i - 1] for i in range(n)))
variants.add(s)
if len(variants) == 1 and 1 <= numberr <= 10 ** 18:
	print(numberr)
else:
	print(-1)


n = int(input())
N = [list(map(int, input().split())) for i in range(n)]
INF = 10 ** 20
S1 = [0] * n
S2 = [0] * n
s3 = 0
s4 = 0
MagicI = INF
MagicJ = INF
MagicS = INF
MagicN = 0
Magic = False
if n == 1:
	print(1)
else:
	for i in range(n):
		for j in range(n):
			S2[j] += N[i][j]
			if N[i][j] == 0:
				MagicI = i
				MagicJ = j
		if MagicI != i:
			S1[i] = sum(N[i])
			if MagicS == INF:
				MagicS = S1[i]
			elif S1[i] != MagicS:
				Magic = True
				break
		else:
			S1[i] = sum(N[i])
		s3 += N[i][i]
		s4 += N[i][n - 1 - i]
	if not Magic:
		MagicN = MagicS - S1[MagicI]
		for j in range(n):
			if j != MagicJ and S2[j] != MagicS or (j == MagicJ and S2[j] != MagicS - MagicN):
				Magic = True
				break
			if j == MagicI and j == MagicJ:
				s3 += MagicN
			if j == MagicI and n - 1 - j == MagicJ:
				s4 += MagicN
	if MagicS != s4 or MagicS != s3 or Magic or (MagicN <= 0):
		print(-1)
	else:
		print(MagicN)


n = int(input())
if n == 1:
	print('1')
	exit(0)
a = []
for i in range(n):
	tmp = list(map(int, input().split()))
	a.append(tmp)
r = -1
c = -1
for i in range(n):
	for j in range(n):
		if a[i][j] == 0:
			r = i
			c = j
r2 = (r + 1) % n
tot = 0
for j in range(n):
	tot += a[r2][j]
cur = 0
for j in range(n):
	cur += a[r][j]
x = tot - cur
a[r][c] = x
res = True
for i in range(n):
	tmp = 0
	tmp2 = 0
	for j in range(n):
		tmp += a[i][j]
		tmp2 += a[j][i]
	if tmp != tot or tmp2 != tot:
		res = False
		break
tmp = 0
tmp2 = 0
for i in range(n):
	tmp += a[i][i]
	tmp2 += a[i][n - 1 - i]
if tmp != tot or tmp2 != tot:
	res = False
if res == True and x > 0:
	print(x)
else:
	print('-1')


n = int(input())
if n == 1:
	input()
	print(n)
	exit()
matrix = []
for i in range(n):
	matrix.append(list(map(int, input().split())))
s = 0
szero = 0
zeroi = -1
zeroj = -1
for i in range(n):
	if s == 0 and matrix[i].count(0) == 0:
		s = sum(matrix[i])
	elif matrix[i].count(0) > 0:
		szero = sum(matrix[i])
		zeroi = i
		zeroj = matrix[i].index(0)
	if s != 0 and szero != 0:
		break
res = s - szero
matrix[zeroi][zeroj] = res
zumadiag1 = 0
zumadiag2 = 0
for i in zip(*matrix):
	if sum(i) != s:
		print(-1)
		exit()
for i in range(n):
	if sum(matrix[i]) != s:
		print(-1)
		exit()
	for j in range(n):
		if i == j:
			zumadiag1 += matrix[i][j]
		if j == n - i - 1:
			zumadiag2 += matrix[i][n - i - 1]
if zumadiag1 == s and zumadiag2 == s and (res > 0):
	print(res)
else:
	print(-1)


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())
data = []
for i in range(n):
	tmp = list(map(int, input().split()))
	data.append(tmp)
cons = 0
for i in range(n):
	if 0 not in data[i]:
		cons = sum(data[i])
		break
ans = 0
flag = 0
for i in range(n):
	for j in range(n):
		if data[i][j] == 0:
			flag = 1
			data[i][j] = cons - sum(data[i])
			ans = data[i][j]
			break
	if flag == 1:
		break
if ans > 0:
	for i in range(n):
		if sum(data[i]) != cons:
			ans = -1
			break
	for j in range(n):
		total = 0
		for i in range(n):
			total += data[i][j]
		if total != cons:
			ans = -1
			break
	total = 0
	for i in range(n):
		total += data[i][i]
	if total != cons:
		ans = -1
	total = 0
	for i in range(n):
		total += data[i][n - 1 - i]
	if total != cons:
		ans = -1
else:
	ans = -1
if n == 1:
	ans = 1
print(ans)


n = int(input())
table = []
for i in range(n):
	table.append(list(map(int, input().split())))
	for (j, vl) in enumerate(table[i]):
		if vl == 0:
			empty = (i, j)
if n == 1:
	print(1)
else:
	if empty[0] != 0:
		sum_ = sum(table[0])
	else:
		sum_ = sum(table[1])
	table[empty[0]][empty[1]] = sum_ - sum(table[empty[0]])
	if table[empty[0]][empty[1]] <= 0:
		print(-1)
	else:
		for row in table:
			if sum_ != sum(row):
				print(-1)
				break
		else:
			for j in range(n):
				s = 0
				for i in range(n):
					s += table[i][j]
				if sum_ != s:
					print(-1)
					break
			else:
				s = 0
				for i in range(n):
					for j in range(n):
						if i == j:
							s += table[i][j]
				if s != sum_:
					print(-1)
				else:
					s = 0
					for i in range(n):
						for j in range(n):
							if i == n - 1 - j:
								s += table[i][j]
					if s != sum_:
						print(-1)
					else:
						print(table[empty[0]][empty[1]])


def col_sum(matrix, col_num):
	sum = 0
	for i in range(len(matrix)):
		sum += matrix[i][col_num]
	return sum

def diag1_sum(matrix):
	sum = 0
	for i in range(len(matrix[0])):
		sum += matrix[i][i]
	return sum

def diag2_sum(matrix):
	n = len(matrix)
	sum = 0
	for i in range(n):
		sum += matrix[i][n - i - 1]
	return sum
x0 = y0 = 0
num = 0
n = int(input())
mat = []
ideal_sum = 0
for i in range(n):
	row = input()
	row = row.split()
	row = [int(x) for x in row]
	for j in range(n):
		if row[j] == 0:
			x0 = i
			y0 = j
	mat.append(row)
if n == 1:
	print(1)
else:
	if x0 != 0:
		ideal_sum = sum(mat[0])
	else:
		ideal_sum = sum(mat[1])
	empty_row_sum = sum(mat[x0])
	if empty_row_sum >= ideal_sum:
		print(-1)
	else:
		mat[x0][y0] = ideal_sum - empty_row_sum
		is_magical = True
		for i in range(n):
			if sum(mat[i]) != ideal_sum or col_sum(mat, i) != ideal_sum:
				is_magical = False
				break
		if diag1_sum(mat) != ideal_sum or diag2_sum(mat) != ideal_sum:
			is_magical = False
		if is_magical:
			print(mat[x0][y0])
		else:
			print(-1)


n = int(input())
a = 0
(r, c) = (0, -1)
square = []
for i in range(n):
	temp = list(map(int, input().split()))
	if c < 0 and temp.count(0):
		r = i
		c = temp.index(0)
	square += [temp]
s = sum(square[(r + 1) % n])
a = s - sum(square[r])
square[r][c] = a
if n > 1 and a < 1:
	print(-1)
	exit()
for i in range(n):
	if s != sum(square[i]):
		print(-1)
		exit()
square = list(map(list, zip(*square)))
for i in range(n):
	if s != sum(square[i]):
		print(-1)
		exit()
s1 = s2 = 0
for i in range(n):
	s1 += square[i][i]
	s2 += square[i][n - i - 1]
if not s1 == s2 == s:
	print(-1)
	exit()
if n == 1:
	a = 1
print(a)


from collections import defaultdict
import sys, os, math
n = int(input())
s = [list(map(int, input().split())) for i in range(n)]
if n == 1:
	print(1)
	sys.exit(0)
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)
for i in range(n):
	if sum(s[i]) != val:
		print(-1)
		sys.exit(0)
for j in range(n):
	t = 0
	for i in range(n):
		t += s[i][j]
	if t != val:
		print(-1)
		sys.exit(0)
(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)
	sys.exit(0)
print(s[pos[0]][pos[1]])


n = int(input())
if n == 1:
	print(10 ** 18 - 1)
	exit()
T = [[] for i in range(n)]
s = 0
(x, y) = (-1, -1)
for i in range(n):
	T[i] = list(map(int, input().split()))
	for j in range(n):
		if T[i][j] == 0:
			(x, y) = (i, j)
for i in range(n):
	if x != i:
		s = sum(T[i])
		break
t = 0
for i in range(n):
	t = sum(T[x])
d = s - t
if d <= 0:
	print(-1)
	exit()
T[x][y] = d
for i in range(n):
	if sum(T[i]) != s:
		print(-1)
		exit()
for j in range(n):
	tmp = 0
	for i in range(n):
		tmp += T[i][j]
	if tmp != s:
		print(-1)
		exit()
tmp = 0
for i in range(n):
	tmp += T[i][i]
if tmp != s:
	print(-1)
	exit()
tmp = 0
for i in range(n):
	tmp += T[n - i - 1][i]
if tmp != s:
	print(-1)
	exit()
print(d)


n = int(input())
a = []
(i, j) = (0, 0)
for k in range(n):
	a.append([int(s) for s in input().split()])
	if 0 in a[-1]:
		(i, j) = (k, a[-1].index(0))
if n == 1:
	print(1)
else:
	x = sum((v for v in a[0 if i else 1])) - sum((v for v in a[i]))
	a[i][j] = x
	ss = [sum(a[i]) for i in range(n)]
	ss += [sum((a[i][j] for i in range(n))) for j in range(n)]
	ss += [sum((a[i][i] for i in range(n)))]
	ss += [sum((a[n - i - 1][i] for i in range(n)))]
	print(x if len(set(ss)) == 1 and x > 0 else -1)


l = []
n = int(input())
st1 = 0
for i in range(n):
	l1 = list(map(int, input().split()))
	l.append(l1)
	if 0 in l1:
		st1 = sum(l1)
if n == 1:
	print(1)
else:
	(l1, l2) = ([], [])
	for i in range(n):
		(s1, s2) = (0, 0)
		for j in range(n):
			s1 += l[i][j]
			s2 += l[j][i]
		l1.append(s1)
		l2.append(s2)
	(l1, l2) = (set(l1), set(l2))
	l3 = 0
	k1 = 0
	for i in range(n):
		l3 += l[i][i]
		if l[i][i] == 0:
			k1 = 1
	l4 = 0
	k2 = 0
	for i in range(n):
		l4 += l[i][n - i - 1]
		if l[i][n - i - 1] == 0:
			k2 = 1
	sm = 0
	n = 0
	if (len(l1) == 2 and len(l2) == 2) and l1 == l2:
		sm = max(l1)
		if sm > st1:
			for i in l1:
				if i != sm:
					n = abs(sm - i)
			if (l3 == sm or (k1 == 1 and l3 + n == sm)) and (l4 == sm or (k2 == 1 and l4 + n == sm)) and (st1 + n == sm):
				print(n)
			else:
				print(-1)
		else:
			print(-1)
	else:
		print(-1)


def sumc(list1):
	final = 0
	for i in range(len(list1)):
		final += int(list1[i])
	return final

def sumr(list1, no):
	final = 0
	for i in range(len(list1)):
		final += int(list1[i][no])
	return final

def sumD(list1, no):
	final = 0
	if no == 1:
		for i in range(len(list1)):
			final += int(list1[i][i])
	else:
		for i in range(len(list1)):
			final += int(list1[len(list1) - i - 1][i])
	return final

def check(list1):
	temp = sumc(list1[0])
	for i in range(1, len(list1)):
		if temp != sumc(list1[i]):
			return False
		if temp != sumr(list1, i):
			return False
	if temp != sumD(list1, 0) or temp != sumD(list1, 1):
		return False
	return True

def find0(list1):
	for i in range(len(list1)):
		if int(list1[i]) == 0:
			return i
	return -1
n = int(input())
list1 = []
if n < 2:
	print(1)
else:
	for i in range(n):
		list1.append(input().split())
	for i in range(len(list1)):
		temp = find0(list1[i])
		if temp != -1:
			col = i
			row = temp
			break
	if i == 0:
		list1[i][temp] = sumc(list1[1]) - sumc(list1[i])
	else:
		list1[i][temp] = sumc(list1[0]) - sumc(list1[i])
	if list1[i][temp] < 1:
		print(-1)
	elif check(list1):
		print(list1[i][temp])
	else:
		print(-1)


n = int(input())
data = [list(map(int, input().split())) for i in range(n)]
if n == 1:
	print(1)
else:
	for i in range(n):
		if 0 in data[i]:
			y = i
			x = data[i].index(0)
			break
	sums = [0] * n
	sg = 0
	sp = 0
	plus = set()
	minus = set()
	for i in range(n):
		temp = sum(data[i])
		if i == y:
			minus.add(temp)
		else:
			plus.add(temp)
		sums = [sums[k] + data[i][k] for k in range(n)]
		sg += data[i][i]
		sp += data[i][n - i - 1]
	for i in range(n):
		if i == x:
			minus.add(sums[i])
		else:
			plus.add(sums[i])
	if x == y:
		minus.add(sg)
	else:
		plus.add(sg)
	if x == n - y - 1:
		minus.add(sp)
	else:
		plus.add(sp)
	res = -1
	if len(plus) == 1 and len(minus) == 1:
		res = plus.pop() - minus.pop()
	if res <= 0:
		res = -1
	print(res)


import sys
n = int(input())
mat = []
empty = (None, None)
if n == 1:
	print(1)
	sys.exit(0)
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])
	if cursum != rowsum:
		print(-1)
		sys.exit(0)
tmat = list(zip(*mat))
col = empty[1]
for i in range(n):
	if i == col:
		continue
	else:
		cursum = sum(tmat[i])
		if cursum != rowsum:
			print(-1)
			sys.exit(0)
row = empty[0]
irowsum = sum(mat[row])
colsum = sum(tmat[col])
if irowsum != colsum:
	print(-1)
	sys.exit(0)
left = 0
right = 0
tochange = rowsum - irowsum
if tochange <= 0:
	print(-1)
	sys.exit(0)
mat[row][col] = tochange
if True:
	for i in range(n):
		left += mat[i][i]
		right += mat[i][n - i - 1]
if left != right or left != rowsum:
	print(-1)
	sys.exit(0)
print(tochange)


n = int(input())
matrix = []
for i in range(n):
	matrix.append([int(i) for i in input().split()])
zero_col = zero_row = 0
if n == 1:
	print(1)
else:
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == 0:
				zero_col = j
				zero_row = i
	cur = (zero_row + 1) % n
	ans = 0
	for i in range(n):
		ans += matrix[cur][i]
	a1 = 0
	for i in range(n):
		a1 += matrix[zero_row][i]
	rem = ans - a1
	matrix[zero_row][zero_col] = rem
	flag = 0
	for i in range(n):
		s1 = 0
		for j in range(n):
			s1 += matrix[i][j]
		if s1 != ans:
			flag = 1
			break
	for i in range(n):
		s1 = 0
		for j in range(n):
			s1 += matrix[j][i]
		if s1 != ans:
			flag = 1
			break
	s1 = 0
	for i in range(n):
		s1 += matrix[i][i]
	if s1 != ans:
		flag = 1
	s1 = 0
	for i in range(n):
		s1 += matrix[i][n - 1 - i]
	if s1 != ans:
		flag = 1
	if flag == 1:
		print(-1)
	elif rem <= 0:
		print(-1)
	else:
		print(rem)


n = int(input())
arr = []
for i in range(n):
	arr.append([int(x) for x in input().split()])
if n == 1:
	print(1)
	exit()
(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)


def magic_square(arr, n):
	rsm = [0 for i in range(n)]
	csm = [0 for i in range(n)]
	d1 = 0
	d2 = 0
	for i in range(n):
		for j in range(n):
			rsm[i] += arr[i][j]
			csm[j] += arr[i][j]
			if i == j:
				d1 += arr[i][j]
			if i + j == n - 1:
				d2 += arr[i][j]
			if arr[i][j] == 0:
				return False
	if d1 != d2:
		return False
	for i in range(n):
		if rsm[i] != csm[i] or rsm[i] != d1:
			return False
	return True
n = int(input())
arr = []
pbuffer = []
flag = False
for i in range(n):
	ls = list(map(int, input().split()))
	arr.append(ls)
rsm = [0 for i in range(n)]
csm = [0 for i in range(n)]
d1 = 0
d2 = 0
ele = [0, 0]
for i in range(n):
	for j in range(n):
		rsm[i] += arr[i][j]
		csm[j] += arr[i][j]
		if i == j:
			d1 += arr[i][j]
		if i + j == n - 1:
			d2 += arr[i][j]
		if arr[i][j] == 0:
			ele = [i, j]
if n == 1:
	if arr[0][0] == 0:
		print('1')
	else:
		print(arr[0][0])
else:
	x = max(rsm[1], rsm[0])
	arr[ele[0]][ele[1]] = abs(rsm[ele[0]] - x)
	if magic_square(arr, n):
		print(arr[ele[0]][ele[1]])
	else:
		print('-1')


n = int(input())
(s, t, y) = ([], 0, [])
for _ in range(n):
	i = list(map(int, input().split()))
	if 0 not in i:
		s.append(sum(i))
	else:
		t = i
	y.append(i)
if len(set(s)) == 1:
	x = s[0] - sum(t)
	z = t.index(0)
	for a in range(n):
		if 0 in y[a]:
			y[a] = t[:z] + [x] + t[z + 1:]
	if all((sum([y[j][k] for j in range(n)]) == s[0] for k in range(n))) and x > 0:
		if sum([y[k][k] for k in range(n)]) == s[0]:
			if sum([y[l][n - l - 1] for l in range(n)]) == s[0]:
				print(x)
				quit()
	print(-1)
elif len(set(s)) == 0:
	print(100)
else:
	print(-1)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def solve(n, lst):
	if n == 1:
		return 1
	ans = 0
	pos = []
	for i in range(n):
		for j in range(n):
			if lst[i][j] == 0:
				pos = [i, j]
				break
	summ = 0
	if pos[0] != 0:
		for i in lst[0]:
			summ += i
	else:
		for i in lst[-1]:
			summ += i
	summ1 = 0
	for i in lst[pos[0]]:
		summ1 += i
	ans = summ - summ1
	lst[pos[0]][pos[1]] = ans
	for i in range(n):
		tempSumm = 0
		for j in lst[i]:
			tempSumm += j
		if tempSumm != summ:
			return -1
	for j in range(n):
		tempSumm = 0
		for i in range(n):
			tempSumm += lst[i][j]
		if tempSumm != summ:
			return -1
	i = 0
	j = 0
	tempSumm = 0
	while i < n and j < n:
		tempSumm += lst[i][j]
		i += 1
		j += 1
	if tempSumm != summ:
		return -1
	i = 0
	j = n - 1
	tempSumm = 0
	while j >= 0 and i < n:
		tempSumm += lst[i][j]
		i += 1
		j -= 1
	if tempSumm != summ:
		return -1
	if ans <= 0:
		return -1
	return ans
n = int(input())
lst = []
for i in range(n):
	lst.append(list(map(int, input().split())))
print(solve(n, lst))


import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list or type(n) is str:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return int(str(n)[::-1])

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True
n = ii()
if n == 1:
	print(1)
	exit()
ms = []
for i in range(n):
	ms.append(li())
sumcolms = [0] * n
diags = [0] * 2
for j in range(n):
	for i in range(n):
		sumcolms[j] += ms[i][j]
		if i == j:
			diags[0] += ms[i][j]
		if i + j == n - 1:
			diags[1] += ms[i][j]
		if ms[i][j] == 0:
			pos = (i, j)
setrowms = list(set(map(sum, ms)))
setcolms = list(set(sumcolms))
if len(setrowms) != 2 or len(setcolms) != 2 or diags[0] not in setcolms or (diags[1] not in setcolms) or (diags[0] not in setrowms) or (diags[1] not in setrowms):
	print(-1)
elif max(setrowms) - min(setrowms) != max(setcolms) - min(setcolms) or max(setrowms) - min(setrowms) < 1 or max(setrowms) - min(setrowms) < 1:
	print(-1)
else:
	ms[pos[0]][pos[1]] = abs(setrowms[0] - setrowms[1])
	sumcolms = [0] * n
	diags = [0] * 2
	for j in range(n):
		for i in range(n):
			sumcolms[j] += ms[i][j]
			if i == j:
				diags[0] += ms[i][j]
			if i + j == n - 1:
				diags[1] += ms[i][j]
			if ms[i][j] == 0:
				pos = (i, j)
	setrowms = list(set(map(sum, ms)))
	setcolms = list(set(sumcolms))
	r = []
	r.extend(setcolms)
	r.extend(setrowms)
	r.extend(diags)
	if len(set(r)) == 1:
		print(ms[pos[0]][pos[1]])
	else:
		print(-1)


n = int(input())
arr = [list(map(int, input().split())) for i in range(n)]
if n == 1:
	print(1)
	exit()
i = 0
while i < n and arr[i].count(0) == 0:
	i += 1
res = sum(arr[i == 0]) - sum(arr[i])
arr[i][arr[i].index(0)] = res
(a, b, c) = ([0] * n, [0] * n, [0] * 2)
for i in range(n):
	c[0] += arr[i][i]
	c[1] += arr[i][n - 1 - i]
	for j in range(n):
		a[i] += arr[i][j]
		b[i] += arr[j][i]
a += b + c
if res <= 0 or min(a) != max(a):
	print(-1)
else:
	print(res)


read = lambda : map(int, input().split())
n = int(input())
if n == 1:
	print(1)
	exit()
a = [list(read()) for i in range(n)]
m = 2 * n + 2
c = [0] * m
for i in range(n):
	c[i] = sum((j for j in a[i]))
	c[i + n] = sum((a[j][i] for j in range(n)))
c[m - 2] = sum((a[i][i] for i in range(n)))
c[m - 1] = sum((a[n - i - 1][i] for i in range(n)))
for i in range(n):
	if a[i].count(0):
		x = i
		y = a[i].index(0)
		break
(s1, s2) = (set(), set())
for i in range(n):
	if x != i:
		s1.add(c[i])
	else:
		s2.add(c[i])
	if y != i:
		s1.add(c[i + n])
	else:
		s2.add(c[i + n])
if x != y:
	s1.add(c[m - 2])
else:
	s2.add(c[m - 2])
if x + y != n - 1:
	s1.add(c[m - 1])
else:
	s2.add(c[m - 1])
if len(s1) > 1 or len(s2) > 1 or max(s1) <= max(s2):
	print(-1)
	exit()
ans = max(s1) - max(s2)
print(ans)


def s():
	n = int(input())
	a = [list(map(int, input().split())) for _ in range(n)]
	e = [0, 0]
	if n == 1:
		print(1)
		return
	for i in range(n):
		for j in range(n):
			if a[i][j] == 0:
				e = [i, j]
				break
	ans = 0
	f = 0
	if e[0] == 0:
		f = sum(a[1])
		ans = f - sum(a[0])
	else:
		f = sum(a[0])
		ans = f - sum(a[e[0]])
	if ans <= 0:
		print(-1)
		return
	a[e[0]][e[1]] = ans
	for k in range(n):
		if sum(a[k]) != f:
			print(-1)
			return
		if sum((a[l][k] for l in range(n))) != f:
			print(-1)
			return
	if sum((a[k][k] for k in range(n))) != f or sum((a[k][n - 1 - k] for k in range(n))) != f:
		print(-1)
		return
	print(ans)
s()


class Found(Exception):
	pass
n = int(input())
matrix = [[int(j) for j in input().split()] for i in range(n)]
try:
	for i in range(n):
		for j in range(n):
			if matrix[i][j] == 0:
				(r, c) = (i, j)
				raise Found
except Found:
	if n == 1:
		print('2')
	else:
		other_sum = 0
		row_sum = 0
		for i in range(n):
			row_sum += matrix[r][i]
			if r == 0:
				other_sum += matrix[1][i]
			else:
				other_sum += matrix[0][i]
		matrix[r][c] = other_sum - row_sum
		try:
			for i in range(n):
				now = 0
				for j in range(n):
					now += matrix[i][j]
				if now != other_sum:
					print('-1')
					raise Found
			for j in range(n):
				now = 0
				for i in range(n):
					now += matrix[i][j]
				if now != other_sum:
					print('-1')
					raise Found
			now = 0
			for i in range(n):
				now += matrix[i][i]
			if now != other_sum:
				print('-1')
				raise Found
			now = 0
			for i in range(n):
				now += matrix[i][n - i - 1]
			if now != other_sum:
				print('-1')
				raise Found
			if matrix[r][c] <= 0:
				print('-1')
				raise Found
			print(matrix[r][c])
		except Found:
			pass


from sys import stdin

def main():
	n = int(stdin.readline())
	mat = []
	if n == 1:
		print(1)
	else:
		for _ in range(n):
			mat.append(list(map(int, stdin.readline().split())))
		c = [0] * n
		check_c = [True] * n
		check_r = [True] * n
		r = [0] * n
		d1 = 0
		check_d1 = True
		d2 = 0
		check_d2 = True
		(x, y) = (-1, -1)
		for i in range(n):
			for j in range(n):
				if mat[i][j] == 0:
					check_c[j] = False
					check_r[i] = False
					if i == j:
						check_d1 = False
					if i + j == n - 1:
						check_d2 = False
					(x, y) = (i, j)
				r[i] += mat[i][j]
				c[j] += mat[i][j]
				if i == j:
					d1 += mat[i][j]
				if i + j == n - 1:
					d2 += mat[i][j]
		c_sum = -1
		for i in range(n):
			if check_r[i]:
				c_sum = r[i]
		for i in range(n):
			if check_c[i]:
				c_sum = c[i]
		if c_sum - r[x] != c_sum - c[y]:
			print(-1)
		else:
			mat[x][y] = c_sum - r[x]
			r[x] = c_sum
			c[y] = c_sum
			ok = True
			if mat[x][y] <= 0:
				ok = False
			for i in range(n):
				c[i] = 0
				r[i] = 0
			d1 = 0
			d2 = 0
			for i in range(n):
				for j in range(n):
					r[i] += mat[i][j]
					c[j] += mat[i][j]
					if i == j:
						d1 += mat[i][j]
					if i + j == n - 1:
						d2 += mat[i][j]
			if not all((r[i] == c_sum for i in range(n))):
				ok = False
			if not all((c[i] == c_sum for i in range(n))):
				ok = False
			if d1 != c_sum:
				ok = False
			if d2 != c_sum:
				ok = False
			if ok:
				print(mat[x][y])
			else:
				print(-1)
main()


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)


def nums(a, n):
	S = {sum((a[i][i] for i in range(n))), sum((a[i][n - i - 1] for i in range(n)))}
	for i in range(n):
		S |= {sum(a[i]), sum((a[j][i] for j in range(n)))}
	return S
n = int(input())
if n == 1:
	print(1)
	exit()
a = [list(map(int, input().split())) for i in range(n)]
(x, y) = next(((i, j) for i in range(n) for j in range(n) if a[i][j] == 0))
S = nums(a, n)
a[x][y] = max(S) - min(S)
print(-1 if len(nums(a, n)) != 1 or a[x][y] <= 0 else a[x][y])


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
	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())


import os
import sys
from io import BytesIO, IOBase
mod = 10 ** 9 + 7

def main():
	n = int(input())
	arr = []
	for i in range(n):
		arr.append(list(map(int, input().split())))
	if n == 1:
		print(1)
		exit()
	(x, y) = (0, 0)
	flag = 0
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 0:
				x = i
				y = j
				flag = 1
				break
		if flag:
			break
	sumd1 = 0
	sumd2 = 0
	rows = set()
	columns = [0] * n
	for i in range(n):
		rowtemp = 0
		for j in range(n):
			rowtemp += arr[i][j]
			columns[j] += arr[i][j]
			if i == j:
				sumd1 += arr[i][j]
			if i == n - j - 1:
				sumd2 += arr[i][j]
		rows.add(rowtemp)
		if len(rows) > 2:
			print(-1)
			exit()
	rows = list(rows)
	rows.sort()
	columns = list(set(columns))
	columns.sort()
	if len(rows) == 2 and len(columns) == 2:
		ansr = rows[1] - rows[0]
		ansc = columns[1] - columns[0]
		if ansr == ansc:
			if x == y:
				sumd1 += ansr
			if x == n - y - 1:
				sumd2 += ansr
			if sumd1 == sumd2 and sumd1 == rows[1] and (sumd1 == columns[1]):
				print(ansr)
			else:
				print(-1)
		else:
			print(-1)
	else:
		print(-1)

def nouse0():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse1():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse2():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


import sys
N = int(input())
position = None
tot = 0
matrix = []
col_sum = [0 for i in range(N)]
row_sum = [0 for i in range(N)]
diag1_sum = 0
diag2_sum = 0
for i in range(N):
	line = input()
	nums = line.split()
	matrix.append(nums)
	for j in range(len(nums)):
		if nums[j] == '0':
			position = (i, j)
		if i == j:
			diag1_sum += int(nums[j])
		if j == N - i - 1:
			diag2_sum += int(nums[j])
		col_sum[j] += int(nums[j])
	if (position == None or position[0] != i) and tot == 0:
		tot = sum([int(n) for n in nums])
	row_sum[i] = sum([int(n) for n in nums])
r = tot - row_sum[position[0]]
if r < -1 or r > 1000000000000000000:
	print(-1)
	sys.exit()
for i in range(N):
	if row_sum[i] != tot and i != position[0]:
		print(-1)
		sys.exit()
	if col_sum[i] != tot and i != position[1]:
		print(-1)
		sys.exit()
if position[0] != position[1] and position[1] != N - position[0] - 1:
	if tot == col_sum[position[1]] + r and tot == diag1_sum and (tot == diag2_sum):
		if r == 0:
			print(-1)
		else:
			print(r)
	else:
		print(-1)
if position[0] == position[1] and position[1] != N - position[0] - 1:
	if tot == col_sum[position[1]] + r and tot == diag1_sum + r and (tot == diag2_sum):
		if r == 0:
			print(-1)
		else:
			print(r)
	else:
		print(-1)
if position[0] != position[1] and position[1] == N - position[0] - 1:
	if tot == col_sum[position[1]] + r and tot == diag2_sum + r and (tot == diag1_sum):
		if r == 0:
			print(-1)
		else:
			print(r)
	else:
		print(-1)
if position[0] == position[1] and position[1] == N - position[0] - 1:
	if tot == col_sum[position[1]] + r and tot == diag2_sum + r and (tot == diag1_sum + r):
		if N == 1:
			print(1)
		else:
			print(r)
	else:
		print(-1)


n = int(input())
grid = [[int(x) for x in input().split()] for _ in range(n)]
if n == 1:
	print(1)
	exit(0)
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)
		exit(0)
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)
		exit(0)
count = 0
for i in range(n):
	count += grid[i][i]
if count != sum(grid[0]):
	print(-1)
	exit(0)
count = 0
for i in range(n):
	count += grid[n - 1 - i][i]
if count != sum(grid[0]):
	print(-1)
	exit(0)
print(answer if answer > 0 else -1)


import sys
n = int(input())
if n == 1:
	print(1)
	sys.exit(0)
magic_square = []
for _ in range(n):
	magic_square.append(list(map(int, input().split())))
(row_sums, col_sums) = ([], [])
(da, db) = (0, 0)
ret = 0

def cal():
	global da, db
	for row in magic_square:
		row_sums.append(sum(row))
	for i in range(n):
		acc = 0
		for j in range(n):
			acc += magic_square[j][i]
		col_sums.append(acc)
		da += magic_square[i][i]
		db += magic_square[-i - 1][i]
cal()
if len(set(row_sums)) > 2 or len(set(col_sums)) > 2:
	print(-1)
else:
	(x, y) = (0, 0)
	(difx, dify) = (row_sums[1] - row_sums[0], col_sums[1] - col_sums[0])
	for i in range(1, n):
		if row_sums[i] < row_sums[i - 1]:
			(x, difx) = (i, row_sums[i - 1] - row_sums[i])
		if col_sums[i] < col_sums[i - 1]:
			(y, dify) = (i, col_sums[i - 1] - col_sums[i])
	if difx != dify:
		print(-1)
	else:
		magic_square[x][y] = difx
		(row_sums, col_sums) = ([], [])
		(da, db) = (0, 0)
		cal()
		row_sums.append(da)
		col_sums.append(db)
		if len(set(row_sums)) > 1 or len(set(col_sums)) > 1:
			print(-1)
		else:
			print(difx)


def check(l):
	sdiag1 = sdiag2 = 0
	l1 = []
	l2 = []
	l3 = []
	l4 = []
	n = len(l)
	for i in range(n):
		srow = scol = 0
		for j in range(n):
			srow += l[j][i]
			scol += l[i][j]
		l1.append(srow)
		l2.append(scol)
	for i in range(n):
		sdiag1 += l[i][i]
		sdiag2 += l[n - i - 1][i]
	l3.append(sdiag1)
	l4.append(sdiag2)
	if set(l1) == set(l2):
		if set(l2) == set(l3):
			if set(l3) == set(l4):
				return True
	return False
ttt = int(input())
l = []
for _ in range(ttt):
	l1 = list(map(int, input().split()))
	l = l + [l1]
ke = 0
for i in range(ttt):
	for j in range(ttt):
		if l[i][j] == 0:
			i1 = i
			j1 = j
			ke = 1
			break
	if ke == 1:
		break
for i in range(ttt):
	if i != i1:
		su = sum(l[i])
	else:
		sut = sum(l[i])
if len(l) != 1:
	l[i1][j1] = su - sut
	if su - sut > 0:
		if su - sut == 0:
			print(-1)
		elif check(l):
			print(su - sut)
		else:
			print(-1)
	else:
		print(-1)
else:
	print(1)


import sys
isis = True
n = int(input())
a = []
for i in range(0, n):
	a.append(list(map(int, list(input().split()))))
if n == 1:
	print(1)
	sys.exit(0)
sum = 0
for i in range(0, n):
	su = 0
	for j in range(0, n):
		if a[i][j] == 0:
			break
		su += a[i][j]
	sum = max(sum, su)
for i in range(0, n):
	su = 0
	for j in range(0, n):
		if a[j][i] == 0:
			break
		su += a[j][i]
	sum = max(sum, su)
su = 0
su1 = 0
for i in range(0, n):
	su += a[i][i]
	su1 += a[i][n - i - 1]
sum = max(sum, su)
sum = max(sum, su1)
ii = 0
jj = 0
for i in range(0, n):
	for j in range(0, n):
		if a[i][j] == 0:
			ii = i
			jj = j
tr = 0
for i in range(0, n):
	tr += a[ii][i]
num = sum - tr
a[ii][jj] = num
s = 0
for i in range(0, n):
	s = 0
	for j in range(0, n):
		s += a[i][j]
	if sum != s:
		isis = False
s = 0
for i in range(0, n):
	s = 0
	for j in range(0, n):
		s += a[j][i]
	if sum != s:
		isis = False
s = 0
s1 = 0
for i in range(0, n):
	s += a[i][i]
	s1 += a[i][n - i - 1]
if sum != s:
	isis = False
if sum != s1:
	isis = False
if isis == False:
	print(-1)
	sys.exit(0)
if num <= 0:
	print(-1)
	sys.exit(0)
if isis == True:
	print(num)


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)
main()


n = int(input())
row = -1
l = 0
r = 0
left = 0
right = 0
arr = []
idex = 0
ans = []
for i in range(n):
	next = list(map(int, input().split()))
	a = next[i]
	b = next[-(i + 1)]
	arr.append(next)
	if 0 in next:
		idex = next.index(0)
		row = i
		if a == 0:
			l = 1
		if b == 0:
			r = 1
	else:
		ans.append(sum(next))
	left += a
	right += b
if n == 1:
	print(1)
else:
	sumA = 0
	if row != 0:
		sumA = sum(arr[0])
	else:
		sumA = sum(arr[1])
	need = sumA - sum(arr[row])
	arr[row][idex] = need
	if l:
		left += need
	if r:
		right += need
	if need > 0:
		ans.append(left)
		ans.append(right)
		ans.append(sum(arr[row]))
		for j in range(n):
			s = 0
			for i in range(n):
				s += arr[i][j]
			ans.append(s)
		if len(set(ans)) == 1:
			print(need)
		else:
			print(-1)
	else:
		print(-1)


n = int(input())
ans = 1
tot = 0
c = 0
r = -1
col = -1
a = [[int(x) for x in input().split()] for y in range(n)]
if n == 1:
	print(1)
	exit(0)
for i in range(n):
	for j in range(n):
		if a[i][j] == 0:
			r = i
			col = j
			break
	if r != -1:
		break
for i in range(0, n):
	if i == r:
		continue
	c = 0
	for j in range(n):
		c = c + a[i][j]
	if tot != 0 and tot != c:
		ans = -1
		break
	else:
		tot = c
	if ans == -1:
		break
if ans != -1:
	c = 0
	for i in range(n):
		c += a[r][i]
a[r][col] = tot - c
for i in range(0, n):
	c = 0
	for j in range(n):
		c = c + a[j][i]
	if tot != c:
		ans = -1
		break
	else:
		tot = c
	if ans == -1:
		break
c = 0
for i in range(0, n):
	c = c + a[i][i]
if c != tot:
	ans = -1
c = 0
for i in range(n - 1, -1, -1):
	c = c + a[n - i - 1][i]
if c != tot:
	ans = -1
if ans == -1 or pow(10, 18) < a[r][col] or a[r][col] <= 0:
	print(-1)
else:
	print(a[r][col])


n = int(input())
arr = []
if n == 1:
	print(1)
	exit()
for _ in range(n):
	a = list(map(int, input().split()))
	arr.append(a)
for i in range(n):
	summ = 0
	flag = True
	for j in range(n):
		if arr[j][i] == 0:
			flag = False
		else:
			summ += arr[j][i]
	if flag:
		break
for i in range(n):
	summ1 = 0
	flag = False
	for j in range(n):
		if arr[j][i] == 0:
			flag = True
		else:
			summ1 += arr[j][i]
	if flag:
		val = max(0, summ - summ1)
		break
if val == 0:
	print(-1)
else:
	s = set()
	for i in range(n):
		summ = 0
		for j in range(n):
			if arr[j][i] == 0:
				summ += val
			else:
				summ += arr[j][i]
		s.add(summ)
	for i in range(n):
		summ = 0
		for j in range(n):
			if arr[i][j] == 0:
				summ += val
			else:
				summ += arr[i][j]
		s.add(summ)
	summ = 0
	for i in range(n):
		if arr[i][i] == 0:
			summ += val
		else:
			summ += arr[i][i]
	s.add(summ)
	k = n - 1
	summ = 0
	for i in range(n):
		if arr[i][k] == 0:
			summ += val
		else:
			summ += arr[i][k]
		k -= 1
	s.add(summ)
	if len(s) == 1:
		print(val)
	else:
		print(-1)


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]

def check_rows(g, n):
	st = []
	for i in g:
		sm = 0
		for j in i:
			sm += j
		st.append(sm)
	return st

def check_col(g, n):
	st = []
	for i in range(n):
		sm = 0
		for j in range(n):
			sm += g[j][i]
		st.append(sm)
	return st

def dig1(g, n):
	st = []
	sm = 0
	for i in range(n):
		for j in range(n):
			if i == j:
				sm += g[i][j]
	st.append(sm)
	return st

def dig2(g, n):
	st = []
	sm = 0
	for i in range(n):
		for j in range(n):
			if i + j + 1 == n:
				sm += g[i][j]
	st.append(sm)
	return st
n = INT()
g = []
for i in range(n):
	s = LIST()
	g.append(s)
k = []
pos = []
for i in range(n):
	sm = 0
	for j in range(n):
		sm += g[i][j]
		if g[i][j] == 0:
			pos.append([i, j])
	k.append(sm)
k.sort()
if len(k) == 1:
	print(1)
	exit(0)
k1 = k[0]
k2 = k[1]
k3 = k2 - k1
p1 = pos[0][0]
p2 = pos[0][1]
g[p1][p2] = k3
r1 = check_rows(g, n)
r2 = check_col(g, n)
r3 = dig1(g, n)
r4 = dig2(g, n)
if len(set(r1)) == len(set(r2)) == len(set(r3)) == len(set(r4)) == 1 and k3 > 0 and (r1[0] == r2[0] == r3[0] == r4[0] == k2):
	print(k3)
else:
	print('-1')


n = int(input())
a = []
s = 0
zero_i = None
zero_j = None
zero_in_1 = True
for i in range(n):
	a.append(list(map(int, input().split())))
	for j in range(n):
		if a[i][j] == 0:
			zero_i = i
			zero_j = j
if n == 1:
	print(1)
	exit(0)
zero_s = sum(a[zero_i])
for i in range(len(a)):
	if i != zero_i:
		s = sum(a[i])
		break
zero_value = s - zero_s
if zero_value <= 0:
	print(-1)
	exit(0)
a[zero_i][zero_j] = zero_value
for i in range(n):
	if sum(a[i]) != s:
		print(-1)
		exit(0)
for j in range(n):
	column_s = 0
	for i in range(n):
		column_s += a[i][j]
	if column_s != s:
		print(-1)
		exit(0)
d1_s = 0
for i in range(n):
	d1_s += a[i][i]
if d1_s != s:
	print(-1)
	exit(0)
d2_s = 0
for i in range(n):
	d2_s += a[i][n - i - 1]
if d2_s != s:
	print(-1)
	exit(0)
print(zero_value)


n = int(input())
val = [list(map(int, input().split())) for _ in range(n)]
(zi, zj) = next(((i, j) for i in range(n) for j in range(n) if val[i][j] == 0))
ans = max((sum(val[i]) for i in range(n))) - sum(val[zi])
if n == 1:
	print(1)
elif ans > 0:
	val[zi][zj] = ans
	rows = [sum(val[i]) for i in range(n)]
	cols = [sum((val[i][j] for i in range(n))) for j in range(n)]
	diag1 = sum((val[i][i] for i in range(n)))
	diag2 = sum((val[i][n - 1 - i] for i in range(n)))
	if min(rows) == max(rows) == min(cols) == max(cols) == diag1 == diag2:
		print(ans)
	else:
		print(-1)
else:
	print(-1)


n = int(input())
g_rl = []
g_lr = []
stroka = []
stolbec = []
x = 0
y = 0
f = True
flag = True
arr = []
for j in range(n):
	stolbec.append([])
for i in range(n):
	arr.append(list(map(int, input().split())))
if n == 1:
	if arr[0][0] == 0:
		print(1)
	else:
		print(-1)
else:
	for i in range(n):
		a = arr[i] * 1
		if f and a.count(0) == 1:
			x = a.index(0)
			y = i
			f = False
			if i == 0:
				t = sum(arr[1]) - sum(a)
			else:
				t = stroka[0] - sum(a)
			if t <= 0:
				flag = False
		stroka.append(sum(a))
		for j in range(n):
			stolbec[j].append(a[j])
		g_lr.append(a[n - 1 - i])
		g_rl.append(a[i])
	for j in range(n):
		stolbec[j] = sum(stolbec[j])
	g_lr = sum(g_lr)
	g_rl = sum(g_rl)
	stroka[y] += t
	stolbec[x] += t
	if x == y:
		g_rl += t
	if x + y == n - 1:
		g_lr += t
	h = stroka + stolbec + [g_lr] + [g_rl]
	h = list(set(h))
	if len(h) == 1 and t > 0:
		print(t)
	else:
		print(-1)


n = int(input())
if n == 1:
	print(1)
	exit()
a = []
for i in range(n):
	b = list(map(int, input().split()))
	a.append(b)
sum1 = 0
for i in range(n):
	if 0 not in a[i]:
		sum1 = sum(a[i])
		break
x = -1
y = -1
for i in range(n):
	if 0 in a[i]:
		y = i
		x = a[i].index(0)
		a[i][x] = sum1 - sum(a[i])
		if a[i][x] <= 0:
			print(-1)
			exit()
for i in range(n):
	if sum(a[i]) != sum1:
		print(-1)
		exit()
for i in range(n):
	sum2 = 0
	for j in range(n):
		sum2 += a[j][i]
	if sum2 != sum1:
		print(-1)
		exit()
sum2 = 0
sum3 = 0
for i in range(n):
	sum2 += a[i][i]
	sum3 += a[n - i - 1][i]
if sum1 != sum2 or sum1 != sum3:
	print(-1)
	exit()
print(a[y][x])


n = int(input())
grid = list()
for i in range(n):
	grid.append(list(map(int, input().strip().split())))
if n != 1:
	for i in range(n):
		if 0 in grid[i]:
			row = i
			for j in range(n):
				if 0 == grid[i][j]:
					col = j
					break
	required_sum = -1
	isum = 0
	for i in range(n):
		if i != row:
			isum = sum(grid[i])
		if isum > required_sum:
			required_sum = isum
	actual_sum = sum(grid[row])
	grid[row][col] = required_sum - actual_sum
	if grid[row][col] == 0:
		print('-1')
	else:
		flag = 0
		for i in range(n):
			if sum(grid[i]) != required_sum:
				flag = 1
				break
		if flag == 0:
			for i in range(n):
				sumcol = 0
				for j in range(n):
					sumcol += grid[j][i]
				if sumcol != required_sum:
					flag = 1
					break
		if flag == 0:
			sumd1 = 0
			for i in range(n):
				sumd1 += grid[i][i]
			if sumd1 != required_sum:
				flag = 1
		if flag == 0:
			sumd1 = 0
			for i in range(n):
				sumd1 += grid[i][len(grid) - 1 - i]
			if sumd1 != required_sum:
				flag = 1
		if flag != 0:
			print('-1')
		elif grid[row][col] >= 1:
			print(grid[row][col])
		else:
			print('-1')
else:
	print('1')


I = lambda : list(map(int, input().split()))
n = int(input())
a = [I() for i in range(n)]
if n == 1:
	print(1)
else:
	s = list(map(sum, a))
	x = max(s) - min(s)
	for i in range(n):
		for j in range(n):
			if a[i][j] == 0:
				a[i][j] = x
	s = list(map(sum, a))
	s += [sum([a[j][i] for j in range(n)]) for i in range(n)]
	s += [sum([a[i][i] for i in range(n)]), sum([a[i][n - 1 - i] for i in range(n)])]
	print(x if min(s) == max(s) and x > 0 else -1)


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])


N = int(input())
rows = []
for i in range(N):
	rows.append(list(map(int, input().split())))
if N == 1:
	print(1)
	exit()
for row in rows:
	if 0 not in row:
		key = sum(row)
		break
for row in rows:
	if 0 not in row and sum(row) != key:
		print(-1)
		exit()
for row in rows:
	if 0 in row:
		ans = key - sum(row)
if ans <= 0:
	print(-1)
	exit()
for i in range(N):
	for j in range(N):
		if rows[i][j] == 0:
			rows[i][j] = ans
columns = []
for j in range(N):
	columns.append([rows[i][j] for i in range(N)])
for column in columns:
	if sum(column) != key:
		print(-1)
		exit()
diagonal = [rows[i][i] for i in range(N)]
if sum(diagonal) != key:
	print(-1)
	exit()
off_diagonal = [rows[i][N - i - 1] for i in range(N)]
if sum(off_diagonal) != key:
	print(-1)
	exit()
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
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
s = 0
have_zero = False
for x in range(lines):
	n = grid[x][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
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:
	number_with_zero.add(s)
elif no_zero == -1:
	no_zero = s
elif no_zero != s:
	impossible = True
if impossible:
	print(-1)
elif len(number_with_zero) == 1:
	num = list(number_with_zero)[0]
	if no_zero - num <= 0:
		print(-1)
	else:
		print(no_zero - num)
else:
	print(-1)


n = int(input())
if n == 1:
	x = input()
	print(1)
	exit()
row = set()
a = []
for _ in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	if 0 not in a[i]:
		total = sum(a[i])
	else:
		ind = a[i].index(0)
		x = i
		y = ind
		val = sum(a[i])
a[x][y] = total - val
if a[x][y] <= 0:
	print(-1)
	exit()
for i in range(n):
	row.add(sum(a[i]))
if len(row) != 1:
	print(-1)
	exit()
diag1 = [a[i][i] for i in range(n)]
diag2 = [row[-i - 1] for (i, row) in enumerate(a)]
d1 = sum(diag1)
d2 = sum(diag2)
if d1 == d2 == total:
	pass
else:
	print(-1)
	exit()
columns = [sum((row[i] for row in a)) for i in range(len(a[0]))]
if len(set(columns)) != 1:
	print(-1)
	exit()
print(a[x][y])


import sys
n = int(input())
if n <= 1:
	print(1)
else:
	a = []
	for _ in range(n):
		a.append(list(map(int, sys.stdin.readline().split())))
	zero = None
	for i in range(n):
		for j in range(n):
			if a[i][j] == 0:
				zero = [i, j]
				break
		if zero:
			break
	mag = None
	noSol = False
	for i in range(n):
		if i != zero[0]:
			s = sum(a[i])
			if mag:
				if mag != s:
					noSol = True
					print(-1)
					break
			else:
				mag = s
	if noSol == False:
		for j in range(n):
			if j != zero[1]:
				s = sum([a[i][j] for i in range(n)])
				if mag != s:
					noSol = True
					print(-1)
					break
	if noSol == False:
		sumi = sum(a[zero[0]])
		sumj = sum([a[i][zero[1]] for i in range(n)])
		if sumi == sumj:
			if mag > sumi:
				(i, j) = zero
				a[i][j] = mag - sumi
				sdiag1 = sum([a[i][i] for i in range(n)])
				if sdiag1 == mag:
					sdiag2 = sum([a[i][n - 1 - i] for i in range(n)])
					if sdiag2 == mag:
						print(mag - sumi)
					else:
						print(-1)
				else:
					print(-1)
			else:
				print(-1)
		else:
			print(-1)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest
INF = 99999999999999999999999999999999

def outIn(x):
	print(x, flush=True)
	return input()

def main():
	mod = 1000000007
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		n = ri()
		matrix = []
		for i in range(n):
			matrix.append(ria())
		if n == 1:
			print(1)
		else:
			r1 = {}
			r0 = {}
			for i in matrix:
				k = sum(i)
				if 0 not in i:
					if k in r1:
						r1[k] += 1
					else:
						r1[k] = 1
				elif k in r0:
					r0[k] += 1
				else:
					r0[k] = 1
			c1 = {}
			c0 = {}
			for i in range(n):
				k = 0
				f = 0
				for j in range(n):
					if matrix[j][i] == 0:
						f = 1
					k += matrix[j][i]
				if f:
					if k in c0:
						c0[k] += 1
					else:
						c0[k] = 1
				elif k in c1:
					c1[k] += 1
				else:
					c1[k] = 1
			sd1 = 0
			f1 = 0
			sd2 = 0
			f2 = 0
			d = {}
			for i in range(n):
				sd1 += matrix[i][i]
				if matrix[i][i] == 0:
					f1 = 1
				if matrix[i][n - i - 1] == 0:
					f2 = 1
				sd2 += matrix[i][n - i - 1]
			if sd1 in d:
				d[sd1] += 1
			else:
				d[sd1] = 1
			if sd2 in d:
				d[sd2] += 1
			else:
				d[sd2] = 1
			if len(r1) == 1 and len(c1) == 1 and (len(r0) == 1) and (len(c0) == 1):
				f = 1
				ns = 0
				gs = 0
				for i in r1:
					for j in c1:
						ns = i
						if i != j:
							f = 0
				for i in r0:
					for j in c0:
						gs = i
						if i != j:
							f = 0
				if f == 0 or gs >= ns:
					print(-1)
				else:
					f = 1
					if f1 + f2 == 2:
						if sd1 != gs or sd2 != gs:
							f = 0
					if f1 + f2 == 1:
						if f1 == 1:
							if sd1 != gs or sd2 != ns:
								f = 0
						if f2 == 1:
							if sd2 != gs or sd1 != ns:
								f = 0
					if f1 + f2 == 0:
						if sd1 != ns or sd2 != ns:
							f = 0
					if f == 0:
						print(-1)
					else:
						print(ns - gs)
			else:
				print(-1)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


n = int(input())
zi = -1
zj = -1
matrix = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
	if 0 in matrix[i]:
		zi = i
		zj = matrix[i].index(0)
		break
if n == 1:
	print(1)
	exit()
goldsum = sum(matrix[not zi])
curSum = sum(matrix[zi])
matrix[zi][zj] = goldsum - curSum
if matrix[zi][zj] <= 0:
	print(-1)
	exit()
for i in range(n):
	curSum = sum(matrix[i])
	if curSum != goldsum:
		print(-1)
		exit()
for i in range(n):
	curSum = sum([row[i] for row in matrix])
	if curSum != goldsum:
		print(-1)
		exit()
curSum = 0
for i in range(n):
	curSum += matrix[i][i]
if curSum != goldsum:
	print(-1)
	exit()
curSum = 0
for i in range(n):
	curSum += matrix[i][n - i - 1]
if curSum != goldsum:
	print(-1)
	exit()
print(matrix[zi][zj])


n = int(input())
num = [[int(x) for x in input().split()] for y in range(n)]
if n == 1:
	print(1)
	exit(0)
sum1 = 0
sum2 = 0
sumx = [0] * n
sumy = [0] * n
for x in range(n):
	for y in range(n):
		if num[x][y] == 0:
			nx = x
			ny = y
		if x == y:
			sum1 += num[x][y]
		if x + y == n - 1:
			sum2 += num[x][y]
		sumx[x] += num[x][y]
		sumy[y] += num[x][y]
if nx == 1:
	sum = sumx[0]
else:
	sum = sumx[1]
ans = sum - sumx[nx]
if ans < 1:
	print(-1)
	exit(0)
if nx == ny:
	sum1 += ans
if nx + ny == n - 1:
	sum2 += ans
if sum1 != sum or sum2 != sum:
	print(-1)
	exit(0)
for x in range(n):
	now = sumx[x]
	if nx == x:
		now += ans
	if now != sum:
		print(-1)
		exit(0)
for y in range(n):
	now = sumy[y]
	if ny == y:
		now += ans
	if now != sum:
		print(-1)
		exit(0)
print(ans)


n = int(input())
x = -1
y = -1
a = []
s = [0] * (2 * n + 2)
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	for j in range(n):
		if not a[i][j]:
			x = i
			y = j
			break
for i in range(n):
	s[i] = sum(a[i])
for i in range(n, 2 * n):
	sm = 0
	for j in range(n):
		sm += a[j][i - n]
	s[i] = sm
for i in range(n):
	s[2 * n] += a[i][i]
	s[2 * n + 1] += a[i][n - i - 1]
check = True
if x == y and x + y == n - 1:
	if not s[x] == s[n + y] == s[2 * n] == s[2 * n + 1]:
		print(-1)
		check = False
elif x == y:
	if not s[x] == s[n + y] == s[2 * n]:
		print(-1)
		check = False
elif x + y == n - 1:
	if not s[x] == s[n + y] == s[2 * n + 1]:
		print(-1)
		check = False
elif not s[x] == s[n + y]:
	print(-1)
	check = False
if check:
	for i in range(2 * n + 2):
		if not check:
			break
		for j in range(2 * n + 2):
			if i != x and i != n + y and (j != n + y) and (j != x) and (not (x == y and i == 2 * n)) and (not (x == y and j == 2 * n)) and (not (x + y == n - 1 and i == 2 * n + 1)) and (not (x + y == n - 1 and j == 2 * n + 1)) and (s[i] != s[j]):
				check = False
				print(-1)
				break
if check:
	if n == 1:
		print(1)
	elif x:
		if s[0] <= s[x]:
			print(-1)
		else:
			print(s[0] - s[x])
	elif s[1] <= s[x]:
		print(-1)
	else:
		print(s[1] - s[x])


n = int(input())
A = []
for i in range(n):
	x = list(map(int, input().split()))
	A.append(x)
if n == 1:
	print(1)
else:
	row = -1
	col = -1
	for i in range(n):
		for j in range(n):
			if A[i][j] == 0:
				row = i
				col = j
	s = 0
	val = 0
	if row < n - 1:
		s = sum(A[row + 1])
		z = sum(A[row])
		val = s - z
		A[row][col] = val
	else:
		s = sum(A[row - 1])
		z = sum(A[row])
		val = s - z
		A[row][col] = val

def isMagicSquare(mat):
	s = 0
	N = len(mat)
	for i in range(0, N):
		s = s + mat[i][i]
	s2 = 0
	for i in range(0, N):
		s2 = s2 + mat[i][N - i - 1]
	if s != s2:
		return False
	for i in range(0, N):
		rowSum = 0
		for j in range(0, N):
			rowSum += mat[i][j]
		if rowSum != s:
			return False
	for i in range(0, N):
		colSum = 0
		for j in range(0, N):
			colSum += mat[j][i]
		if s != colSum:
			return False
	return True
if n != 1:
	if isMagicSquare(A) == True and val >= 1:
		print(val)
	else:
		print(-1)


(n, Result1, Result2, Sum, Result) = (int(input()), 0, 0, 0, 0)
Square = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
	Square[i] = list(map(int, input().split()))
if n == 1:
	print('1')
	exit()
for i in range(n):
	(Sum1, Sum2, Control1, Control2) = (0, 0, 0, 0)
	for j in range(n):
		(Sum1, Sum2) = (Sum1 + Square[i][j], Sum2 + Square[j][i])
		if Square[i][j] == 0:
			Control1 = 1
		if Square[j][i] == 0:
			Control2 = 1
	if Control1 == 1:
		Result1 = Sum1
	elif Sum == 0:
		Sum = Sum1
	elif Sum1 != Sum:
		print('-1')
		exit()
	if Control2 == 1:
		Result2 = Sum2
	elif Sum == 0:
		Sum = Sum2
	elif Sum2 != Sum:
		print('-1')
		exit()
if Result2 == Result1 and Result2 < Sum:
	Result = Sum - Result1
else:
	print('-1')
	exit()
(Sum1, Sum2, Control1, Control2) = (0, 0, 0, 0)
for i in range(n):
	(Sum1, Sum2) = (Sum1 + Square[i][i], Sum2 + Square[i][n - 1 - i])
	if Square[i][i] == 0:
		Control1 = 1
	if Square[i][n - 1 - i] == 0:
		Control2 = 1
if Control1 == 1 and Sum1 + Result != Sum:
	print('-1')
	exit()
if Control1 == 0 and Sum1 != Sum:
	print('-1')
	exit()
if Control2 == 1 and Sum2 + Result != Sum:
	print('-1')
	exit()
if Control2 == 0 and Sum2 != Sum:
	print('-1')
	exit()
print(Result)


a = int(input())
x = []
if a == 1:
	print(1)
else:
	for i in range(a):
		x.append(list(map(int, input().split())))
	s = 0
	sums = []
	for i in x:
		if 0 in i:
			s = sum(i)
		else:
			sums.append(sum(i))
	if sums[0] > s:
		s = sums[0] - s
	else:
		print(-1)
		quit()
	for i in range(a):
		if 0 in x[i]:
			x[i][x[i].index(0)] = s
	sums = sum(x[0])
	for i in range(a):
		if sums != sum(x[i]):
			print(-1)
			quit()
	for i in range(a):
		y = 0
		for j in range(a):
			y += x[j][i]
		if sums != y:
			print(-1)
			quit()
	y = 0
	for i in range(a):
		y += x[i][i]
	if sums != y:
		print(-1)
		quit()
	y = 0
	for i in range(a):
		y += x[i][a - i - 1]
	if sums != y:
		print(-1)
		quit()
	print(s)


import sys, os.path
from collections import *
from copy import *
import math
mod = 10 ** 9 + 7
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
n = int(input())
mat = []
for i in range(n):
	l = list(map(int, input().split()))
	mat.append(l)
if n == 1:
	print(1)
else:
	x = 0
	y = 0
	for i in range(n):
		for j in range(n):
			if mat[i][j] == 0:
				x = i
				y = j
	r = 0
	r1 = 0
	for i in range(n):
		r += mat[x][i]
	if x != 0:
		for i in range(n):
			r1 += mat[0][i]
	else:
		for i in range(n):
			r1 += mat[1][i]
	mat[x][y] = abs(r1 - r)
	flag = 0
	sm = 0
	sm2 = 0
	for i in range(n):
		sm = 0
		sm2 = 0
		for j in range(n):
			sm += mat[i][j]
			sm2 += mat[j][i]
		if sm != r1 or sm2 != r1:
			flag = 1
			break
	sm = 0
	sm2 = 0
	for i in range(n):
		sm += mat[i][i]
		sm2 += mat[i][n - i - 1]
	if sm != r1 or sm2 != r1:
		flag = 1
	if flag == 0 and mat[x][y] != 0:
		print(mat[x][y])
	else:
		print(-1)


import sys
n = int(input(), 10)
a = []
rowsum = 0
rs = set()
columnsum = 0
cs = set()
x = 0
d = 0
if n == 1:
	print('1')
	sys.exit()
for i in range(n):
	a.extend([int(i) for i in input().split()])
for i in range(n * n):
	if a[i] == 0:
		x = i
for i in range(n):
	rowsum = 0
	for j in range(n):
		rowsum = rowsum + a[i * n + j]
	rs.add(rowsum)
for i in range(n):
	columnsum = 0
	for j in range(n):
		columnsum = columnsum + a[i + j * n]
	cs.add(columnsum)
if len(rs) != 2 or len(cs) != 2:
	print('-1')
	sys.exit()
d = abs(rs.pop() - rs.pop())
a[x] = d
rs = set()
cs = set()
for i in range(n):
	rowsum = 0
	for j in range(n):
		rowsum = rowsum + a[i * n + j]
	rs.add(rowsum)
for i in range(n):
	columnsum = 0
	for j in range(n):
		columnsum = columnsum + a[i + j * n]
	cs.add(columnsum)
diag1 = 0
diag2 = 0
for i in range(n):
	diag1 = diag1 + a[i * n + i]
	diag2 = diag2 + a[i * n + (n - i - 1)]
r = rs.pop()
c = cs.pop()
if len(rs) != 0 or len(cs) != 0 or r != c or (diag1 != diag2) or (diag1 != r):
	print('-1\n')
else:
	print(d)


import sys
n = int(input())
if 2 > n:
	print(1)
	sys.exit()
A = tuple((list(map(int, input().split())) for _ in range(n)))
s = 0
for i in range(n):
	for j in range(n):
		if not A[i][j]:
			s = 1
			break
	if s:
		break
s = sum(A[not i])
A[i][j] = x = s - sum(A[i])
if 1 > x or s != sum((A[i][i] for i in range(n))) or sum((A[i][-1 - i] for i in range(n))) != s:
	print(-1)
	sys.exit()
for i in range(n):
	if s != sum(A[i]) or sum((A[j][i] for j in range(n))) != s:
		print(-1)
		sys.exit()
print(x)


import sys
import math
from collections import Counter

def IO():
	sys.stdin = open('pyinput.txt', 'r')
	sys.stdout = open('pyoutput.txt', 'w')

def GCD(a, b):
	if b == 0:
		return a
	else:
		return GCD(b, a % b)

def LCM(a, b):
	return a * (b // GCD(a, b))

def scan(TYPE_1, TYPE_2=0):
	if TYPE_1 == int:
		return map(int, sys.stdin.readline().strip().split())
	elif TYPE_1 == float:
		return map(float, sys.stdin.readline().strip().split())
	elif TYPE_1 == list and TYPE_2 == float:
		return list(map(float, sys.stdin.readline().strip().split()))
	elif TYPE_1 == list and TYPE_2 == int:
		return list(map(int, sys.stdin.readline().strip().split()))
	elif TYPE_1 == str:
		return sys.stdin.readline().strip()
	else:
		print('ERROR!!!!')

def main():
	n = int(scan(str))
	a = []
	idx = 0
	s = []
	for i in range(n):
		x = scan(list, int)
		if 0 in x:
			idx = i
		a.append(x)
		s.append(sum(x))
	if n == 1:
		print(1)
		return
	s = list(Counter(s).keys())
	if len(s) != 2:
		print(-1)
		return
	k = max(s[0], s[1]) - min(s[0], s[1])
	for i in range(n):
		if a[idx][i] == 0:
			a[idx][i] = k
	s = []
	d1 = d2 = 0
	for i in range(n):
		r = c = 0
		for j in range(n):
			r += a[i][j]
			c += a[j][i]
		d1 += a[i][i]
		d2 += a[i][n - i - 1]
		s.append(r)
		s.append(c)
	s.append(d1)
	s.append(d2)
	if len(Counter(s).keys()) == 1:
		print(k)
	else:
		print(-1)
main()


import sys
n = int(input())
matrix = []
row_sums = []
row_zero = 0
if n == 1:
	print(1)
	sys.exit()
for i in range(n):
	temp = list(map(int, input().split(' ')))
	if 0 in temp:
		row_zero = temp
	else:
		row_sums.append(sum(temp))
	matrix.append(temp)
for i in range(n):
	temp = [matrix[x][i] for x in range(n)]
	if 0 not in temp:
		if sum(temp) != row_sums[0]:
			print(-1)
			sys.exit()
if len(set(row_sums)) == 1:
	temp_sum = row_sums[0] - sum(row_zero)
	for i in range(n):
		if 0 in matrix[i]:
			matrix[i][matrix[i].index(0)] = temp_sum
	for row in matrix:
		if sum(row) == row_sums[0] and temp_sum > 0:
			if sum([matrix[x][x] for x in range(n)]) == row_sums[0] and sum([matrix[x][n - x - 1] for x in range(n)]) == row_sums[0]:
				print(temp_sum)
				sys.exit()
	print(-1)
else:
	print(-1)


def main():
	n = int(input())
	if n == 1:
		print(1)
		return
	l = list((list(map(int, input().split())) for _ in range(n)))
	for (i, r) in enumerate(l, -1):
		if 0 in r:
			break
	s = sum(l[i])
	x = s - sum(r)
	if x <= 0:
		x = -1
	r[r.index(0)] = x
	if any((sum(r) != s for r in l)) or any((sum(r) != s for r in zip(*l))) or sum((l[i][i] for i in range(n))) != s or (sum((l[i][j] for (i, j) in enumerate(range(n - 1, -1, -1)))) != s):
		x = -1
	print(x)
main()


from sys import exit

def check_all(l, exclude):
	result = 0
	(x, y) = exclude
	for i in range(n):
		if i == x:
			continue
		s = sum(l[i])
		if not result:
			result = s
		elif s != result:
			return (False, 0)
	for j in range(n):
		if j == y:
			continue
		s = sum((row[j] for row in l))
		if s != result:
			return (False, 0)
	if x != y:
		s = sum((l[i][i] for i in range(n)))
		if s != result:
			return (False, 0)
	if y != n - x - 1:
		s = sum((l[i][n - i - 1] for i in range(n)))
		if s != result:
			return (False, 0)
	return (True, result)
n = int(input())
sq = []
zero_position = None
for i in range(n):
	row = list(map(int, input().split()))
	if not zero_position:
		if 0 in row:
			zero_position = (i, row.index(0))
	sq.append(row)
(possible, s) = check_all(sq, zero_position)
if not possible:
	print(-1)
else:
	(x, y) = zero_position
	result = s - sum(sq[x])
	number = s - sum((row[y] for row in sq))
	if number != result:
		print(-1)
		exit(0)
	if x == y:
		number = s - sum((sq[i][i] for i in range(n)))
		if number != result:
			print(-1)
			exit(0)
	if y == n - x - 1:
		number = s - sum((sq[i][n - i - 1] for i in range(n)))
		if number != result:
			print(-1)
			exit(0)
	if n == 1:
		print(1)
	elif result <= 0:
		print(-1)
	else:
		print(result)


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
	elif 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())
matr = []
ind = []
m = 0
t = True
for i in range(n):
	matr.append(list(map(int, input().split(' '))))
	if sum(matr[i]) > m:
		m = sum(matr[i])
	if 0 in matr[i]:
		ind.append(i)
		ind.append(matr[i].index(0))
matr[ind[0]][ind[1]] = m - sum(matr[ind[0]])
for i in range(n):
	if sum(matr[i]) != m:
		t = False
	if sum([b[i] for b in matr]) != m:
		t = False
if sum((matr[i][i] for i in range(n))) != m:
	t = False
if sum((matr[i][n - i - 1] for i in range(n))) != m:
	t = False
if t and matr[ind[0]][ind[1]] > 0:
	print(max(matr[ind[0]][ind[1]], 1))
elif n == 1 and ind[0] == 0 and (ind[1] == 0):
	print(1)
else:
	print(-1)


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)
	exit(0)
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)


fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from re import *
from sys import *
from math import *
from heapq import *
from queue import *
from bisect import *
from string import *
from itertools import *
from collections import *
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from collections import Counter as cc
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def output(answer):
	stdout.write(str(answer))
n = int(z())
lst = []
t1 = -1
t2 = -1
ans = []
ind = -1
for i in range(n):
	arr = zzz()
	if arr.count(0):
		t1 = sum(arr)
		idx = (i, arr.index(0))
	else:
		t2 = sum(arr)
		ans.append(t2)
	lst.append(arr)
if n == 1:
	exit(print(1))
if len(set(ans)) == 1 and ans[0] - t1 > 0:
	t = ans[0] - t1
	lst[idx[0]][idx[1]] = t
	res = set()
	ma = 0
	mi = 0
	for i in range(n):
		(row, col) = (0, 0)
		for j in range(n):
			row += lst[i][j]
			col += lst[j][i]
		ma += lst[i][i]
		mi += lst[i][n - i - 1]
		res.add(row)
		res.add(col)
	res.add(mi)
	res.add(ma)
	if len(set(res)) == 1:
		print(t)
	else:
		print(-1)
else:
	print(-1)


from sys import exit
table = []
column1 = []
column2 = []
row = 0
column = 0
sums = 0
counter = 0
index = (0, 0)
dimension = int(input())
if dimension > 1:
	for i in range(dimension):
		line = [list(map(int, input().split()))]
		table += line
		if 0 in line[0]:
			row = sum(line[0])
			index = (i, line[0].index(0))
		elif counter > 0:
			if sum(line[0]) != sums:
				print(-1)
				exit()
		else:
			sums = sum(table[i])
			counter += 1
	for i in range(len(table)):
		for e in range(len(table[i])):
			if e == index[1]:
				column1.append(table[i][e])
			elif index[1] != 0 and e == 0:
				column2.append(table[i][e])
			elif index[1] == 0 and e == len(table[i]) - 1:
				column2.append(table[i][e])
	if sum(column1) + (sums - row) != sum(column2):
		print(-1)
		exit()
	diagonal1 = 0
	diagonal2 = 0
	diagonal1VAR = False
	diagonal2VAR = False
	for i in range(dimension):
		if (i, i) != index:
			diagonal1 += table[i][i]
		else:
			diagonal1VAR = True
		if (i, dimension - i - 1) != index:
			diagonal2 += table[i][dimension - i - 1]
		else:
			diagonal2VAR = True
	if diagonal1VAR and diagonal2VAR:
		if diagonal1 != row or diagonal2 != diagonal1:
			print(-1)
			exit()
	elif diagonal1VAR:
		if diagonal1 != row or diagonal2 != sums:
			print(-1)
			exit()
	elif diagonal2VAR:
		if diagonal2 != row or diagonal1 != sums:
			print(-1)
			exit()
	elif diagonal1 != sums or diagonal2 != sums:
		print(-1)
		exit()
	if sums - row > 0:
		print(sums - row)
	else:
		print(-1)
		exit()
else:
	print(1)


from sys import *
n = int(stdin.readline())
mat = []
if n == 1:
	print(1)
	exit(0)
cur = [int(z) for z in stdin.readline().split()]
mat.append(cur[:])
if 0 in cur:
	sui = sum(cur)
	k = 2
	cur = [int(z) for z in stdin.readline().split()]
	mat.append(cur[:])
	su = sum(cur)
else:
	su = sum(cur)
	k = 1
for i in range(k, n):
	mat.append([int(z) for z in stdin.readline().split()])
	if 0 in mat[-1]:
		sui = sum(mat[-1])
	elif sum(mat[-1]) != su:
		print(-1)
		exit(0)
if su <= sui:
	print(-1)
	exit(0)
k = False
for i in range(n):
	for j in range(n):
		if mat[i][j] == 0:
			mat[i][j] = su - sui
			k = True
			break
	if k:
		break
for i in range(n):
	if sum([mat[j][i] for j in range(n)]) != su:
		print(-1)
		exit(0)
if sum([mat[i][i] for i in range(n)]) != su:
	print(-1)
	exit(0)
if sum([mat[n - i - 1][i] for i in range(n)]) != su:
	print(-1)
	exit(0)
print(su - sui)


n = int(input())
M = [[int(i) for i in input().split()] for _ in range(n)]
(d1, d2) = (0, 0)
R = [0 for _ in range(n)]
C = [0 for _ in range(n)]
(mi, mj) = (0, 0)
for i in range(n):
	for j in range(n):
		if M[i][j] == 0:
			mi = i
			mj = j
		R[i] += M[i][j]
		C[j] += M[i][j]
num = 0
ans = True
su = 0
for i in range(n):
	d1 += M[i][i]
	d2 += M[i][n - i - 1]
try:
	su = R[mi - 1]
except:
	su = R[mi + 1]
for i in range(n):
	if i != mi and R[i] != su or (i != mj and C[i] != su):
		ans = False
		break
if su - R[mi] != su - C[mj]:
	ans = False
if mi == mj:
	d1 += su - R[mi]
if mj == n - mi - 1:
	d2 += su - R[mi]
if d1 != su or d2 != su:
	ans = False
if su - R[mi] <= 0:
	ans = False
if n == 1:
	print(1)
else:
	print([-1, su - R[mi]][ans])


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 check(tbl, res):
	if res <= 0:
		return -1
	s = sum(tbl[0])
	for i in range(n):
		if s != sum(tbl[i]):
			return -1
	for j in range(n):
		n_s = 0
		for i in range(n):
			n_s += tbl[i][j]
		if n_s != s:
			return -1
	n_s_1 = 0
	n_s_2 = 0
	for i in range(n):
		n_s_1 += tbl[i][i]
		n_s_2 += tbl[i][n - 1 - i]
	if n_s_1 != s or n_s_2 != s:
		return -1
	return res
n = int(input())
tbl = [[int(j) for j in input().split()] for i in range(n)]
res = -1
if n == 1:
	print(1)
else:
	for i in range(n):
		for j in range(n):
			if tbl[i][j] == 0:
				tbl[i][j] = sum(tbl[i - 1]) - sum(tbl[i])
				res = tbl[i][j]
	print(check(tbl, res))


n = int(input())
if n == 1:
	print('1')
	quit()
if n == 0:
	print('-1')
	quit()
grid = []
x_pos = y_pos = None
for i in range(n):
	line = [int(a) for a in input().split()]
	if 0 in line:
		y_pos = i
		x_pos = line.index(0)
	grid.append(line)
on_diag1 = on_diag2 = None
if x_pos == y_pos:
	on_diag1 = True
if x_pos + y_pos + 1 == n:
	on_diag2 = True
rows = [0] * n
cols = [0] * n
diags = [0] * 2
for i in range(n):
	diags[0] += grid[i][i]
	diags[1] += grid[i][n - 1 - i]
	for j in range(n):
		cols[j] += grid[i][j]
		rows[i] += grid[i][j]
f_rows = []
for (i, r) in enumerate(rows):
	if i != y_pos:
		f_rows.append(r)
f_cols = []
for (i, c) in enumerate(cols):
	if i != x_pos:
		f_cols.append(c)
f_diags = []
if not on_diag1:
	f_diags.append(diags[0])
if not on_diag2:
	f_diags.append(diags[1])

def compare(n, arr):
	for a in arr:
		if n != a:
			return False
	return True
num = f_rows[0]
if not compare(num, f_rows):
	print('-1')
	quit()
if not compare(num, f_cols):
	print('-1')
	quit()
if not compare(num, f_diags):
	print('-1')
	quit()
needed = num - rows[y_pos]
if num - cols[x_pos] != needed:
	print('-1')
	quit()
if on_diag1:
	if num - diags[0] != needed:
		print('-1')
		quit()
if on_diag2:
	if num - diags[1] != needed:
		print('-1')
		quit()
if needed <= 0:
	print('-1')
	quit()
print(needed)


n = int(input())
M = [list(map(int, input().split())) for i in range(n)]
ch = 0
if n == 1:
	print(1)
	exit()
for i in range(n):
	if 0 in M[i]:
		q = M[i].index(0)
		ch = i
		break
if n != 1:
	S = sum(M[ch]) - sum(M[ch - 1])
if S >= 0:
	print(-1)
	exit()
M[ch][q] = abs(S)
T = sum(M[0])
for i in range(1, n):
	if sum(M[i]) != T:
		print(-1)
		exit()
for i in range(n):
	G = 0
	for j in range(n):
		G = G + M[j][i]
	if G != T:
		print(-1)
		exit()
G = 0
for i in range(n):
	G = G + M[i][i]
if G != T:
	print(-1)
	exit()
f = n - 1
f1 = 0
G = 0
for i in range(n):
	G = G + M[f1][f]
	f = f - 1
	f1 = f1 + 1
if T != G:
	print(-1)
	exit()
print(abs(S))


from sys import stdin, stdout
n = int(stdin.readline())
numbers = []
label = 1
cnt = 0
for i in range(n):
	numbers.append(list(map(int, stdin.readline().split())))
	if not numbers[-1].count(0):
		cnt = sum(numbers[-1])
for i in range(n):
	if numbers[i].count(0):
		ans = cnt - sum(numbers[i])
		numbers[i][numbers[i].index(0)] = cnt - sum(numbers[i])
		break
for i in range(n):
	if not i:
		cnt = sum(numbers[i])
	elif sum(numbers[i]) != cnt:
		label = 0
for j in range(n):
	value = 0
	for i in range(n):
		value += numbers[i][j]
	if value != cnt:
		label = 0
value = 0
for i in range(n):
	value += numbers[i][i]
if value != cnt:
	label = 0
value = 0
for i in range(n):
	value += numbers[n - 1 - i][i]
if value != cnt:
	label = 0
if n == 1:
	stdout.write('1')
elif label and ans > 0:
	stdout.write(str(ans))
else:
	stdout.write('-1')


def test_square(a):
	n = len(a)
	summ = sum(a[0])
	for i in range(1, n):
		if sum(a[i]) != sum(a[0]):
			return False
	summs = [0] * n
	for i in range(n):
		for j in range(n):
			summs[j] += a[i][j]
	for elem in summs:
		if elem != summ:
			return False
	diagonals = [0, 0]
	for i in range(n):
		for j in range(n):
			if i == j:
				diagonals[0] += a[i][j]
			if i == n - j - 1:
				diagonals[1] += a[i][j]
	if diagonals[0] != summ or diagonals[1] != summ:
		return False
	return True
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
if n == 1:
	print(1)
else:
	index = [0, 0]
	f = False
	for i in range(n):
		for j in range(n):
			if not a[i][j]:
				index = [i, j]
				f = True
				break
		if f:
			break
	summ = sum(a[index[0] - 1])
	summ2 = sum(a[index[0]])
	ans = summ - summ2
	a[index[0]][index[1]] = ans
	if ans <= 0:
		print(-1)
		exit()
	if test_square(a):
		print(ans)
	else:
		print(-1)


n = int(input())
matrix = [[int(i) for i in input().split()] for j in range(n)]
for i in range(n):
	for j in range(n):
		if matrix[i][j] == 0:
			(r, c) = (i, j)
			break
if n > 1:
	neededSum = sum(matrix[0]) if r != 0 else sum(matrix[1])
else:
	neededSum = 1
matrix[r][c] = neededSum - sum(matrix[r])
partialSums = [0] * (2 * (1 + n))
for i in range(n):
	partialSums[0] += matrix[i][i]
	partialSums[1] += matrix[i][n - i - 1]
	for j in range(n):
		partialSums[i + 2] += matrix[i][j]
		partialSums[i + 2 + n] += matrix[j][i]
if matrix[r][c] > 0 and len(set(partialSums)) == 1:
	print(matrix[r][c])
else:
	print(-1)


def rowsum(x):
	return [sum(i) for i in x]

def colsum(x):
	return list(map(sum, zip(*x)))

def diagsum(x):
	n = len(x)
	d1 = sum([x[i][i] for i in range(n)])
	d2 = sum([x[i][abs(n - i) - 1] for i in range(n)])
	return [d1, d2]

def ismagic(x):
	return len(set(rowsum(x) + colsum(x) + diagsum(x))) == 1
n = int(input())
if n == 1:
	print(1)
else:
	square = []
	for i in range(n):
		row = [int(j) for j in input().split()]
		square.append(row)
		mask = [j == 0 for j in row]
		if any(mask):
			(r, c) = (i, mask.index(True))
	if ismagic(square):
		print(-1)
	else:
		sums = rowsum(square)
		magic = list(set(sums) - set([sums[r]]))
		magic = magic[0]
		square[r][c] = magic - sums[r]
		if not ismagic(square) or magic - sums[r] <= 0:
			print(-1)
		else:
			print(magic - sums[r])


n = int(input())
m = []
r = 0
c = True
s = 0
for i in range(n):
	m.append([int(x) for x in input().split()])
if n > 1:
	for i in range(n):
		found = False
		for j in range(n):
			if m[i][j] == 0:
				if i == n - 1:
					m[i][j] = sum(m[i - 1]) - sum(m[i])
				else:
					m[i][j] = sum(m[i + 1]) - sum(m[i])
				r = m[i][j]
				found = True
				break
		if found:
			break
else:
	m[0][0] = 1
	r = m[0][0]
s = sum(m[0])
if c == True:
	for i in range(1, n):
		if sum(m[i]) != s:
			c = False
			break
mt = list(map(list, zip(*m)))
if c == True:
	for i in range(n):
		if sum(mt[i]) != s:
			c = False
			break
if c == True:
	st = 0
	for i in range(n):
		st += m[i][i]
	if st != s:
		c = False
if c == True:
	st = 0
	for i in range(n):
		st += m[i][n - 1 - i]
	if st != s:
		c = False
if c == False or r <= 0:
	r = -1
print(r)


from __future__ import division, print_function
import math
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def check(l):
	sdiag1 = sdiag2 = 0
	l1 = []
	l2 = []
	l3 = []
	l4 = []
	n = len(l)
	for i in range(n):
		srow = scol = 0
		for j in range(n):
			srow += l[j][i]
			scol += l[i][j]
		l1.append(srow)
		l2.append(scol)
	for i in range(n):
		sdiag1 += l[i][i]
		sdiag2 += l[n - i - 1][i]
	l3.append(sdiag1)
	l4.append(sdiag2)
	if set(l1) == set(l2):
		if set(l2) == set(l3):
			if set(l3) == set(l4):
				return True
	return False
ttt = int(input())
l = []
for _ in range(ttt):
	l1 = list(map(int, input().split()))
	l = l + [l1]
ke = 0
for i in range(ttt):
	for j in range(ttt):
		if l[i][j] == 0:
			i1 = i
			j1 = j
			ke = 1
			break
	if ke == 1:
		break
for i in range(ttt):
	if i != i1:
		su = sum(l[i])
	else:
		sut = sum(l[i])
if len(l) != 1:
	l[i1][j1] = su - sut
	if su - sut > 0:
		if su - sut == 0:
			print(-1)
		elif check(l):
			print(su - sut)
		else:
			print(-1)
	else:
		print(-1)
else:
	print(1)


from sys import *
n = int(input())
mat = []
if n == 1:
	print(1)
	exit(0)
cur = [int(z) for z in input().split()]
mat.append(cur[:])
if 0 in cur:
	sui = sum(cur)
	k = 2
	cur = [int(z) for z in input().split()]
	mat.append(cur[:])
	su = sum(cur)
else:
	su = sum(cur)
	k = 1
for i in range(k, n):
	mat.append([int(z) for z in input().split()])
	if 0 in mat[-1]:
		sui = sum(mat[-1])
if su <= sui:
	print(-1)
	exit(0)
for i in range(n):
	for j in range(n):
		if mat[i][j] == 0:
			mat[i][j] = su - sui
for i in mat:
	if sum(i) != su:
		print(-1)
		exit(0)
for i in range(n):
	if sum([mat[j][i] for j in range(n)]) != su:
		print(-1)
		exit(0)
if sum([mat[i][i] for i in range(n)]) != su:
	print(-1)
	exit(0)
if sum([mat[n - i - 1][i] for i in range(n)]) != su:
	print(-1)
	exit(0)
print(su - sui)


def sumLine(arr, x):
	total = 0
	for i in range(N):
		total += arr[x][i]
	return total

def sumRow(arr, y):
	total = 0
	for i in range(N):
		total += arr[i][y]
	return total

def checkInclined(arr, baseTotal):
	N = len(arr)
	left = 0
	right = 0
	for i in range(N):
		left += arr[i][i]
		right += arr[i][N - i - 1]
	if left == right and left == baseTotal and (right == baseTotal):
		return True
	else:
		return False

def findZero(arr):
	for i in range(len(arr)):
		for j in range(len(arr)):
			if arr[i][j] == 0:
				return (i, j)
N = int(input())
arr = []
data = []
for i in range(N):
	inputs = input().split(' ')
	arr.append(inputs)
for i in range(N):
	for j in range(N):
		arr[i][j] = int(arr[i][j])
validateLine = False
validateRow = False
countLine = 0
countRow = 0
if N == 1:
	print(1)
else:
	(zeroX, zeroY) = findZero(arr)
	baseTotal = 0
	for i in range(N):
		if i != zeroX:
			baseTotal = sumLine(arr, i)
			break
	subtrahend = 0
	for i in range(N):
		if i != zeroY:
			subtrahend += arr[zeroX][i]
	arr[zeroX][zeroY] = baseTotal - subtrahend
	if arr[zeroX][zeroY] > 0:
		for i in range(N):
			if baseTotal == sumLine(arr, i):
				countLine += 1
		if countLine == N:
			validateLine = True
		for i in range(N):
			if baseTotal == sumRow(arr, i):
				countRow += 1
		if countRow == N:
			validateRow = True
	if validateLine and validateRow and checkInclined(arr, baseTotal):
		print(arr[zeroX][zeroY])
	else:
		print(-1)


import sys
n = int(sys.stdin.readline())
inps = []
zero = False
for i in range(n):
	inps.append(sys.stdin.readline()[:-1].split(' '))
	if zero == False:
		for j in range(len(inps[i])):
			if inps[i][j] == '0':
				zero = [i, j]
				break
ans = -1
if n == 1:
	ans = 1
else:
	zeroSum = 0
	for el in inps[zero[0]]:
		el = int(el)
		zeroSum = zeroSum + el
	line = zero[0] - 1
	if line < 0:
		line = zero[0] + 1
	sumLine = 0
	for el in inps[line]:
		el = int(el)
		sumLine = sumLine + el
	zeroValue = sumLine - zeroSum
	if zeroValue <= 0:
		ans = -1
	else:
		ans = zeroValue
		inps[zero[0]][zero[1]] = zeroValue
		lastSum = sumLine
		for i in range(n):
			tmpSum = 0
			for el in inps[i]:
				el = int(el)
				tmpSum = tmpSum + el
			if tmpSum != lastSum:
				ans = -1
				break
			lastSum = tmpSum
		if ans != -1:
			for i in range(n):
				tmpSum = 0
				for j in range(n):
					el = int(inps[j][i])
					tmpSum = tmpSum + el
				if tmpSum != lastSum:
					ans = -1
					break
				lastSum = tmpSum
			tmpSum = 0
			tmpSum2 = 0
			for i in range(n):
				el = int(inps[i][i])
				tmpSum = tmpSum + el
				el = int(inps[i][n - i - 1])
				tmpSum2 = tmpSum2 + el
			if tmpSum != lastSum:
				ans = -1
			if tmpSum2 != lastSum:
				ans = -1
print(ans)


def main():
	n = int(input())
	if n == 1:
		print(1)
		return
	m = []
	for i in range(n):
		l = list(map(int, input().split()))
		m += [l]
		for j in range(n):
			if l[j] == 0:
				istar = i
				jstar = j
	if istar > 0:
		ii = istar - 1
	else:
		ii = istar + 1
	magic = 0
	for j in range(n):
		magic += m[ii][j]
	sstar = magic
	for j in range(n):
		sstar -= m[istar][j]
	m[istar][jstar] = sstar
	for i in range(n):
		t = 0
		for j in range(n):
			t += m[i][j]
		if t != magic:
			print(-1)
			return
	for j in range(n):
		t = 0
		for i in range(n):
			t += m[i][j]
		if t != magic:
			print(-1)
			return
	t = 0
	for i in range(n):
		t += m[i][i]
	if t != magic:
		print(-1)
		return
	t = 0
	for i in range(n):
		t += m[i][n - 1 - i]
	if t != magic:
		print(-1)
		return
	if sstar <= 0:
		print(-1)
		return
	print(sstar)
main()


n = int(input())
jadval = []
for i in range(n):
	jadval.append([int(x) for x in input().split()])
for i in range(n):
	for j in range(n):
		if jadval[i][j] == 0:
			a = i
			b = j
			break
if a != 0:
	s = sum(jadval[0])
	jadval[a][b] = s - sum(jadval[a])
elif a == 0 and n == 1:
	print(1)
	exit()
else:
	s = sum(jadval[1])
	jadval[a][b] = s - sum(jadval[a])
if jadval[a][b] <= 0:
	print(-1)
	exit()
s = sum(jadval[0])
for i in range(1, n):
	if s != sum(jadval[i]):
		print(-1)
		exit()
for i in range(n):
	q = 0
	for j in range(n):
		q += jadval[j][i]
	if s != q:
		print(-1)
		exit()
q = 0
for i in range(n):
	q += jadval[i][i]
if q != s:
	print(-1)
	exit()
q = 0
for i in range(n):
	q += jadval[i][n - i - 1]
if q != s:
	print(-1)
	exit()
print(jadval[a][b])


n = int(input())
l_c = [list() for _ in range(n)]
s_l = None
j = None
k = None
if n == 1:
	print(1)
	quit()
for i in range(n):
	l_c[i] = list(map(int, input().split()))
	if 0 in l_c[i]:
		j = i
		k = l_c[i].index(0)
	elif s_l:
		if s_l != sum(l_c[i]):
			print(-1)
			quit()
	else:
		s_l = sum(l_c[i])
l_c[j][k] = s_l - sum(l_c[j])
if l_c[j][k] <= 0:
	print(-1)
	quit()
for c in range(n):
	if sum((l_c[r][c] for r in range(n))) != s_l:
		print(-1)
		quit()
if not sum((l_c[r][r] for r in range(n))) == s_l == sum((l_c[r][n - 1 - r] for r in range(n))):
	print(-1)
	quit()
else:
	print(l_c[j][k])


n = int(input())
if n == 1:
	print(1)
	exit()
a = [list(map(int, input().split())) for i in range(n)]
(s, r) = ([sum(ai) for ai in a], [0 in ai for ai in a].index(True))
(x, y) = (s[r], s[(r + 1) % n])
if x >= y or not all((i == r or si == y for (i, si) in enumerate(s))):
	print(-1)
else:
	a[r][a[r].index(0)] = y - x
	fc = all((sum((a[i][j] for i in range(n))) == y for j in range(n)))
	fld = sum((a[i][i] for i in range(n))) == y
	frd = sum((a[i][n - i - 1] for i in range(n))) == y
	print(y - x if fc and fld and frd else -1)


n = int(input())
m = [list(map(int, input().split())) for _ in range(n)]
if n == 1:
	print(1)
	quit()
x = 0
while 0 not in m[x]:
	x += 1
col = m[x].index(0)
k = 0
if x == 0:
	k = 1
val = sum(m[0 + k]) - sum(m[x])
m[x][col] = val
a = [0] * n
b = [0] * n
c = [0] * 2
for i in range(n):
	c[0] += m[i][i]
	c[1] += m[i][n - 1 - i]
	for j in range(n):
		a[i] += m[i][j]
		b[i] += m[j][i]
ls = a + b + c
if min(ls) == max(ls) and val > 0:
	print(val)
else:
	print(-1)


n = int(input())
(l, index1, index2, flag) = ([], -1, -1, True)
for i in range(n):
	s = list(map(int, input().split()))
	if flag == True and 0 in s:
		index1 = i
		index2 = s.index(0)
		flag = False
	l.append(s)
(s1, s2) = (0, 0)
if n == 1 and l[0][0] == 0:
	print(1)
	exit(0)
for i in range(n):
	if i == index1:
		continue
	s1 = sum(l[i])
	break
for i in range(n):
	if i == index2:
		continue
	for j in range(n):
		s2 += l[j][i]
	break
if s1 == s2:
	x = s1 - sum(l[index1])
	if x <= 0:
		print(-1)
		exit(0)
	l[index1][index2] = x
	(s1, s2, s3, s4, m1, m2) = (0, 0, 0, 0, [], [])
	for i in range(n):
		m1.append(sum(l[i]))
	y = list(set(m1))
	if len(y) == 1:
		s1 = y[0]
	else:
		print(-1)
		exit(0)
	for i in range(n):
		s2 = 0
		for j in range(n):
			s2 += l[j][i]
		m2.append(s2)
	z = list(set(m2))
	if len(z) == 1:
		s2 = z[0]
	else:
		print(-1)
		exit(0)
	for i in range(n):
		s3 += l[i][i]
	j = n - 1
	for i in range(n):
		s4 += l[i][j]
		j -= 1
	if s1 == s2 == s3 == s4:
		print(x)
	else:
		print(-1)
else:
	print(-1)


import sys
x = int(input())
mat = []
sums = [0] * x
r = 0
c = 0
for i in range(x):
	a = list(map(int, sys.stdin.readline().split()))
	mat.append(a)
	sums[i] = sum(a)
	for j in range(x):
		if mat[i][j] == 0:
			r = i
			c = j
if r == 0:
	if x == 1:
		ans = 1
	else:
		ans = sums[1] - sums[r]
else:
	ans = sums[0] - sums[r]
mat[r][c] = ans
ok = True
if ans <= 0:
	ok = False
if ok:
	s = sum(mat[0])
	for i in range(x):
		si = sum(mat[i])
		if si != s:
			ok = False
			break
if ok:
	for i in range(x):
		si = 0
		for j in range(x):
			si += mat[j][i]
		if si != s:
			ok = False
			break
if ok:
	si = 0
	for i in range(x):
		si += mat[i][i]
	if si != s:
		ok = False
if ok:
	si = 0
	for i in range(x):
		si += mat[i][x - i - 1]
	if si != s:
		ok = False
if ok:
	print(ans)
else:
	print(-1)


n = int(input())
f = [list(map(int, input().split())) for i in range(n)]
d1 = 0
d2 = 0
str_sum = [0] * n
col_sum = [0] * n
i1 = -1
j1 = -1
sum1 = -1
sum2 = -1
mistake = False
if n == 1:
	print(1)
else:
	for i in range(n):
		for j in range(n):
			col_sum[j] += f[i][j]
			if f[i][j] == 0:
				i1 = i
				j1 = j
		if i != i1:
			str_sum[i] = sum(f[i])
			if sum1 == -1:
				sum1 = str_sum[i]
			elif str_sum[i] != sum1:
				mistake = True
				break
		else:
			str_sum[i] = sum(f[i])
		d1 += f[i][i]
		d2 += f[i][n - i - 1]
	ans = sum1 - str_sum[i1]
	for j in range(n):
		if j != j1 and col_sum[j] != sum1 or (j == j1 and col_sum[j] != sum1 - ans):
			mistake = True
			break
		if j == i1 == j1:
			d1 += ans
		if j == i1 and j1 == n - j - 1:
			d2 += ans
	if mistake or sum1 != d1 or sum1 != d2 or (ans == 0) or (ans < 0):
		print(-1)
	else:
		print(ans)


n = int(input())
rows = [list(map(int, input().split())) for x in range(n)]
columns = [[y[x] for y in rows] for x in range(n)]
d1 = [rows[x][x] for x in range(n)]
d2 = [rows[x][n - 1 - x] for x in range(n)]
condition = True
for x in rows:
	if 0 in x:
		try:
			if sum(x) != t:
				condition = False
				break
		except NameError:
			t = sum(x)
	else:
		try:
			if sum(x) != s:
				condition = False
				break
		except NameError:
			s = sum(x)
if condition:
	for x in columns:
		if 0 in x:
			if sum(x) != t:
				condition = False
				break
		elif sum(x) != s:
			condition = False
			break
if condition:
	if 0 in d1:
		if sum(d1) != t:
			condition = False
	elif sum(d1) != s:
		condition = False
	if 0 in d2:
		if sum(d2) != t:
			condition = False
	elif sum(d2) != s:
		condition = False
if condition:
	if n == 1:
		print(1)
	else:
		for x in rows:
			if 0 in x:
				a = x
			else:
				b = x
		c = sum(b) - sum(a)
		if c <= 0:
			print(-1)
		else:
			print(c)
else:
	print(-1)


def main():
	n = int(input())
	mx = [[int(x) for x in input().split()] for _ in range(n)]
	(i, j) = (-1, -1)
	if n == 1:
		print(1)
		return
	for ii in range(n):
		if 0 in mx[ii]:
			i = ii
			j = mx[i].index(0)
			break
	sr = [sum(x) for x in mx]
	sc = [sum(x) for x in zip(*mx)]
	sd1 = sum((mx[i][i] for i in range(n)))
	sd2 = sum((mx[i][n - i - 1] for i in range(n)))
	ii = 0 if i != 0 else 1
	d = sr[ii] - sr[i]
	if d <= 0 or d > 10 ** 18:
		print(-1)
		return
	sr[i] += d
	sc[j] += d
	if i == j:
		sd1 += d
	if j == n - i - 1:
		sd2 += d
	if len(set(sr + sc + [sd1, sd2])) > 1:
		print(-1)
	else:
		print(d)
main()


def bad():
	print(-1)
	exit(0)
MX_SZ = 512
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
x = 0
y = 0
for i in range(0, n):
	for j in range(0, n):
		if a[i][j] == 0:
			x = i
			y = j
if n == 1:
	print(1)
	exit(0)
rsum = 0
for i in range(0, n):
	if i == x:
		continue
	for j in range(0, n):
		rsum += a[i][j]
	break
csum = 0
for i in range(0, n):
	csum += a[i][y]
if csum >= rsum:
	bad()
a[x][y] = rsum - csum
for i in range(0, n):
	vsum = 0
	hsum = 0
	for j in range(0, n):
		vsum += a[i][j]
		hsum += a[j][i]
	if vsum != rsum or hsum != rsum:
		bad()
dsum = 0
rdsum = 0
for i in range(0, n):
	dsum += a[i][i]
	rdsum += a[i][n - i - 1]
if dsum != rsum or rdsum != rsum:
	bad()
print(a[x][y])


def check(arr, n):
	S = list(map(sum, arr))
	S += list(map(sum, list(zip(*arr))))
	S.append(sum([arr[i][i] for i in range(n)]))
	S.append(sum([arr[n - 1 - i][i] for i in range(n)]))
	return not any([s - S[0] for s in S])
n = int(input())
arr = []
for i in range(n):
	arr.append(list(map(int, input().split())))
	for j in range(n):
		if arr[i][j] == 0:
			(r, c) = (i, j)
if n == 1:
	print(1)
	exit(0)
arr[r][c] = sum(arr[r - 1]) - sum(arr[r])
if arr[r][c] <= 0 or not check(arr, n):
	print(-1)
else:
	print(arr[r][c])


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()


def get_sum(leng, cube):
	if leng == 1:
		return (0, 0, cube[0][0])
	if 0 in cube[0]:
		sum_v = sum(cube[1])
	else:
		sum_v = sum(cube[0])
	posx = -1
	for line in range(0, leng):
		if posx == -1 and 0 in cube[line]:
			posx = cube[line].index(0)
			posy = line
			continue
		if sum_v != sum(cube[line]):
			return (-1, -1, -1)
	for col in [[cube[i][ii] for i in range(leng)] for ii in range(leng)]:
		if sum_v != sum(col) and 0 not in col:
			return (-1, -1, -1)
	if posx != posy and sum_v != sum([cube[i][i] for i in range(leng)]):
		return (-1, -1, -1)
	if leng - posx - 1 != posy and sum_v != sum([cube[leng - i - 1][i] for i in range(leng)]):
		return (-1, -1, -1)
	return (posx, posy, sum_v)
leng = int(input().strip())
cube = [list(map(int, input().strip().split())) for i in range(leng)]
(posx, posy, sumv) = get_sum(leng, cube)
if posx == 0 and posy == 0 and (sumv == 0):
	print(999)
elif posx == -1 or sum(cube[posy]) != sum([cube[i][posx] for i in range(leng)]):
	print(-1)
elif posx == posy and sum(cube[posy]) != sum([cube[i][i] for i in range(leng)]):
	print(-1)
elif leng - 1 - posx == posy and sum(cube[posy]) != sum([cube[leng - 1 - i][i] for i in range(leng)]):
	print(-1)
elif sumv - sum(cube[posy]) <= 0:
	print(-1)
else:
	print(sumv - sum(cube[posy]))


n = int(input())
a = [[0 for i in range(n)] for j in range(n)]
zerorow = -1
zerocol = -1
for i in range(n):
	a[i] = [int(x) for x in input().split()]
	for j in range(n):
		if a[i][j] == 0:
			zerorow = i
			zerocol = j
if n == 1:
	print(1)
else:
	thesum = 0
	if zerorow != 0:
		thesum = sum(a[0])
	else:
		thesum = sum(a[1])
	thezero = thesum - sum(a[zerorow])
	a[zerorow][zerocol] = thezero
	good = True
	for r in a:
		if sum(r) != thesum:
			good = False
	for c in range(n):
		colsum = 0
		for r in range(n):
			colsum += a[r][c]
		if colsum != thesum:
			good = False
	diagonesum = 0
	diagtwosum = 0
	for i in range(n):
		diagonesum += a[i][i]
		diagtwosum += a[i][n - i - 1]
	if diagonesum != thesum or diagtwosum != thesum:
		good = False
	if good and thezero > 0:
		print(thezero)
	else:
		print(-1)


n = int(input())
flag = True
a = []
for i in range(n):
	b = [int(x) for x in input().split()]
	a.append(b)
	if flag:
		for j in range(n):
			if b[j] == 0:
				flag = False
				row = i
				col = j
				break
if n == 1:
	print('1')
else:
	d = set()
	e = set()
	for i in range(n):
		if i != row:
			d.add(sum(a[i]))
		else:
			e.add(sum(a[i]))
		if i != col:
			d.add(sum((a[j][i] for j in range(n))))
		else:
			e.add(sum((a[j][i] for j in range(n))))
	if len(d) == 1 and len(e) == 1:
		if row == col and row == n - col - 1:
			temp1 = sum((a[i][i] for i in range(n)))
			temp2 = sum((a[i][n - j - 1] for i in range(n)))
			temp3 = e.pop()
			if temp1 == temp2 == temp3:
				temp4 = d.pop()
				if temp4 - temp1 > 0:
					print(temp4 - temp1)
				else:
					print('-1')
			else:
				print('-1')
		elif row == col:
			e.add(sum((a[i][i] for i in range(n))))
			d.add(sum((a[i][n - i - 1] for i in range(n))))
			if len(d) == len(e) == 1:
				temp1 = d.pop()
				temp2 = e.pop()
				if temp1 - temp2 > 0:
					print(temp1 - temp2)
				else:
					print('-1')
			else:
				print('-1')
		elif row == n - col - 1:
			d.add(sum((a[i][i] for i in range(n))))
			e.add(sum((a[i][n - i - 1] for i in range(n))))
			if len(d) == len(e) == 1:
				temp1 = d.pop()
				temp2 = e.pop()
				if temp1 - temp2 > 0:
					print(temp1 - temp2)
				else:
					print('-1')
			else:
				print('-1')
		else:
			d.add(sum((a[i][i] for i in range(n))))
			d.add(sum((a[i][n - i - 1] for i in range(n))))
			if len(d) == 1 and len(e) == 1:
				temp1 = d.pop()
				temp2 = e.pop()
				if temp1 - temp2 > 0:
					print(temp1 - temp2)
				else:
					print('-1')
			else:
				print('-1')
	else:
		print('-1')


import sys, os, io
import math, bisect, operator
(inf, mod) = (float('inf'), 10 ** 9 + 7)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
Neo = lambda : list(map(int, input().split()))
(test,) = Neo()
n = test
A = []
C = []
if test == 1:
	print(1)
	exit()
for i in range(test):
	B = Neo()
	C.append(sum(B))
	A.append(B)
(p, q) = (0, 0)
f = 0
for i in range(n):
	for j in range(n):
		if i == j:
			p += A[i][j]
			if A[i][j] == 0:
				f += 1
		if i + j == n - 1:
			q += A[i][j]
			if A[i][j] == 0:
				f += 1
A = list(zip(*A))
for i in A:
	C.append(sum(i))
C = Counter(C)
C[p] += 1
C[q] += 1
t = tuple(sorted(C.values()))
k = sorted(C.keys())
if (2, 2 * test) == t and len(C) == 2 and (f == 0) or ((3, 2 * test - 1) == t and len(C) == 2 and (f == 1)) or ((4, 2 * test - 2) == t and len(C) == 2 and (f == 2)):
	if C[k[0]] <= C[k[1]]:
		print(k[1] - k[0])
	else:
		print(-1)
else:
	print(-1)