#include #define rep(i,n) for (int i = 0; i < int(n); ++i) using namespace std; void solve(){ int n, p; cin >> n >> p; vector> a(n,vector(n)); rep(i,n) rep(j,n) cin >> a[i][j]; if (p != 2){ cout << 0 << endl; return ; } vector rs, cs; rep(i,n){ int cnt = 0; rep(j,n){ if (a[i][j] == -1) cnt++; } if (cnt >= 2){ cout << 0 << endl; return ; } if (cnt == 0){ rs.emplace_back(i); } } rep(j,n){ int cnt = 0; rep(i,n){ if (a[i][j] == -1) cnt++; } if (cnt >= 2){ cout << 0 << endl; return ; } if (cnt == 0){ cs.emplace_back(j); } } vector bs; for (int i : rs){ int x = 0; for (int j : cs){ x <<= 1; x += a[i][j]; } for (int b : bs){ x = min(x,x^b); } if (x == 0){ cout << 0 << endl; return ; } bs.emplace_back(x); } cout << 1 << endl; } int main(){ int t; cin >> t; while(t--) solve(); }