#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int a[n + 1][m + 1];
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
      }
    }
    bool flag = true;
    if (a[1][1] > 2 || a[n][1] > 2 || a[1][m] > 2 || a[n][m] > 2) flag = false;
    for (int i = 1; i <= n; i++) {
      for (int j = 2; j <= m - 1; j++) {
        if ((i == 1 || i == n) && a[i][j] > 3) flag = false;
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 2; j <= n - 1; j++) {
        if ((i == 1 || i == m) && a[j][i] > 3) flag = false;
      }
    }
    for (int i = 2; i <= n - 1; i++) {
      for (int j = 2; j <= m - 1; j++) {
        if (a[i][j] > 4) flag = false;
      }
    }
    int ans[n + 1][m + 1];
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        ans[i][j] = 4;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 2; j <= m - 1; j++) {
        if (i == 1 || i == n) ans[i][j] = 3;
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 2; j <= n - 1; j++) {
        if (i == 1 || i == m) ans[j][i] = 3;
      }
    }
    ans[1][1] = 2;
    ans[1][m] = 2;
    ans[n][1] = 2;
    ans[n][m] = 2;
    if (!flag)
      cout << "NO" << '\n';
    else {
      cout << "YES" << '\n';
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) cout << ans[i][j] << ' ';
        cout << '\n';
      }
    }
  }
  return 0;
}
