Given a matrix of size n x n such that it has only one 0, Find the positive number (greater than zero) to be placed in place of the 0 such that sum of the numbers in every row, column and two diagonals become equal. If no such number exists, return -1.
Note: Diagonals should be only of the form matrix[i][i] and matrix[i][n-i-1]. n is always greater than 1. The answer can be greater than 10^{9}. 
 
Example 1:
Input: matrix = {{5, 5}, {5, 0}}
Output: 5
Explanation: The matrix is
5 5
5 0
Therefore If we place 5 instead of 0, all
the element of matrix will become 5. 
Therefore row 5+5=10, column 5+5=10 and 
diagonal 5+5=10, all are equal.
Example 2:
Input: matrix = {{1, 2, 0}, {3, 1, 2}, 
{2, 3, 1}}
Output: -1
Explanation: It is not possible to insert 
an element in place of 0 so that the 
condition is satisfied.thus result is -1. 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function MissingNo() which takes the matrix as input parameter and returns the number which should be placed in place of 0 such that the condition gets satisfied. If not possible return -1.
 
Expected Time Complexity: O(n * n)
Expected Space Complexity: O(2 * n)
 
Constraints:
2 <= n <= 1000
1 <= elements in the matrix <= 10^9

Difficulty: MEDIUM
Tags: Matrices

Data structures
Skill types: Data structures
Expected Time Complexity: O(n * n)
Expected Auxiliary Space: O(2 * n)

Starter code:
#User function Template for python3



class Solution:

	def MissingNo(self, matrix):

		# Code here

Solutions:
class Solution:

	def MissingNo(self, matrix):
		num = 0
		(s, s1) = (0, 0)
		for i in matrix:
			if 0 not in i:
				s = sum(i)
				break
		for (i, j) in enumerate(matrix):
			if 0 in j:
				s1 = sum(j)
				matrix[i][j.index(0)] = s - s1
				num = s - s1
				break
		l = []
		for i in matrix:
			l.append(sum(i))
		i = 0
		while i < len(matrix):
			s = 0
			for j in matrix:
				s += j[i]
			l.append(s)
			i += 1
		(s, s1) = (0, 0)
		for i in range(len(matrix)):
			for j in range(len(matrix)):
				if i == j:
					s += matrix[i][j]
				if i + j == len(matrix) - 1:
					s1 += matrix[i][j]
		l.append(s)
		l.append(s1)
		if len(set(l)) == 1 and num > 0:
			return num
		return -1


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix[0])
		indcol = 0
		indrow = 0
		res = []
		for i in range(n):
			for j in range(n):
				if matrix[i][j] == 0:
					indcol = j
					indrow = i
		for i in range(n):
			res.append(sum(matrix[i]))
		res = list(set(res))
		if len(res) > 2:
			return -1
		else:
			res1 = []
			valmax = max(res)
			valmin = min(res)
			matrix[indrow][indcol] = valmax - valmin
			for i in range(n):
				sum3 = 0
				for j in range(n):
					sum3 += matrix[j][i]
				res1.append(sum3)
			res1 = list(set(res1))
			if len(res1) > 1:
				return -1
			else:
				sum1 = 0
				sum2 = 0
				for i in range(n):
					sum1 += matrix[i][i]
					sum2 += matrix[i][n - i - 1]
				if sum1 == sum2:
					if sum1 == res1[0]:
						if matrix[indrow][indcol] == 0:
							return -1
						else:
							return matrix[indrow][indcol]
					else:
						return -1
				else:
					return -1


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		rowsum = []
		colsum = []
		for i in range(n):
			s = 0
			p = 0
			for j in range(n):
				s = s + matrix[i][j]
				if matrix[i][j] == 0:
					x = i
					y = j
			rowsum.append(s)
		d = 0
		c = 0
		r = max(rowsum) - min(rowsum)
		matrix[x][y] = r
		for i in range(n):
			s = 0
			for j in range(n):
				s = s + matrix[j][i]
			colsum.append(s)
		colsum = list(set(colsum))
		d = 0
		c = 0
		for i in range(n):
			d = d + matrix[i][i]
			c = c + matrix[i][n - i - 1]
		if len(colsum) == 1 and d == c and (r != 0):
			return r
		else:
			return -1


class Solution:

	def MissingNo(self, matrix):
		arr = []
		arr0 = []
		for i in range(n):
			row = [matrix[i][j] for j in range(n)]
			col = [matrix[j][i] for j in range(n)]
			if 0 not in row:
				arr.append(sum(row))
			else:
				arr0.append(sum(row))
			if 0 not in col:
				arr.append(sum(col))
			else:
				arr0.append(sum(col))
		for i in range(len(arr) - 2):
			if arr[i] != arr[i + 1]:
				return -1
			else:
				continue
		for i in range(len(arr0) - 2):
			if arr0[i] != arr[i + 1]:
				return -1
			else:
				continue
		for i in arr:
			for j in arr0:
				if j >= i:
					return -1
				else:
					continue
		d1 = []
		d2 = []
		for i in range(n):
			d1.append(matrix[i][i])
			d2.append(matrix[n - i - 1][i])
		if 0 in d1:
			if sum(d1) == arr0[0]:
				pass
			else:
				return -1
		elif sum(d1) == arr[0]:
			pass
		else:
			return -1
		if 0 in d2:
			if sum(d2) == arr0[0]:
				pass
			else:
				return -1
		elif sum(d2) == arr[0]:
			pass
		else:
			return -1
		return arr[0] - arr0[0]


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		res = []
		(a, b) = (0, 0)
		(sum1, sum2, sum3, sum4) = (0, 0, 0, 0)
		for i in range(n):
			sum1 = 0
			for j in range(n):
				sum1 = sum1 + matrix[i][j]
			res.append(sum1)
		for i in range(n):
			for j in range(n):
				if matrix[i][j] == 0:
					(a, b) = (i, j)
		if max(res) == min(res):
			return -1
		matrix[a][b] = max(res) - min(res)
		res = []
		for i in range(n):
			sum1 = 0
			for j in range(n):
				sum1 = sum1 + matrix[i][j]
			res.append(sum1)
		for j in range(n):
			sum2 = 0
			for i in range(n):
				sum2 = sum2 + matrix[i][j]
			res.append(sum2)
		for i in range(n):
			sum3 = sum3 + matrix[i][i]
		res.append(sum3)
		for i in range(n):
			sum4 = sum4 + matrix[i][n - i - 1]
		res.append(sum4)
		for k in range(len(res)):
			if res[0] == res[k]:
				f = 1
			else:
				f = 0
				break
		if f == 1:
			return matrix[a][b]
		else:
			return -1


class Solution:

	def MissingNo(self, matrix):
		nonzero = []
		zero = []
		for i in range(len(matrix)):
			f = True
			sum = 0
			for j in range(len(matrix[i])):
				sum += matrix[i][j]
				if not matrix[i][j]:
					f = False
			if f:
				nonzero.append(sum)
			else:
				zero.append(sum)
		for j in range(len(matrix[0])):
			f = True
			sum = 0
			for i in range(len(matrix)):
				sum += matrix[i][j]
				if not matrix[i][j]:
					f = False
			if f:
				nonzero.append(sum)
			else:
				zero.append(sum)
		f = True
		sum = 0
		for i in range(len(matrix)):
			sum += matrix[i][i]
			if not matrix[i][i]:
				f = False
		if f:
			nonzero.append(sum)
		else:
			zero.append(sum)
		f = True
		sum = 0
		i = 0
		j = len(matrix[0]) - 1
		while i < len(matrix) and j >= 0:
			sum += matrix[i][j]
			if not matrix[i][j]:
				f = False
			i += 1
			j -= 1
		if f:
			nonzero.append(sum)
		else:
			zero.append(sum)
		if len(set(zero)) == 1 and len(set(nonzero)) == 1:
			x = zero[0]
			y = nonzero[0]
			if x >= y:
				return -1
			return y - x
		return -1


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix[0])
		temp = []
		for i in matrix:
			temp.append(sum(i))
		ptr = max(temp) - min(temp)
		for i in range(n):
			for j in range(n):
				if matrix[i][j] == 0:
					matrix[i][j] = ptr
		res = set()
		for i in matrix:
			res.add(sum(i))
		s = 0
		for j in range(n):
			for i in matrix:
				s += i[j]
			res.add(s)
			s = 0
		for i in range(n):
			for j in range(n):
				if i == j:
					s += matrix[i][j]
		res.add(s)
		mat = []
		s = 0
		for i in matrix:
			mat.append(i[::-1])
		for i in range(n):
			for j in range(n):
				if i == j:
					s += mat[i][j]
		res.add(s)
		if len(res) == 1 and ptr != 0:
			return ptr
		else:
			return -1


