#include <bits/stdc++.h>
using namespace std;
const int SIZE = 640;
int h, w;
int matr[SIZE][SIZE];
int main() {
  int q;
  scanf("%d", &q);
  for (int tt = 0; tt < q; tt++) {
    scanf("%d%d", &h, &w);
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) scanf("%d", &matr[i][j]);
    int cut = h / 2;
    int diffZero = 0;
    int diffSwap = 0;
    for (int j = 0; j < w; j++) {
      diffZero += abs(matr[cut - 1][j] - matr[cut][j]);
      diffSwap += abs(matr[0][j] - matr[h - 1][j]);
    }
    ((void)0);
    printf(diffZero < diffSwap ? "NO\n" : "YES\n");
  }
  return 0;
}
