#include <bits/stdc++.h>
using namespace std;
namespace patch {
template <typename T>
std::string to_string(const T& n) {
  std::ostringstream stm;
  stm << n;
  return stm.str();
}
}  // namespace patch
long long modpow(long long base, long long exponent, long long modulus) {
  long long result = 1;
  while (exponent > 0) {
    if (exponent % 2 == 1) result = (result * base) % modulus;
    exponent = exponent >> 1;
    base = (base * base) % modulus;
  }
  return result;
}
long long gcd(long long u, long long v) { return (v != 0) ? gcd(v, u % v) : u; }
long long mulmod(long long a, long long b, long long c) {
  long long x = 0, y = a % c;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x + y) % c;
    }
    y = (y * 2) % c;
    b /= 2;
  }
  return x % c;
}
int q, h, w;
short int vals[601][601];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> h >> w;
    for (int j = 0; j < h; ++j) {
      for (int k = 0; k < w; ++k) {
        cin >> vals[j][k];
      }
    }
    int ansidx = 0;
    long long delta = 0;
    for (int j = 0; j < w; ++j) delta += abs(vals[1][j] - vals[0][j]);
    long long maxx = delta;
    for (int j = 2; j < h; ++j) {
      long long temp = 0;
      for (int k = 0; k < w; ++k) {
        temp += abs(vals[j][k] - vals[j - 1][k]);
      }
      if (temp > maxx) {
        maxx = temp;
        ansidx = j;
      }
      delta = temp;
    }
    if (ansidx == h / 2 || ansidx == h / 2 - 1 || ansidx == h / 2 + 1)
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
