import sys input = sys.stdin.readline def det2(A): if not A: return 1 n = len(A) pivot = 0 for j in range(n): i = pivot while i < n and A[i][j] == 0: i += 1 if i == n: return 0 if i != pivot: A[i], A[pivot] = A[pivot], A[i] for k in range(n): if k == pivot: continue if A[k][j]: for l in range(j, n): A[k][l] ^= A[pivot][l] pivot += 1 return 1 T = int(input()) for _ in range(T): N, P = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] if P >= 3: print(0) else: row = [0] * N col = [0] * N ok = True for i in range(N): for j in range(N): if A[i][j] == -1: if row[i] or col[j]: ok = False break row[i] = 1 col[j] = 1 if not ok: break if not ok: print(0) else: M = N - sum(row) rs = [i for i in range(N) if not row[i]] cs = [j for j in range(N) if not col[j]] rem = [[0] * M for _ in range(M)] for ni, i in enumerate(rs): for nj, j in enumerate(cs): rem[ni][nj] = A[i][j] print(det2(rem))