import sys input = sys.stdin.readline def rref(A): if not A: return n, m = len(A), len(A[0]) pivot = 0 for j in range(m): i = pivot while i < n and A[i][j] == 0: i += 1 if i == n: continue if i != pivot: A[i], A[pivot] = A[pivot], A[i] p = A[pivot][j] for l in range(j, m): A[pivot][l] //= p for k in range(n): if k == pivot: continue v = A[k][j] for l in range(j, m): A[k][l] -= A[pivot][l] * v pivot += 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] rref(rem) print(1 if not rem or sum(rem[-1]) > 0 else 0)