def sod(m, n):
	a = 0
	b = 0
	j = 0
	for i in m:
		a += i[j]
		b += i[-j - 1]
		j += 1
	return (a, b)

def s(m):
	for i in m:
		if 0 not in i:
			return sum(i)

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

def sp(m, p):
	return sum(m[p])

def sq(m, q):
	s = 0
	for i in m:
		s += i[q]
	return s

def sumcheck(m, n):
	sumf = sum(m[0])
	for i in range(1, n):
		if sum(m[i]) != sumf:
			return -1
	return 1

class Solution:

	def MissingNo(self, m):
		n = len(m)
		sum1 = s(m)
		(p, q) = fp(m, n)
		sumr = sp(m, p)
		sumc = sq(m, q)
		if sumr != sumc:
			return -1
		else:
			z = sum1 - sumr
			if z < 1:
				return -1
			m[p][q] = z
			(x, y) = sod(m, n)
			final = sumcheck(m, n)
			if x == y and final == 1:
				return z
		return -1


class Solution:

	def MissingNo(self, matrix):
		l = len(matrix)
		if l == 1:
			if matrix[0][0] == 0:
				return 1
		for i in matrix:
			if 0 not in i:
				req_sum = sum(i)
				break
		for i in range(l):
			for j in range(l):
				if matrix[i][j] == 0:
					pos_num = req_sum - sum(matrix[i])
					if pos_num <= 0:
						return -1
					matrix[i][j] = pos_num
					for i in matrix:
						if sum(i) != req_sum:
							return -1
					(sum_1, sum_2) = (0, 0)
					for i in range(l):
						sum_1 += matrix[i][i]
						sum_2 += matrix[i][l - i - 1]
					if sum_1 != req_sum or sum_2 != req_sum:
						return -1
					for i in range(l):
						sum_1 = 0
						for j in range(l):
							sum_1 += matrix[i][j]
						if sum_1 != req_sum:
							return -1
					return pos_num


class Solution:

	def MissingNo(self, m):
		a = []
		for i in range(len(m)):
			if 0 in m[i]:
				ind = i
				continue
			a.append(sum(m[i]))
		if a.count(a[0]) != len(a):
			return -1
		for i in range(len(m)):
			if m[ind][i] == 0:
				m[ind][i] = a[0] - sum(m[ind])
				ans = m[ind][i]
		if ans <= 0:
			return -1
		b = []
		(d1, d2) = (0, 0)
		for i in range(len(m)):
			c = 0
			for j in range(len(m)):
				if i == j:
					d1 += m[i][j]
				if i + j == len(m) - 1:
					d2 += m[i][j]
				c += m[j][i]
			b.append(c)
		if len(set(b)) == 1 and d1 == d2 and (d1 == a[0]) and (d2 == b[0]):
			return ans
		else:
			return -1


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		(x_0, y_0) = (0, 0)
		row_red = [0] * n
		col_red = [0] * n
		left_diag_red = 0
		right_diag_red = 0
		for (i, row) in enumerate(matrix):
			left_diag_red += row[i]
			right_diag_red += row[n - i - 1]
			for (j, ele) in enumerate(row):
				if not ele:
					x_0 = i
					y_0 = j
				row_red[i] += ele
				col_red[j] += ele
		col_req_sum = col_red[y_0 - 1] if y_0 else col_red[y_0 + 1]
		row_req_sum = row_red[x_0 - 1] if x_0 else row_red[x_0 + 1]
		diff = col_req_sum - col_red[y_0]
		if diff <= 0 or diff != row_req_sum - row_red[x_0]:
			return -1
		if x_0 == y_0:
			left_diag_red += diff
		if x_0 == n - y_0 - 1:
			right_diag_red += diff
		if left_diag_red != right_diag_red:
			return -1
		row_red[x_0] += diff
		col_red[y_0] += diff
		for num in row_red:
			if num != left_diag_red:
				return -1
		for num in col_red:
			if num != right_diag_red:
				return -1
		return diff


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		l = [0] * (2 * n + 2)
		(digonal, bothDigonal) = (False, False)
		i = 0
		for row in range(n):
			for col in range(n):
				l[i] += matrix[row][col]
				l[i + 1] += matrix[col][row]
				if row == col:
					l[2 * n] += matrix[row][col]
				if row == n - col - 1:
					l[2 * n + 1] += matrix[row][col]
				if matrix[row][col] == 0:
					if row == col or row == n - col - 1:
						digonal = True
					if n % 2 and row == col == n // 2:
						bothDigonal = True
			i += 2
		mn = min(l)
		mx = max(l)
		if bothDigonal:
			if l.count(mn) == 4 and l.count(mx) == 2 * n - 2:
				return mx - mn
			return -1
		elif digonal:
			if l.count(mn) == 3 and l.count(mx) == 2 * n - 1:
				return mx - mn
			return -1
		elif l.count(mn) == 2 and l.count(mx) == 2 * n:
			return mx - mn
		else:
			return -1


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		row_sums = [0 for _ in range(n)]
		col_sums = [0 for _ in range(n)]
		diag_sums = [0 for _ in range(2)]
		for r in range(n):
			for c in range(n):
				if matrix[r][c] == 0:
					(zr, zc) = (r, c)
				else:
					row_sums[r] += matrix[r][c]
					col_sums[c] += matrix[r][c]
					if r == c:
						diag_sums[0] += matrix[r][c]
					if r + c == n - 1:
						diag_sums[1] += matrix[r][c]
		diffr = max(row_sums) - min(row_sums)
		if len(set(row_sums)) != 2 or len(set(col_sums)) != 2:
			return -1
		i = 0
		while row_sums[i] == max(row_sums):
			i += 1
		row_sums[i] += diffr
		i = 0
		while col_sums[i] == max(col_sums):
			i += 1
		col_sums[i] += diffr
		if zr == zc:
			diag_sums[0] += diffr
		if zr + zc == n - 1:
			diag_sums[1] += diffr
		if len(set(row_sums)) != 1 or len(set(col_sums)) != 1 or len(set(diag_sums)) != 1:
			return -1
		return diffr


import numpy as np

class Solution:

	def MissingNo(self, matrix):
		other_value = None
		matrix = np.array(matrix)
		indexz = np.where(matrix == 0)
		(row, col) = (indexz[0][0], indexz[1][0])
		for i in range(n):
			if i == row:
				row_sum = np.sum(matrix[i, :])
			else:
				value = np.sum(matrix[i, :])
				if other_value != None and value != other_value:
					return -1
				elif other_value == None:
					other_value = value
			if i == col:
				col_sum = np.sum(matrix[:, i])
			else:
				value = np.sum(matrix[:, i])
				if other_value != None and value != other_value:
					return -1
				elif other_value == None:
					other_value = value
		if row_sum == col_sum and row_sum < other_value:
			diff = other_value - row_sum
			diag1 = np.diagonal(matrix)
			dia_sum1 = np.sum(diag1)
			if 0 in diag1:
				dia_sum1 += diff
			diag2 = np.diagonal(np.fliplr(matrix))
			dia_sum2 = np.sum(diag2)
			if 0 in diag2:
				dia_sum2 += diff
			if row_sum + diff == dia_sum1 == dia_sum2 == other_value:
				return diff
		return -1
		print(other_value, row_sum, col_sum)


