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.

sabka_din_aayega | 317 |

maggiiiii | 248 |

thanuvinu94 | 211 |

Savage_19 | 206 |

Anugrah_kumar | 172 |

Bishnu Dev Panda | 803 |

sabka_din_aayega | 628 |

Anugrah_kumar | 562 |

okayboss | 561 |

maggiiiii | 531 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.