Given a matrix A[][] of size N such that it has only one 0, Find the number to be placed in place of the 0 such that sum of the numbers in every row, column and two diagonals become equal.

**Note:** Diagonals should be only of the form A[i][i] and A[i][N-i-1]. If there is a matrix of size 1, with an element 0, then print 1.

**Input:**

First line contains the test cases, T . Then T test cases follow. Each test case contains a single integer N which represents the number of rows and columns of the matrix. Then in the next line are N*N space separated of the matrix A.

**Output:**

Display the number in place of 0 if possible, otherwise display -1. If there is a matrix of size 1, with an element 0, then print 1.

**Constraints:**

1<=T<=25

1<=n<=100

1<=a_{i}<=1000000000

**Example:
Input**

2

2

5 5 5 0

3

1 2 0 3 1 2 2 3 1

**Output:**

5

-1

**Explanation**:

(i)In the first test case 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.

(ii)Second sample, it is not possible to insert an element in place of 0 so that the condition is satisfied.thus result is -1.

Author: justrelax

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

agness | 122 |

aman1931998 | 102 |

Survivor | 100 |

uchitnitk | 96 |

lonecoder | 95 |

KshatriyaYash | 1066 |

nikhil_sojan | 1032 |

lonecoder | 1030 |

mazumderrohit8 | 899 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4492 |

Quandray | 4444 |

Login to report an issue on this page.