class Solution:

	def MissingNo(self, matrix):
		store1 = 0
		r = []
		flag = False
		for i in range(len(matrix)):
			s = 0
			for j in range(len(matrix[0])):
				if matrix[i][j] == 0:
					flag = True
				else:
					s = s + matrix[i][j]
			if flag == False:
				r.append(s)
			else:
				store1 = s
				flag = False
		store2 = 0
		c = []
		flag = False
		for i in range(len(matrix[0])):
			s = 0
			for j in range(len(matrix)):
				if matrix[j][i] == 0:
					flag = True
				else:
					s = s + matrix[j][i]
			if flag == False:
				c.append(s)
			else:
				store2 = s
				flag = False
		store3 = 0
		store4 = 0
		d = []
		s = 0
		flag = False
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if i == j:
					if matrix[i][j] == 0:
						flag = True
					else:
						s = s + matrix[i][j]
		if flag == False:
			d.append(s)
		else:
			store3 = s
		flag = False
		s = 0
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if i + j == len(matrix) - 1:
					if matrix[i][j] == 0:
						flag = True
					else:
						s = s + matrix[i][j]
		if flag == False:
			d.append(s)
		else:
			store4 = s
		l = []
		for i in r:
			l.append(i)
		for j in c:
			l.append(j)
		for i in d:
			l.append(i)
		if l.count(l[0]) != len(l):
			return -1
		else:
			a = l[0] - store1
			b = l[0] - store2
			if a != b:
				return -1
			elif store3 == 0 and store4 == 0:
				if a <= 0:
					return -1
				return a
			elif store3 == 0 and store4 != 0:
				c = l[0] - store4
				if c == a:
					if a <= 0:
						return -1
					return a
				else:
					return -1
			elif store4 == 0 and store3 != 0:
				d = l[0] - store3
				if d == a:
					if a <= 0:
						return -1
					return a
				else:
					return -1
			else:
				d = l[0] - store3
				c = l[0] - store4
				if c == d and c == a:
					if a <= 0:
						return -1
					return a
				else:
					return -1


class Solution:

	def MissingNo(self, m):
		n = len(m)
		ans = [-1, -1, -1, -1]
		pris = -1
		zrow = [-1, -1]
		for i in range(n):
			flag = False
			curs = 0
			for j in range(n):
				if m[i][j] == 0:
					flag = True
				curs += m[i][j]
			if flag == False:
				if pris != -1 and curs != pris:
					return -1
				pris = curs
			else:
				zrow = [i, j]
				ans[0] = curs
		for j in range(n):
			curs = 0
			flag = False
			for i in range(n):
				if m[i][j] == 0:
					flag = True
				curs += m[i][j]
			if flag:
				ans[1] = curs
			else:
				if curs != pris:
					return -1
				pris = curs
		flag = False
		d1 = 0
		for i in range(n):
			if m[i][i] == 0:
				flag = True
			d1 += m[i][i]
		if flag == False:
			if d1 != pris:
				return -1
		else:
			ans[2] = d1
		flag = False
		d2 = 0
		for i in range(n):
			if m[i][n - 1 - i] == 0:
				flag = True
			d2 += m[i][n - 1 - i]
		if flag == False:
			if pris != d2:
				return -1
		else:
			ans[3] = d2
		porans = -1
		for i in ans:
			if i == -1:
				continue
			orans = pris - i
			if porans != -1:
				if porans != orans:
					return -1
			porans = orans
		return orans if orans > 0 else -1


class Solution:

	def MissingNo(self, matrix):
		sum_without_zero = 0
		sum_with_zero = 0
		isZero = False
		(row, cl) = (None, None)
		for i in range(len(matrix)):
			cur_sum = 0
			for j in range(len(matrix)):
				cur_sum += matrix[i][j]
				if matrix[i][j] == 0:
					isZero = True
					(row, cl) = (i, j)
			if isZero == True:
				sum_with_zero = cur_sum
				isZero = False
			else:
				if sum_without_zero > 0 and cur_sum != sum_without_zero:
					return -1
				sum_without_zero = cur_sum
		matrix[row][cl] = sum_without_zero - sum_with_zero
		check1 = self.checkdig(matrix)
		check2 = self.checkup(matrix)
		if check1[0] and check2[0] and (check1[1] == check2[1] == sum_without_zero) and (matrix[row][cl] > 0):
			return matrix[row][cl]
		else:
			return -1

	def checkdig(self, matrix):
		cur_sum1 = 0
		for i in range(len(matrix)):
			cur_sum1 += matrix[i][i]
		cur_sum2 = 0
		for j in range(len(matrix)):
			cur_sum2 += matrix[j][len(matrix) - 1 - j]
		if cur_sum1 != cur_sum2:
			return (False, 0)
		else:
			return (True, cur_sum2)

	def checkup(self, matrix):
		prev = None
		for i in range(len(matrix)):
			cur_sum = 0
			for j in range(len(matrix)):
				cur_sum += matrix[j][j]
			if prev == None:
				prev = cur_sum
			elif prev != cur_sum:
				return (False, 0)
		return (True, cur_sum)


class Solution:

	def MissingNo(self, m):
		ind = 0
		s = set()
		for i in m:
			if 0 in i:
				ind = m.index(i)
			else:
				s.add(sum(i))
		if len(s) > 1:
			return -1
		r = abs(sum(m[ind]) - sum(m[ind - 1]))
		n = m[ind]
		a = n.index(0)
		m[ind][a] = r
		x = list(zip(*m))
		sx = set()
		for i in x:
			sx.add(sum(i))
		if len(sx) > 1:
			return -1
		l = []
		le = len(m)
		for i in range(le):
			for j in range(le):
				if i == j:
					l.append(m[i][j])
		x = sum(l)
		l = []
		mat = []
		for i in m:
			i.reverse()
			mat.append(i)
		for i in range(le):
			for j in range(le):
				if i == j:
					l.append(mat[i][j])
		y = sum(l)
		if x == y and r > 0:
			return r
		return -1


class Solution:

	def MissingNo(self, matrix):
		(n, r, c) = (len(matrix), 0, 0)
		found = False
		while r < n:
			c = 0
			while c < n:
				if matrix[r][c] == 0:
					found = True
					break
				c += 1
			if found:
				break
			r += 1
		if r - 1 >= 0:
			d = sum(matrix[r - 1]) - sum(matrix[r])
		else:
			d = sum(matrix[r + 1]) - sum(matrix[r])
		if d <= 0:
			return -1
		matrix[r][c] = d

		def sumc(c):
			nonlocal matrix
			s = 0
			for r in range(n):
				s += matrix[r][c]
			return s
		if len(set([sum(r) for r in matrix])) != 1:
			return -1
		if len(set([sumc(c) for c in range(n)])) != 1:
			return -1
		s1 = 0
		for i in range(n):
			s1 += matrix[i][i]
		s2 = 0
		for i in range(n):
			s2 += matrix[i][n - 1 - i]
		if s1 != s2:
			return -1
		return d


