#include <bits/stdc++.h>
using namespace std;
int a[301][301], h, w, dp[301][301];
int used[601][601];
int dx[] = {0, 1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 0, 1, -1, 1, 1, -1, -1};
int read() {
  int x;
  scanf("%d", &x);
  return x;
}
bool check(int x, int y) {
  if (x >= 1 && x <= h && y >= 1 && y <= w && !used[x][y])
    return true;
  else
    return false;
}
void dfs(int x, int y, int component) {
  used[x][y] = component;
  for (int i = 1; i <= 8; i++) {
    int kx = dx[i] + x, ky = dy[i] + y;
    if (check(kx, ky) && a[x][y] == a[kx][ky]) {
      dfs(kx, ky, component);
    }
  }
}
void init() {
  for (int i = 0; i <= 600; i++) {
    for (int j = 0; j <= 600; j++) {
      used[i][j] = 0;
    }
  }
}
int main() {
  int q;
  q = read();
  while (q--) {
    h = read(), w = read();
    init();
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        a[i][j] = read();
      }
    }
    int cntfirst = 0, cntsecond = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (!used[i][j]) dfs(i, j, cntfirst + 1), cntfirst++;
      }
    }
    for (int i = h / 2 + 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        dp[i - h / 2][j] = a[i][j];
      }
    }
    for (int i = 1; i <= h / 2; i++) {
      for (int j = 1; j <= w; j++) {
        dp[i + h / 2][j] = a[i][j];
      }
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        a[i][j] = dp[i][j];
      }
    }
    init();
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (!used[i][j]) dfs(i, j, cntsecond + 1), cntsecond++;
      }
    }
    if (cntfirst <= cntsecond) {
      printf("NO\n");
    } else {
      printf("YES\n");
    }
  }
}
