luogu#P5038. [SCOI2012] 奇怪的游戏
[SCOI2012] 奇怪的游戏
Problem Description
Blinker has recently become interested in a strange game.
The game is played on an board, and each cell contains a number. Each time, Blinker chooses two adjacent cells and increases both numbers by .
Now Blinker wants to know the minimum number of moves needed to make all numbers on the board become the same. If it can never become the same, output .
Input Format
The first line contains an integer , meaning the input consists of rounds of the game.
In each round, the first line contains two integers and , representing the number of rows and columns of the board.
Then follow lines, each containing numbers.
Output Format
For each round, output the minimum number of moves needed to finish the game. If it can never become the same number, output .
2
2 2
1 2
2 3
3 3
1 2 3
2 3 4
4 3 2
2
-1
Hint
For of the testdata, it is guaranteed that and .
For of the testdata, it is guaranteed that and . All numbers are positive integers and less than .
Translated by ChatGPT 5