class Solution:

	def MissingNo(self, matrix):
		sum = 0
		non_zero = set()
		zero = set()
		is_zero = False
		for i in range(len(matrix)):
			sum = 0
			for j in range(len(matrix)):
				if matrix[i][j] == 0:
					is_zero = True
				sum += matrix[i][j]
			if is_zero:
				zero.add(sum)
				is_zero = False
			else:
				non_zero.add(sum)
		for i in range(len(matrix)):
			sum = 0
			for j in range(len(matrix)):
				if matrix[j][i] == 0:
					is_zero = True
				sum += matrix[j][i]
			if is_zero:
				zero.add(sum)
				is_zero = False
			else:
				non_zero.add(sum)
		sum = 0
		for i in range(len(matrix)):
			if matrix[i][i] == 0:
				is_zero = True
			sum += matrix[i][i]
		if is_zero:
			zero.add(sum)
			is_zero = False
		else:
			non_zero.add(sum)
		sum = 0
		for i in range(len(matrix)):
			if matrix[i][len(matrix) - i - 1] == 0:
				is_zero = True
			sum += matrix[i][len(matrix) - i - 1]
		if is_zero:
			zero.add(sum)
			is_zero = False
		else:
			non_zero.add(sum)
		if len(zero) == 1 and len(non_zero) == 1:
			if list(non_zero)[0] - list(zero)[0] <= 0:
				return -1
			return list(non_zero)[0] - list(zero)[0]
		else:
			return -1
		print(zero, non_zero)


class Solution:

	def MissingNo(self, matrix):
		row = len(matrix)
		col = len(matrix[0])
		if row == 1 and col == 1:
			return 0
		rows = set()
		cols = [0] * col
		front = rear = 0
		zero = None
		zero_row = 0
		for i in range(row):
			flag = False
			row_sum = 0
			for j in range(col):
				row_sum += matrix[i][j]
				cols[j] += matrix[i][j]
				if matrix[i][j] == 0:
					zero = [i, j]
					flag = True
				if i == j:
					front += matrix[i][j]
				if i + j == col - 1:
					rear += matrix[i][j]
			rows.add(row_sum)
			if flag:
				zero_row = row_sum
			if len(rows) > 2:
				return -1
		rows = list(rows)
		zero_col = cols[zero[1]]
		cols = list(set(cols))
		if len(cols) != 2:
			return -1
		diff1 = 0
		for i in rows:
			diff1 = max(diff1, i - zero_row)
		diff2 = 0
		for i in cols:
			diff2 = max(diff2, i - zero_col)
		if diff1 != diff2:
			return -1
		front_diagonal = back_diagonal = False
		if zero[0] == zero[1]:
			front_diagonal = True
		if zero[0] + zero[1] == col - 1:
			back_diagonal = True
		rear_diagonal = back_diagonal
		if front_diagonal and back_diagonal:
			if front - rear != 0:
				return -1
			return diff1
		if front_diagonal:
			if rear < front:
				return -1
			return diff1 if front + diff1 == rear else -1
		if rear_diagonal:
			if front < rear:
				return -1
			return diff1 if rear + diff1 == front else -1
		return diff1 if diff1 else -1


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		s = 0
		error = False
		for i in range(n):
			if 0 not in matrix[i]:
				if s != 0:
					x = sum(matrix[i])
					if s != x:
						error = True
						break
				else:
					s = sum(matrix[i])
			else:
				zeroRow = i
				zeroRowSum = sum(matrix[i])
				zeroCol = matrix[i].index(0)
		if error:
			return -1
		for j in range(n):
			x = 0
			for i in range(n):
				x += matrix[i][j]
			if j == zeroCol:
				zeroColSum = x
				continue
			elif s != x:
				error = True
				break
		if error:
			return -1
		x = 0
		hasDiagonal1Zero = False
		for i in range(n):
			x += matrix[i][i]
			if matrix[i][i] == 0:
				hasDiagonal1Zero = True
		if not hasDiagonal1Zero and s != x:
			return -1
		else:
			zeroDiagonal1Sum = x
		x = 0
		hasDiagonal2Zero = False
		for i in range(n):
			x += matrix[i][n - 1 - i]
			if matrix[i][n - 1 - i] == 0:
				hasDiagonal2Zero = True
		if not hasDiagonal2Zero and s != x:
			return -1
		else:
			zeroDiagonal2Sum = x
		if zeroRowSum != zeroColSum or (hasDiagonal1Zero and zeroRowSum != zeroDiagonal1Sum) or (hasDiagonal2Zero and zeroRowSum != zeroDiagonal2Sum):
			return -1
		res = s - zeroRowSum
		if res <= 0:
			return -1
		return res


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		sumr = list((0 for _ in range(n)))
		sumc = list((0 for _ in range(n)))
		sumd = [0, 0]
		srow = 0
		ind = [-1, -1]
		for i in range(n):
			hasZero = False
			for j in range(n):
				sumr[i] = sumr[i] + matrix[i][j]
				sumc[j] = sumc[j] + matrix[i][j]
				if matrix[i][j] == 0:
					ind = [i, j]
					hasZero = True
				if i == j:
					sumd[0] = sumd[0] + matrix[i][j]
				if i + j == n - 1:
					sumd[1] = sumd[1] + matrix[i][j]
			if not hasZero:
				srow = i
		ans = sumr[srow] - sumr[ind[0]]
		matrix[ind[0]][ind[1]] = ans
		sumr[ind[0]] = sumr[srow]
		sumc[ind[1]] = sumc[ind[1]] + ans
		if ind[0] == ind[1]:
			sumd[0] = sumd[0] + ans
		if ind[0] + ind[1] == n - 1:
			sumd[1] = sumd[1] + ans
		for i in range(n - 1):
			if not (sumr[i] == sumr[i + 1] and sumc[i] == sumc[i + 1]):
				return -1
		if not (sumd[0] == sumd[1] and ans > 0):
			return -1
		return ans


class Solution:

	def MissingNo(self, matrix):
		row = len(matrix)
		col = len(matrix[0])
		st = -1
		p1 = -1
		p2 = -1
		flag1 = 0
		store = 0
		store1 = 0
		flag2 = 0
		for i in range(row):
			add = 0
			flag = 1
			for j in range(col):
				if matrix[i][j] != 0:
					add += matrix[i][j]
				else:
					p1 = i
					p2 = j
					flag = 0
			if flag == 0 and flag2 != 1:
				store1 = add
				flag2 = 1
			if flag == 1 and flag1 != 1:
				store = add
				flag1 = 1
			if flag1 == 1 and flag2 == 1:
				break
		add3 = 0
		add4 = 0
		st = store
		matrix[p1][p2] = store - store1
		for i in range(row):
			add1 = 0
			add2 = 0
			for j in range(col):
				add1 += matrix[i][j]
				add2 += matrix[j][i]
				if i == j:
					add3 += matrix[i][j]
				if i + j == row - 1:
					add4 += matrix[i][j]
			if add1 != st or add2 != st:
				return -1
		if add3 != st or add4 != st:
			return -1
		if matrix[p1][p2] <= 0:
			return -1
		return matrix[p1][p2]


import numpy as np

class Solution:

	def sums(self, m, n: int):
		countZero = 0
		zeroIndex = list([0, 0])
		diognalSum = list([0, 0])
		diognalFlag = list([False, False])
		rowSum = [0 for i in range(0, n)]
		colSum = [0 for i in range(0, n)]
		for i in range(0, n):
			for j in range(0, n):
				if m[i][j] == 0:
					if countZero > 1 or m[i][j] < 0:
						return (rowSum, colSum, diognalSum, diognalFlag, -1, zeroIndex)
					else:
						countZero += 1
						zeroIndex[0] = i
						zeroIndex[1] = j
				if i == j:
					diognalSum[0] = diognalSum[0] + m[i][j]
					if m[i][j] == 0:
						diognalFlag[0] = True
					diognalSum[1] += m[i][n - 1 - j]
					if m[i][n - 1 - j] == 0:
						diognalFlag[1] = True
				rowSum[i] = rowSum[i] + m[i][j]
				colSum[j] = colSum[j] + m[i][j]
		return list((rowSum, colSum, diognalSum, diognalFlag, countZero, zeroIndex))

	def MissingNo(self, m):
		n = len(m)
		if n <= 1:
			return -1
		s = self.sums(m, n)
		if s[4] != 1:
			return -1
		diff = max(s[0]) - min(s[0])
		if s[3][0]:
			s[2][0] = s[2][0] + diff
		if s[3][1]:
			s[2][1] = s[2][1] + diff
		s[0][s[5][0]] = s[0][s[5][0]] + diff
		s[1][s[5][1]] = s[1][s[5][1]] + diff
		if len(set(s[0] + s[1] + s[2])) == 1 and s[4] == 1 and (diff > 0):
			return diff
		else:
			return -1


