#include<bits/stdc++.h>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t, i, r, k, n, m, sr = 0, sk = 0, a[100][100]; bool moguce = true;
	cin >> t;
	for(i = 0; i < t; i++){
		cin >> n >> m;
		
		for(r = 0; r < n; r++)
			for(k = 0; k < m; k++)cin >> a[r][k];
			
		for(r = 0; r < n; r++){
			for(k = 0; k < m; k++){
				sr += a[r][k];
			}
			if(sr % n != 0){
				moguce = false;
				sr = 0;
			}
		}
		
		for(k = 0; k < m; r++){
			for(r = 0; r < n; k++){
				sk += a[r][k];
				
			}
			if(r == n-1 && sk % m != 0){
				moguce = false;
				sk = 0;
			}
		}
		
		if(moguce == true){
			cout << "1" << "\n";
		}
		else{
			cout << "-1" << "\n";
		}
			
	}
	return 0;
}

/*for(r = 0; r < n; r++){
			for(k = 0; k < m; k++)cout << a[r][k] << " ";
			cout << "\n";	
		}*/