#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const int N = 1 << 20;
const int P = 1e9 + 7;
int a[1000][1000];
double calc(int n, int m) {
  double ret = 0;
  int i, j;
  i = n / 2 - 1;
  for (j = 0; j < m; j++) ret += abs(a[i][j] - a[i + 1][j]);
  return ret;
}
int main() {
  int N;
  scanf("%d", &N);
  while (N--) {
    int n, m;
    int i, j, k;
    scanf("%d%d", &n, &m);
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++) scanf("%d", &a[i][j]);
    double a1 = calc(n, m);
    for (i = 0; i < n / 2; i++)
      for (j = 0; j < m; j++) swap(a[i][j], a[i + n / 2][j]);
    double a2 = calc(n, m);
    if (a2 < a1)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