class Solution:

	def MissingNo(self, a):
		a1 = {}
		a2 = {}
		for i in range(len(a)):
			s = 0
			c = 0
			for j in range(len(a[i])):
				s += a[i][j]
				if a[i][j] == 0:
					c += 1
			if c > 0:
				a2[s] = 1
			else:
				a1[s] = 1
		for i in range(len(a[0])):
			s = 0
			c = 0
			for j in range(len(a)):
				s += a[j][i]
				if a[j][i] == 0:
					c += 1
			if c > 0:
				a2[s] = 1
			else:
				a1[s] = 1
		i = 0
		j = 0
		s = 0
		c = 0
		while i < len(a) and j < len(a[0]):
			s += a[i][j]
			if a[i][j] == 0:
				c += 1
			i += 1
			j += 1
		if c > 0:
			a2[s] = 1
		else:
			a1[s] = 1
		i = 0
		j = len(a[0]) - 1
		s = 0
		c = 0
		while i < len(a) and j >= 0:
			s += a[i][j]
			if a[i][j] == 0:
				c += 1
			i += 1
			j -= 1
		if c > 0:
			a2[s] = 1
		else:
			a1[s] = 1
		if len(a2) > 1 or len(a1) > 1:
			return -1
		for i in a2:
			o = i
			break
		for i in a1:
			p = i
			break
		if o >= p:
			return -1
		else:
			return p - o


class Solution:

	def MissingNo(self, M):
		import numpy as np
		M = np.array(M)
		SR = M.sum(axis=0)
		SC = M.sum(axis=1)
		(zr, zc, X) = (np.argmin(SR), np.argmin(SC), SR.max())
		add = X - SR[zr]
		(SR[zr], SC[zc], M[zr, zc]) = (SR[zr] + add, SC[zc] + add, M[zr, zc] + add)
		if add <= 0:
			return -1
		if not np.array_equal(SR, SC):
			return -1
		if M.diagonal().sum() != X:
			return -1
		if np.fliplr(M).diagonal().sum() != X:
			return -1
		return add


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		zx = None
		zy = None
		for i in range(n):
			row_sum = 0
			for j in range(n):
				row_sum += matrix[i][j]
				if matrix[i][j] == 0:
					zx = i
					zy = j
		equal_sum = 0
		i = 0 if zx else 1
		for j in range(n):
			equal_sum += matrix[i][j]
		zero_sum = 0
		for j in range(n):
			zero_sum += matrix[zx][j]
		matrix[zx][zy] = equal_sum - zero_sum
		if matrix[zx][zy] <= 0:
			return -1
		for i in range(n):
			row_sum = 0
			col_sum = 0
			for j in range(n):
				row_sum += matrix[i][j]
				col_sum += matrix[j][i]
			if row_sum != equal_sum or col_sum != equal_sum:
				return -1
		diag_sum1 = 0
		diag_sum2 = 0
		for i in range(n):
			diag_sum1 += matrix[i][i]
			diag_sum2 += matrix[i][n - i - 1]
		if diag_sum1 != equal_sum or diag_sum2 != equal_sum:
			return -1
		return matrix[zx][zy]


class Solution:

	def MissingNo(self, matrix):
		for i in range(len(matrix)):
			for j in range(len(matrix[i])):
				if matrix[i][j] == 0:
					idx = i
					jdx = j
					break
		row = [0] * len(matrix)
		col = [0] * len(matrix)
		(d1, d2, s, flag) = (0, 0, 0, 0)
		for i in range(len(matrix)):
			(si, sj) = (0, 0)
			for j in range(len(matrix)):
				si += matrix[i][j]
				sj += matrix[j][i]
			row[i] = si
			col[i] = sj
			d1 += matrix[i][i]
			d2 += matrix[i][len(matrix) - i - 1]
			if i != idx and flag == 0:
				s = row[i]
				flag = 1
		temp = s - row[idx]
		if temp <= 0:
			return -1
		row[idx] += temp
		col[jdx] += temp
		if idx == jdx:
			d1 += temp
		if idx == len(matrix) - jdx - 1:
			d2 += temp
		for i in range(len(matrix)):
			if row[i] != s or col[i] != s:
				return -1
			if d1 != s or d2 != s:
				return -1
		return temp


class Solution:

	def MissingNo(self, matrix):
		N = len(matrix[0])
		zeroRow = -1
		zeroCol = -1
		rowSum = 0
		zeroRowSum = 0
		colSums = [0 for i in range(N)]
		rbDiagSum = 0
		rtDiagSum = 0
		for rowNum in range(N):
			zeroRowBool = False
			tmpRowSum = 0
			for colNum in range(N):
				val = matrix[rowNum][colNum]
				if val == 0:
					zeroRow = rowNum
					zeroCol = colNum
					zeroRowBool = True
					continue
				tmpRowSum += val
				colSums[colNum] += val
				if rowNum == colNum:
					rbDiagSum += val
				if rowNum + colNum == N - 1:
					rtDiagSum += val
			if zeroRowBool:
				zeroRowSum = tmpRowSum
			elif rowSum == 0:
				rowSum = tmpRowSum
			elif tmpRowSum != rowSum:
				return -1
		colSumSet = set(colSums)
		colSumSet.add(rowSum)
		colSumSet.add(zeroRowSum)
		if rowSum <= zeroRowSum:
			return -1
		if len(colSumSet) != 2:
			return -1
		if zeroCol == zeroRow:
			if rbDiagSum != zeroRowSum:
				return -1
		elif rbDiagSum != rowSum:
			return -1
		if zeroCol + zeroRow == N - 1:
			if rtDiagSum != zeroRowSum:
				return -1
		elif rtDiagSum != rowSum:
			return -1
		return rowSum - zeroRowSum


class Solution:

	def MissingNo(self, matrix):
		col = []
		row = []
		(diag1, diag2) = (0, 0)
		zero = []
		for i in range(len(matrix)):
			temp = 0
			for j in range(len(matrix[0])):
				if matrix[i][j] == 0:
					zero = [i, j]
				temp += matrix[i][j]
			col.append(temp)
		for i in range(len(matrix[0])):
			temp = 0
			for j in range(len(matrix)):
				temp += matrix[j][i]
			row.append(temp)
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if i == j:
					diag1 += matrix[i][j]
				if i + j == n - 1:
					diag2 += matrix[i][j]
		if zero[0] == 0:
			k = col[1] - col[zero[0]]
		else:
			k = col[0] - col[zero[0]]
		if zero[1] == 0:
			l = row[1] - row[zero[1]]
			suma = row[1]
		else:
			l = row[0] - row[zero[1]]
			suma = row[0]
		if k == l:
			res = k
		else:
			return -1
		if res <= 0:
			return -1
		if zero[0] == zero[1]:
			diag1 += res
		if zero[0] == len(matrix) - zero[1] - 1:
			diag2 += res
		if diag1 != diag2:
			return -1
		col[zero[0]] += res
		row[zero[1]] += res
		for i in col:
			if i != suma:
				return -1
		for i in row:
			if i != suma:
				return -1
		if diag1 != suma or diag2 != suma:
			return -1
		return res


