#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int main() {
  fast();
  int t = 1;
  cin >> t;
  while (t--) {
    int m, n, f = 0;
    cin >> m >> n;
    int mat[m][n], ans[m][n];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) cin >> mat[i][j];
    }
    if (mat[0][0] > 2 || mat[0][n - 1] > 2 || mat[m - 1][0] > 2 ||
        mat[m - 1][n - 1] > 2) {
      cout << "NO" << endl;
      continue;
    }
    for (int i = 1; i < n - 1; i++) {
      if (mat[0][i] > 3 || mat[m - 1][i] > 3) {
        cout << "NO" << endl;
        f = 1;
        break;
      }
    }
    for (int i = 1; i < m - 1; i++) {
      if ((mat[i][0] > 3 || mat[i][n - 1] > 3) && (f == 0)) {
        cout << "NO" << endl;
        f = 1;
        break;
      }
    }
    for (int i = 1; i < m - 1; i++) {
      for (int j = 1; j < n - 1; j++) {
        if (mat[i][j] > 4 && (f == 0)) {
          cout << "NO" << endl;
          f = 1;
          break;
        }
      }
      if (f == 1) break;
    }
    if (f == 1) continue;
    cout << "YES" << endl;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if ((i == 0 && j == 0) || (i == 0 && j == n - 1) ||
            (i == m - 1 && j == 0) || (i == m - 1 && j == n - 1))
          mat[i][j] = 2;
        else if (i == 0 || j == 0 || i == m - 1 || j == n - 1)
          mat[i][j] = 3;
        else
          mat[i][j] = 4;
        cout << mat[i][j] << " ";
      }
      cout << endl;
    }
  }
  return 0;
}
