/**
 *    author:  tourist
 *    created: 02.08.2018 17:59:02       
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    int top;
    cin >> top;
    int ok = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i + j == 0) continue;
        int cur;
        cin >> cur;
        ok &= (cur == (top + i + j) % 2);
      }
    }
    cout << (ok ? "Yes" : "No") << '\n';
  }
  return 0;
}