class Solution:

	def MissingNo(self, matrix):
		add = []
		index = []
		diag1 = diag2 = sum_ = col = 0
		for i in range(len(matrix)):
			sum_ = 0
			for j in range(len(matrix)):
				if matrix[i][j] == 0:
					index.append(i)
					index.append(j)
				else:
					sum_ += matrix[i][j]
			add.append(sum_)
		toadd = max(add) - min(add)
		if toadd:
			matrix[index[0]][index[1]] = toadd
		else:
			return -1
		add = []
		for i in range(len(matrix)):
			diag1 += matrix[i][i]
			diag2 += matrix[i][len(matrix) - i - 1]
			add.append(sum(matrix[i]))
		if diag1 != diag2 or min(add) != max(add):
			return -1
		return toadd


class Solution:

	def MissingNo(self, matrix):
		if 0 not in matrix[0]:
			s = sum(matrix[0])
		else:
			zs = sum(matrix[0])
			s = sum(matrix[1])
		(d1s, d2s, czs) = (0, 0, 0)
		(d1, d2) = (True, True)
		n = len(matrix)
		for i in range(n):
			if 0 in matrix[i]:
				zs = sum(matrix[i])
			elif s != sum(matrix[i]):
				return -1
			cs = 0
			cz = False
			for j in range(n):
				if matrix[j][i] == 0:
					cz = True
				cs += matrix[j][i]
			if cz:
				czs = cs
			elif cs != s:
				return -1
			if matrix[i][i] != 0:
				d1s += matrix[i][i]
			else:
				d1 = False
			if matrix[i][n - i - 1] != 0:
				d2s += matrix[i][n - i - 1]
			else:
				d2 = False
		if d1:
			if d1s != s:
				return -1
		elif s - d1s != s - zs:
			return -1
		if d2:
			if d2s != s:
				return -1
		elif s - d2s != s - zs:
			return -1
		if s - zs > 0 and s - zs == s - czs:
			return s - zs
		else:
			return -1


class Solution:

	def MissingNo(self, arr):
		n = len(arr)
		rsum = [0] * n
		csum = [0] * n
		dia1 = 0
		dia2 = 0
		pos1 = -1
		pos2 = -1
		for i in range(n):
			for j in range(n):
				if arr[i][j] == 0:
					pos1 = i
					pos2 = j
				rsum[i] += arr[i][j]
				csum[i] += arr[j][i]
				if i == j:
					dia1 += arr[i][j]
					dia2 += arr[n - 1 - i][j]
		row = set(rsum)
		rval = 0
		cval = 0
		if len(row) > 2:
			return -1
		rval = max(row) - min(row)
		col = set(csum)
		if len(col) > 2:
			return -1
		cval = max(col) - min(col)
		if rsum[pos1] > min(row):
			return -1
		if csum[pos2] > min(col):
			return -1
		if cval != rval:
			return -1
		arr[pos1][pos2] += cval
		rsum[pos1] += cval
		csum[pos2] += cval
		dia1 = 0
		dia2 = 0
		for i in range(n):
			dia1 += arr[i][i]
			dia2 += arr[n - 1 - i][i]
		if dia1 == dia2:
			if set(rsum) == set(csum):
				if cval > 0:
					return cval
		return -1


class Solution:

	def MissingNo(self, matrix):
		sm = -1
		res = -1
		j = -1
		k = -1
		for (i, num) in enumerate(matrix):
			if 0 in num:
				res = sum(num)
				j = i
				k = num.index(0)
			elif sm == -1:
				sm = sum(num)
			elif sm != sum(num):
				return -1
		res = sm - res
		matrix[j][k] = res
		l = len(matrix[0])
		for i in range(l):
			tmp = 0
			for j in range(l):
				tmp += matrix[j][i]
			if tmp != sm:
				return -1
		tmp = 0
		for i in range(l):
			tmp += matrix[i][i]
		if tmp != sm:
			return -1
		tmp = 0
		for i in range(l - 1, -1, -1):
			tmp += matrix[i][l - i - 1]
		if tmp != sm:
			return -1
		return res if res > 0 else -1


class Solution:

	def MissingNo(self, l):
		(n, m) = (len(l), len(l[0]))
		r = [0] * n
		c = [0] * m
		(pd, nd) = [0] * 2
		(x, y) = [-1] * 2
		for i in range(n):
			for j in range(n):
				if l[i][j] == 0:
					(x, y) = (i, j)
					continue
				r[i] += l[i][j]
				c[j] += l[i][j]
				if i == j:
					nd += l[i][j]
				if i + j == n - 1:
					pd += l[i][j]
		(a1, b1) = (min(r), max(r))
		(a2, b2) = (min(c), max(c))
		if r.count(b1) != n - 1 or c.count(b2) != n - 1:
			return -1
		r[x] += b1 - a1
		c[y] += b2 - a2
		if b2 - a2 != b1 - a1:
			return -1
		if x == y:
			nd += b1 - a1
		if x + y == n - 1:
			pd += b1 - a1
		if pd != nd:
			return -1
		return b1 - a1


class Solution:

	def MissingNo(self, matrix):
		m = len(matrix)
		sum_row = [0 for i in range(m)]
		sum_col = [0 for i in range(m)]
		sum_dig = [0, 0]
		(zero_row, zero_col) = (0, 0)
		for i in range(m):
			for j in range(m):
				if matrix[i][j] == 0:
					(zero_row, zero_col) = (i, j)
				sum_row[i] += matrix[i][j]
				sum_col[j] += matrix[i][j]
				if i == j:
					sum_dig[0] += matrix[i][j]
				if i + j == m - 1:
					sum_dig[1] += matrix[i][j]
		if zero_row == 0:
			val = sum_row[1] - sum_row[0]
		else:
			val = sum_row[zero_row - 1] - sum_row[zero_row]
		if val <= 0:
			return -1
		sum_row[zero_row] += val
		sum_col[zero_col] += val
		if zero_row == zero_col:
			sum_dig[0] += val
		if zero_row + zero_col == m - 1:
			sum_dig[1] += val
		for x in sum_row + sum_col + sum_dig:
			if x != sum_row[0]:
				return -1
		return val


class Solution:

	def MissingNo(self, matrix):
		n = len(matrix)
		row_sum = []
		for i in range(n):
			curr_row = 0
			for j in range(n):
				if matrix[i][j] == 0:
					p = i
					q = j
					break
		if p != 0:
			row_sum = sum(matrix[0][0:n])
		else:
			row_sum = sum(matrix[1][0:n])
		col_sum = 0
		diag_sum = 0
		diag_sum2 = 0
		if q != 0:
			for i in range(n):
				col_sum += matrix[i][0]
		else:
			for i in range(n):
				col_sum += matrix[i][1]
		row_sum_0 = 0
		col_sum_0 = 0
		for i in range(n):
			row_sum_0 += matrix[p][i]
			col_sum_0 += matrix[i][q]
		if row_sum - row_sum_0 != col_sum - col_sum_0:
			return -1
		else:
			x = row_sum - row_sum_0
		matrix[p][q] = x
		for i in range(n):
			diag_sum += matrix[i][i]
			diag_sum2 += matrix[i][n - i - 1]
		if diag_sum != diag_sum2:
			return -1
		for i in range(n):
			curr_row = 0
			curr_col = 0
			for j in range(n):
				curr_row += matrix[i][j]
				curr_col += matrix[j][i]
			if curr_row != row_sum or curr_col != col_sum:
				return -1
		return -1 if x <= 0 else x


