#include <bits/stdc++.h>
using namespace std;
const int N = 605;
const int grad = 8;
int n, m, a[N][N], b[N][N];
bool u[N][N], f[N][N];
queue<pair<int, int> > q1, q2;
void can1(int px, int py, int x, int y) {
  if (x <= 0 || y <= 0 || x > n || y > m || u[x][y] ||
      abs(a[px][py] - a[x][y]) >= grad)
    return;
  q1.push(pair<int, int>(x, y));
  u[x][y] = true;
}
void bfs1(int x, int y) {
  q1.push(pair<int, int>(x, y));
  while (!q1.empty()) {
    x = q1.front().first;
    y = q1.front().second;
    q1.pop();
    can1(x, y, x - 1, y);
    can1(x, y, x, y - 1);
    can1(x, y, x + 1, y);
    can1(x, y, x, y + 1);
    can1(x, y, x - 1, y - 1);
    can1(x, y, x + 1, y + 1);
  }
}
void can2(int px, int py, int x, int y) {
  if (x <= 0 || y <= 0 || x > n || y > m || f[x][y] ||
      abs(b[px][py] - b[x][y]) >= grad)
    return;
  q2.push(pair<int, int>(x, y));
  f[x][y] = true;
}
void bfs2(int x, int y) {
  q2.push(pair<int, int>(x, y));
  while (!q2.empty()) {
    x = q2.front().first;
    y = q2.front().second;
    q2.pop();
    can2(x, y, x - 1, y);
    can2(x, y, x, y - 1);
    can2(x, y, x + 1, y);
    can2(x, y, x, y + 1);
    can2(x, y, x - 1, y - 1);
    can2(x, y, x + 1, y + 1);
  }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int test;
  cin >> test;
  while (test--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
      }
    }
    memset(u, 0, sizeof(u));
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= n / 2; i++) {
      for (int j = 1; j <= m; j++) {
        b[i][j] = a[i + n / 2][j];
        b[i + n / 2][j] = a[i][j];
      }
    }
    int k1 = 0, k2 = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (!u[i][j]) {
          bfs1(i, j);
          k1++;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (!f[i][j]) {
          bfs2(i, j);
          k2++;
        }
      }
    }
    if (k1 < k2)
      printf("NO\n");
    else
      printf("YES\n");
  }
}
