#include <bits/stdc++.h>
using namespace std;
int grid[500][500];
int main() {
  int q, h, w;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &h, &w);
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        scanf("%d", &grid[i][j]);
      }
    }
    double ans = 0;
    for (int j = 0; j < w; ++j) {
      ans += (grid[0][j] - grid[h - 1][j]) * (grid[0][j] - grid[h - 1][j]);
    }
    ans /= 255 * 255 * w;
    cout << (ans < 0.015 ? "YES" : "NO") << "\n";
  }
  return 0;
}
