#include <bits/stdc++.h>
using namespace std;
int abs(int x) { return x * x; }
int dist(const vector<int> &a, const vector<int> &b) {
  int ret = 0;
  for (int i = 0; i < a.size(); ++i) {
    ret += abs(a[i] - b[i]);
  }
  return ret;
}
int main() {
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; ++q) {
    int h, w;
    cin >> h >> w;
    vector<int> top, middle_top, middle_bot, bot;
    top.resize(w);
    middle_top.resize(w);
    middle_bot.resize(w);
    bot.resize(w);
    for (int row = 0; row < h; ++row) {
      if (row == 0) {
        for (auto &c : top) cin >> c;
      } else if (row == h / 2 - 1) {
        for (auto &c : middle_top) cin >> c;
      } else if (row == h / 2) {
        for (auto &c : middle_bot) cin >> c;
      } else if (row == h - 1) {
        for (auto &c : bot) cin >> c;
      } else {
        int discard;
        for (int i = 0; i < w; ++i) cin >> discard;
      }
    }
    cout << ((dist(middle_top, middle_bot) <= dist(bot, top)) ? "NO" : "YES")
         << endl;
  }
  return 0;
}