class Solution:

	def MissingNo(self, matrix):
		rsum = []
		csum = []
		diags = [[] for __ in range(2)]
		dsums = [0] * 2
		res = -1
		N = len(matrix)
		zero = [-1, -1]
		for i in range(N):
			__r = 0
			__c = 0
			for j in range(N):
				if zero[0] == -1 and matrix[i][j] == 0:
					zero[0] = i
					zero[1] = j
				__r += matrix[i][j]
				__c += matrix[j][i]
			rsum.append(__r)
			csum.append(__c)
		rs = set(rsum)
		cs = set(csum)
		rl = list(rs)
		cl = list(cs)
		rl.sort()
		cl.sort()
		if len(rl) == 2 and len(cl) == 2 and (rsum[zero[0]] == csum[zero[1]]) and (rsum[zero[0]] == rl[0]):
			diff = rl[1] - rl[0]
			matrix[zero[0]][zero[1]] = diff
			for i in range(N):
				j = N - i - 1
				dsums[0] += matrix[i][i]
				dsums[1] += matrix[i][j]
			if dsums[0] == dsums[1] and dsums[0] == rl[1]:
				res = diff
		return res


class Solution:

	def MissingNo(self, matrix):
		status = 0
		status_f = 0
		sm_dia1 = 0
		sm_dia2 = 0
		for i in range(len(matrix)):
			for f in range(len(matrix)):
				if matrix[i][f] == 0:
					zero = [i, f]
		if 0 != zero[0]:
			sm = sum(matrix[0])
		else:
			sm = sum(matrix[1])
		for i in range(len(matrix)):
			sm_col = 0
			for j in range(len(matrix)):
				if zero[0] == i and zero[1] == j and (status_f == 0):
					status = 1
				sm_col += matrix[i][j]
			if sm_col != sm:
				if status == 0:
					return -1
				else:
					matrix[zero[0]][zero[1]] = sm - sm_col
					status = 0
					status_f = 1
		for i in range(len(matrix)):
			sm_dia1 += matrix[i][i]
			sm_dia2 += matrix[i][(i + 1) * -1]
		if sm_dia1 != sm or sm_dia2 != sm:
			return -1
		if matrix[zero[0]][zero[1]] <= 0:
			return -1
		return matrix[zero[0]][zero[1]]


class Solution:

	def MissingNo(self, matrix):
		nzSum = 0
		zSum = 0
		zFlag = False
		n = len(matrix[0])
		localSum = 0
		for i in range(0, n):
			localSum = 0
			for j in range(0, n):
				if matrix[i][j] == 0:
					zFlag = True
				localSum += matrix[i][j]
			if zFlag:
				if zSum > 0 and zSum != localSum:
					return -1
				zSum = localSum
				zFlag = False
			else:
				if nzSum > 0 and nzSum != localSum:
					return -1
				nzSum = localSum
		for i in range(0, n):
			localSum = 0
			for j in range(0, n):
				if matrix[j][i] == 0:
					zFlag = True
				localSum += matrix[j][i]
			if zFlag:
				if zSum > 0 and zSum != localSum:
					return -1
				zSum = localSum
				zFlag = False
			else:
				if nzSum > 0 and nzSum != localSum:
					return -1
				nzSum = localSum
		localSum = 0
		for i in range(0, n):
			if matrix[i][i] == 0:
				zFlag = True
			localSum += matrix[i][i]
		if zFlag:
			if zSum > 0 and zSum != localSum:
				return -1
			zSum = localSum
			zFlag = False
		else:
			if nzSum > 0 and nzSum != localSum:
				return -1
			nzSum = localSum
		localSum = 0
		for i in range(0, n):
			if matrix[i][n - i - 1] == 0:
				zFlag = True
			localSum += matrix[i][n - i - 1]
		if zFlag:
			if zSum > 0 and zSum != localSum:
				return -1
			zSum = localSum
			zFlag = False
		else:
			if nzSum > 0 and nzSum != localSum:
				return -1
			nzSum = localSum
		if nzSum - zSum <= 0:
			return -1
		else:
			return nzSum - zSum


class Solution:

	def MissingNo(self, matrix):
		(r, c) = (None, None)
		prev = None
		n = len(matrix)
		for i in range(n):
			if 0 in matrix[i]:
				r = i
				continue
			sm = sum(matrix[i])
			if prev is None:
				prev = sm
			elif prev != sm:
				return -1
		ans = prev - sum(matrix[r])
		if ans <= 0:
			return -1
		for i in range(n):
			sm = 0
			flag = False
			for j in range(n):
				if matrix[j][i] == 0:
					matrix[j][i] = ans
				sm += matrix[j][i]
			if not flag:
				if prev != sm:
					return -1
		sm1 = sm2 = 0
		for i in range(n):
			sm1 += matrix[i][i]
			sm2 += matrix[i][n - i - 1]
		if sm1 != prev or sm2 != prev:
			return -1
		return ans


class Solution:

	def MissingNo(self, matrix):
		(row, col) = (0, 0)
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if matrix[i][j] == 0:
					row = i
					col = j
		for i in range(len(matrix)):
			if i != row:
				sumCount = sum(matrix[i])
				break
		sumReq = sumCount - sum(matrix[row])
		matrix[row][col] = sumReq
		if self.isInValid(matrix, sumCount):
			return -1
		return sumReq if sumReq > 0 else -1

	def isInValid(self, matrix, sumCount):
		rLen = len(matrix)
		(sumRowCol, sumColRow) = (0, 0)
		for i in range(len(matrix)):
			sumRowCol += matrix[i][i]
			sumColRow += matrix[rLen - i - 1][i]
			if sumCount != sum(matrix[i]):
				return 1
		if sumRowCol != sumCount or sumColRow != sumCount:
			return 1
		for i in range(len(matrix[0])):
			tempSum = 0
			for j in range(len(matrix)):
				tempSum += matrix[j][i]
			if tempSum != sumCount:
				return 1


import numpy as np

class Solution:

	def MissingNo(self, matrix):
		matrix = np.array(matrix)
		shape = np.shape(matrix)
		row_sum = np.sum(matrix, axis=1)
		zerox_pos = np.where(matrix == 0)[0]
		temp = matrix[zerox_pos].flatten()
		zeroy_pos = np.where(temp == 0)[0]
		diff = np.amax(row_sum) - np.amin(row_sum)
		matrix[zerox_pos[0]][zeroy_pos[0]] = diff
		sum_1 = 0
		for i in range(shape[0]):
			sum_1 += matrix[i][i]
		sum_2 = 0
		for j in range(shape[0]):
			sum_2 += matrix[shape[0] - 1 - j][j]
		if (np.sum(matrix, axis=1) == np.sum(matrix, axis=0)).all() and sum_1 == sum_2 and (diff > 0):
			return diff
		else:
			return -1


class Solution:

	def diagonalSum(self, matrix, n):
		(i, j) = (0, 0)
		d1 = 0
		d2 = 0
		for i in range(0, n):
			d1 += matrix[i][i]
		for i in range(n - 1, -1, -1):
			d2 += matrix[i][n - 1 - i]
		return (d1, d2)

	def checkZero(self, matrix, n):
		for i in range(0, n, 2):
			for j in range(0, n):
				if i < n and matrix[i][j] == 0:
					return (i, j)
				if i + 1 < n and matrix[i + 1][j] == 0:
					return (i + 1, j)

	def getRowSum(self, matrix, n, i):
		ans = 0
		for j in range(0, n):
			ans += matrix[i][j]
		return ans

	def getColSum(self, matrix, n, j):
		ans = 0
		for i in range(0, n):
			ans += matrix[i][j]
		return ans

	def MissingNo(self, matrix):
		row = len(matrix)
		col = row
		n = row
		(rowZero, colZero) = self.checkZero(matrix, n)
		row1 = self.getRowSum(matrix, n, rowZero)
		row2 = 0
		if rowZero == 0:
			row2 = self.getRowSum(matrix, n, 1)
		else:
			row2 = self.getRowSum(matrix, n, 0)
		if row1 > row2:
			matrix[rowZero][colZero] = row1 - row2
		else:
			matrix[rowZero][colZero] = row2 - row1
		if matrix[rowZero][colZero] == 0:
			return -1
		(d1, d2) = self.diagonalSum(matrix, row)
		if d1 != d2:
			return -1
		rowOld = self.getRowSum(matrix, n, 0)
		for i in range(1, n):
			rowNew = self.getRowSum(matrix, n, i)
			if rowNew != rowOld:
				return -1
		colOld = self.getColSum(matrix, n, 0)
		for j in range(1, n):
			colNew = self.getColSum(matrix, n, j)
			if colNew != colOld:
				return -1
		if colOld != rowOld or colOld != d1 or d1 != rowOld:
			return -1
		return matrix[rowZero][colZero]


