T = int(input()) for _ in range(T): n, p = map(int,input().split()) a = [list(map(int,input().split())) for i in range(n)] if p > 2: print(0) continue # p = 2 x = [0] * n y = [0] * n for i in range(n): for j in range(n): if a[i][j] == -1: x[i] += 1 y[j] += 1 mode = 1 for i in range(n): if x[i] > 1 or y[i] > 1: mode = 0 break if mode == 0: print(0) continue bx = [] by = [] for i in range(n): if x[i] == 0: bx.append(x[i]) if y[i] == 0: by.append(y[i]) m = len(bx) b = [0] * m for i in range(m): for j in range(m): b[i] += a[bx[i]][by[j]] << j base = [] for v in b: for e in base: v = min(v, v^e) if v > 0: base.append(v) if len(base) == m: print(1) else: print(0)