#include <stdio.h>

int main () {
  int t = 0;
  int n = 0;
  int p = 0;
  int a[50][50] = {};
  
  int res = 0;
  
  long long b[50] = {};
  
  res = scanf("%d", &t);
  while (t > 0) {
    res = scanf("%d", &n);
    res = scanf("%d", &p);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        res = scanf("%d", a[i]+j);
      }
    }
    if (p > 2) {
      printf("0\n");
    } else {
      int bcnt = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (a[i][j] < 0) {
            int cnt = 0;
            for (int k = 0; k < n; k++) {
              if (a[i][k] < 0) {
                cnt++;
              }
              if (a[k][j] < 0) {
                cnt++;
              }
              a[i][k] = 0;
              a[k][j] = 0;
            }
            if (cnt == 2) {
              a[i][j] = 1;
            }
          }
        }
      }
      for (int i = 0; i < n; i++) {
        long long v = 0LL;
        for (int j = 0; j < n; j++) {
          if (a[i][j] > 0) {
            v |= (1LL<<((long long)j));
          }
        }
        for (int j = 0; j < bcnt; j++) {
          if (v > (v^b[j])) {
            v = (v^b[j]);
          }
        }
        if (v > 0) {
          b[bcnt] = v;
          bcnt++;
        }
      }
      if (bcnt == n) {
        printf("1\n");
      } else {
        printf("0\n");
      }
    }
    t--;
  }
  
  return 0;
}