class Solution:

	def MissingNo(self, matrix):
		s_without_zero = 0
		for i in matrix:
			if 0 not in i:
				s_without_zero = sum(i)
				break
		s_with_zero = 0
		for i in range(len(matrix)):
			if 0 in matrix[i]:
				s_with_zero = sum(matrix[i])
				for j in range(len(matrix[0])):
					if matrix[i][j] == 0:
						matrix[i][j] = s_without_zero - s_with_zero
						break
				break
		curR = sum(matrix[0])
		for i in matrix:
			if curR != sum(i):
				return -1
		firstC = 0
		for i in range(len(matrix)):
			firstC += matrix[i][0]
		for i in range(len(matrix)):
			curC = 0
			for j in range(len(matrix)):
				curC += matrix[j][i]
			if curC != firstC:
				return -1
		diag1 = 0
		for i in range(len(matrix)):
			diag1 += matrix[i][i]
		diag2 = 0
		for i in range(len(matrix)):
			diag2 += matrix[i][len(matrix) - i - 1]
		if diag1 != diag2:
			return -1
		if s_without_zero - s_with_zero > 0:
			return s_without_zero - s_with_zero
		return -1


class Solution:

	def MissingNo(self, matrix):

		def row_sum(matrix, actual_sum, missing_value):
			n = len(matrix)
			for i in range(n):
				sum = 0
				for j in range(n):
					if matrix[i][j] == 0:
						sum += missing_value
					else:
						sum += matrix[i][j]
				if sum != actual_sum:
					return 0
			return 1

		def col_sum(matrix, actual_sum, missing_value):
			n = len(matrix)
			for i in range(n):
				sum = 0
				for j in range(n):
					if matrix[j][i] == 0:
						sum += missing_value
					else:
						sum += matrix[j][i]
				if sum != actual_sum:
					return 0
			return 1

		def dia_sum(matrix, actual_sum, missing_value):
			n = len(matrix)
			(d1, d2) = (0, 0)
			for i in range(n):
				if matrix[i][i] == 0:
					d1 += missing_value
				else:
					d1 += matrix[i][i]
			for i in range(n):
				if matrix[i][n - i - 1] == 0:
					d2 += missing_value
				else:
					d2 += matrix[i][n - i - 1]
			if d1 == d2 and d1 == actual_sum and (d2 == actual_sum):
				return 1
			return 0
		n = len(matrix)
		(zx, zy) = (0, 0)
		for i in range(n):
			for j in range(n):
				if matrix[i][j] == 0:
					(zx, zy) = (i, j)
		(actual_sum, zero_sum, missing_value) = (0, 0, 0)
		if zx == 0:
			for i in range(n):
				zero_sum += matrix[0][i]
			for i in range(n):
				actual_sum += matrix[1][i]
		else:
			for i in range(n):
				actual_sum += matrix[0][i]
			for i in range(n):
				zero_sum += matrix[zx][i]
		missing_value = actual_sum - zero_sum
		if missing_value <= 0:
			return -1
		if row_sum(matrix, actual_sum, missing_value) and col_sum(matrix, actual_sum, missing_value) and dia_sum(matrix, actual_sum, missing_value):
			return missing_value
		else:
			return -1


class Solution:

	def MissingNo(self, matrix):
		size = len(matrix)
		col_sum = [0] * size
		row_sum = [0] * size
		diag_1_sum = 0
		diag_2_sum = 0
		missing_in_diag_1 = False
		missing_in_diag_2 = False
		for x in range(size):
			for y in range(size):
				cell = matrix[x][y]
				col_sum[x] += cell
				row_sum[y] += cell
				if x == y:
					diag_1_sum += cell
					if cell == 0:
						missing_in_diag_1 = True
				if x == size - y - 1:
					diag_2_sum += cell
					if cell == 0:
						missing_in_diag_2 = True
		missing_row = max(row_sum) - min(row_sum)
		missing_col = max(col_sum) - min(col_sum)
		if missing_in_diag_1:
			diag_1_sum += missing_row
		if missing_in_diag_2:
			diag_2_sum += missing_row
		ok = missing_row == missing_col and diag_1_sum == diag_2_sum and (diag_1_sum == max(col_sum)) and (diag_1_sum == max(row_sum)) and (sum(col_sum) + missing_row == max(row_sum) * size) and (missing_row > 0)
		if ok:
			return missing_row
		else:
			return -1


class Solution:

	def MissingNo(self, matrix):
		(rows, cols) = (len(matrix), len(matrix[0]))
		rowSum = [0] * rows
		colSum = [0] * cols
		diagSum = [0, 0]
		x = -1
		y = -1
		for i in range(rows):
			diagSum[0] += matrix[i][i]
			diagSum[1] += matrix[i][n - i - 1]
			for j in range(cols):
				if matrix[i][j] == 0:
					x = i
					y = j
				rowSum[i] += matrix[i][j]
				colSum[j] += matrix[i][j]
		expRowSum = rowSum[0] if x != 0 else rowSum[1]
		expColSum = colSum[0] if y != 0 else colSum[1]
		diff = expColSum - colSum[y]
		if expRowSum != expColSum or diff != expRowSum - rowSum[x] or diff <= 0:
			return -1
		if x == y:
			diagSum[0] += diff
		if x == rows - y - 1:
			diagSum[1] += diff
		if diagSum[0] != diagSum[1]:
			return -1
		rowSum[x] += diff
		colSum[y] += diff
		for i in range(rows):
			if rowSum[i] != diagSum[0] or colSum[i] != diagSum[0]:
				return -1
		return diff


class Solution:

	def MissingNo(self, matrix):
		i = 0
		j = len(matrix[0]) - 1
		d2 = 0
		d1 = 0
		f = [0, 0]
		r = [0 for i in range(len(matrix))]
		c = [0 for i in range(len(matrix[0]))]
		while j >= 0:
			d2 += matrix[i][j]
			j -= 1
			i += 1
		i = 0
		s = 0
		n = len(matrix[0])
		while i < len(matrix):
			j = 0
			while j < n:
				s += matrix[i][j]
				if matrix[i][j] == 0:
					s = 0
					break
				j += 1
			if s != 0:
				break
			i += 1
		for i in range(len(matrix)):
			for j in range(n):
				if matrix[i][j] == 0:
					f = [i, j]
				if i == j:
					d1 += matrix[i][j]
				c[j] += matrix[i][j]
				r[i] += matrix[i][j]
		k = 0
		if len(set(r)) != 2 or len(set(c)) != 2:
			return -1
		if d2 != s:
			k = s - d2
		if d1 != s:
			if f[0] == f[1]:
				if k != 0 and k != s - d1:
					return -1
				else:
					k = s - d1
			else:
				return -1
		if k != 0 and (k != s - r[f[0]] or k != s - c[f[1]]):
			return -1
		elif s - r[f[0]] == s - c[f[1]] and s - r[f[0]] > 0:
			return s - r[f[0]]
		else:
			return -1