#include<bits/stdc++.h>
#define ll long long
#define lu long unsigned
#define ull unsigned long long
#define vi vector<int>

using namespace std;

long long int  binpow(long long int a, long long int b) {
    long long int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll int nthRoot(ll int A, ll int N){
    ll int xPre= rand() %10;
    ll int eps=1e-3;
    ll int delX =INT_MAX;
    ll int xK;
    while(delX>eps){
        xK=((N-1)*xPre+A/binpow(xPre,N-1))/N;
        delX=abs(xK-xPre);
        xPre=xK;
    }
    return xK;
}

bool sortbysec(const pair<string,int> &a,
              const pair<string,int> &b)
{
    return (a.second < b.second);
}
bool sortpair(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
        return (a.second<b.second);
    else
        return(a.first<b.first);
}
bool s(const string &a, const string &b){
    bool ans;
    return (a.size()<b.size());
}
bool isPrime(int val){
    for(int i=2;i*i<=val;i++){
        if(val%i==0)
            return false;
    }
    return true;
}
ll int conc(ll int a,ll int b){
   stringstream ss;
   ss<<a<<b;
   ll int ans;
   ss>>ans;
   ss.clear();
   return ans;
}
ll int binexp(int a,int b){
    ll int res=1;
    while(b!=0){
        if(b&1){
            res*=a;
            b--;
        }else{
            a*=a;
            b/=2;
        }
    }
    return res;
}
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }else{
        return gcd(a%b,b);
    }
}
vector<int> ar[60];
int vis[60]={0};

void dfs(int v){
    vis[v]=1;
    cout<<v<<"->";
    for(int c:ar[v]){
        if(vis[c]==0){
            dfs(c);
        }
    }
}
int mindig(ll int n){
    int c=9;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=min(c,s[i]-'0');
    }
    return c;

}

int maxdig(ll int n){
    int c=0;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=max(c,s[i]-'0');
    }
    return c;

}

void solve(){
    int n,m;
    cin>>n>>m;
    int v[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>v[i][j];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int k=0;
            if(v[i][j]==0)
                continue;
            if(i>0){
                if(v[i-1][j]!=0)
                    k++;
            }
            if(i<n-1){
                if(v[i+1][j]!=0)
                    k++;
            }
            if(j>0){
                if(v[i][j-1]!=0)
                    k++;
            }
            if(j<m-1)
                if(v[i][j+1]!=0)
                    k++;
            //cout<<k<<endl;
            if(k>v[i][j]){
                v[i][j]=k;
            }else if(k<v[i][j]){
                if(v[i][j]>4||(((i==0&&(j==0||j==m-1))||(i==n-1&&(j==0||j==m-1)))&&v[i][j]>2)||((i==0||j==0||i==n-1||j==m-1)&&v[i][j]>3)){
                    cout<<"NO\n";
                    return;
                }else{
                    int x=v[i][j]-k;
                    if(i>0){
                        if(v[i-1][j]==0){
                            v[i-1][j]=1;
                            i=-1;
                            j=-1;
                            continue;
                        }
                    }
                    if(j>0){
                        if(v[i][j-1]==0){
                            v[i][j-1]=1;
                            i=-1;
                            j=-1;
                            continue;
                        }
                    }
                    if(i<n-1){
                        if(v[i+1][j]==0){
                            v[i+1][j]=1;
                            x--;
                            if(x==0)
                            continue;
                        }
                    }
                    if(j<m-1){
                        if(v[i][j+1]==0){
                            v[i][j+1]=1;
                        }
                    }

                }
            }
        }
    }
    cout<<"YES\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

}

int main(){
    int t;
    cin>>t;
    while(t--)
        solve();

    return 0;
}

//1-(3+5-2+(3+19-(3-1-4+(9-4-(4-(1+(3)-2)-5)+8-(3-5)-1)-4)-5)-4+3-9)-4-(3+2-5)-10


#include<bits/stdc++.h>
#define ll long long
#define lu long unsigned
#define ull unsigned long long
#define vi vector<int>

using namespace std;

long long int  binpow(long long int a, long long int b) {
    long long int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll int nthRoot(ll int A, ll int N){
    ll int xPre= rand() %10;
    ll int eps=1e-3;
    ll int delX =INT_MAX;
    ll int xK;
    while(delX>eps){
        xK=((N-1)*xPre+A/binpow(xPre,N-1))/N;
        delX=abs(xK-xPre);
        xPre=xK;
    }
    return xK;
}

bool sortbysec(const pair<string,int> &a,
              const pair<string,int> &b)
{
    return (a.second < b.second);
}
bool sortpair(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
        return (a.second<b.second);
    else
        return(a.first<b.first);
}
bool s(const string &a, const string &b){
    bool ans;
    return (a.size()<b.size());
}
bool isPrime(int val){
    for(int i=2;i*i<=val;i++){
        if(val%i==0)
            return false;
    }
    return true;
}
ll int conc(ll int a,ll int b){
   stringstream ss;
   ss<<a<<b;
   ll int ans;
   ss>>ans;
   ss.clear();
   return ans;
}
ll int binexp(int a,int b){
    ll int res=1;
    while(b!=0){
        if(b&1){
            res*=a;
            b--;
        }else{
            a*=a;
            b/=2;
        }
    }
    return res;
}
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }else{
        return gcd(a%b,b);
    }
}
vector<int> ar[60];
int vis[60]={0};

void dfs(int v){
    vis[v]=1;
    cout<<v<<"->";
    for(int c:ar[v]){
        if(vis[c]==0){
            dfs(c);
        }
    }
}
int mindig(ll int n){
    int c=9;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=min(c,s[i]-'0');
    }
    return c;

}

int maxdig(ll int n){
    int c=0;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=max(c,s[i]-'0');
    }
    return c;

}

void solve(){
    int n,m;
    cin>>n>>m;
    int v[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>v[i][j];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int k=0;
            if(v[i][j]==0)
                continue;
            if(i>0){
                if(v[i-1][j]!=0)
                    k++;
            }
            if(i<n-1){
                if(v[i+1][j]!=0)
                    k++;
            }
            if(j>0){
                if(v[i][j-1]!=0)
                    k++;
            }
            if(j<m-1)
                if(v[i][j+1]!=0)
                    k++;
            //cout<<k<<endl;
            if(k>v[i][j]){
                v[i][j]=k;
            }else if(k<v[i][j]){
                if(v[i][j]>4||(((i==0&&(j==0||j==m-1))||(i==n-1&&(j==0||j==m-1)))&&v[i][j]>2)||((i==0||j==0||i==n-1||j==m-1)&&v[i][j]>3)){
                    cout<<"NO\n";
                    return;
                }else{
                    int x=v[i][j]-k;
                    if(i>0){
                        if(v[i-1][j]==0){
                            v[i-1][j]=1;
                            i--;
                            j--;
                            continue;
                        }
                    }
                    if(j>0){
                        if(v[i][j-1]==0){
                            v[i][j-1]=1;
                            j-=2;
                            continue;
                        }
                    }
                    if(i<n-1){
                        if(v[i+1][j]==0){
                            v[i+1][j]=1;
                            if(x==0)
                            continue;
                        }
                    }
                    if(j<m-1){
                        if(v[i][j+1]==0){
                            v[i][j+1]=1;
                        }
                    }

                }
            }
        }
    }
    cout<<"YES\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

}

int main(){
    int t;
    cin>>t;
    while(t--)
        solve();

    return 0;
}

//1-(3+5-2+(3+19-(3-1-4+(9-4-(4-(1+(3)-2)-5)+8-(3-5)-1)-4)-5)-4+3-9)-4-(3+2-5)-10


#include<bits/stdc++.h>
#define ll long long
#define lu long unsigned
#define ull unsigned long long
#define vi vector<int>

using namespace std;

long long int  binpow(long long int a, long long int b) {
    long long int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll int nthRoot(ll int A, ll int N){
    ll int xPre= rand() %10;
    ll int eps=1e-3;
    ll int delX =INT_MAX;
    ll int xK;
    while(delX>eps){
        xK=((N-1)*xPre+A/binpow(xPre,N-1))/N;
        delX=abs(xK-xPre);
        xPre=xK;
    }
    return xK;
}

bool sortbysec(const pair<string,int> &a,
              const pair<string,int> &b)
{
    return (a.second < b.second);
}
bool sortpair(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
        return (a.second<b.second);
    else
        return(a.first<b.first);
}
bool s(const string &a, const string &b){
    bool ans;
    return (a.size()<b.size());
}
bool isPrime(int val){
    for(int i=2;i*i<=val;i++){
        if(val%i==0)
            return false;
    }
    return true;
}
ll int conc(ll int a,ll int b){
   stringstream ss;
   ss<<a<<b;
   ll int ans;
   ss>>ans;
   ss.clear();
   return ans;
}
ll int binexp(int a,int b){
    ll int res=1;
    while(b!=0){
        if(b&1){
            res*=a;
            b--;
        }else{
            a*=a;
            b/=2;
        }
    }
    return res;
}
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }else{
        return gcd(a%b,b);
    }
}
vector<int> ar[60];
int vis[60]={0};

void dfs(int v){
    vis[v]=1;
    cout<<v<<"->";
    for(int c:ar[v]){
        if(vis[c]==0){
            dfs(c);
        }
    }
}
int mindig(ll int n){
    int c=9;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=min(c,s[i]-'0');
    }
    return c;

}

int maxdig(ll int n){
    int c=0;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=max(c,s[i]-'0');
    }
    return c;

}

void solve(){
    int n,m;
    cin>>n>>m;
    int v[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>v[i][j];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int k=0;
            if(v[i][j]==0)
                continue;
            if(i>0){
                if(v[i-1][j]!=0)
                    k++;
            }
            if(i<n-1){
                if(v[i+1][j]!=0)
                    k++;
            }
            if(j>0){
                if(v[i][j-1]!=0)
                    k++;
            }
            if(j<m-1)
                if(v[i][j+1]!=0)
                    k++;
            cout<<k<<endl;
            if(k>v[i][j]){
                v[i][j]=k;
            }else if(k<v[i][j]){
                if(v[i][j]>4||(((i==0&&(j==0||j==m-1))||(i==n-1&&(j==0||j==m-1)))&&v[i][j]>2)||((i==0||j==0||i==n-1||j==m-1)&&v[i][j]>3)){
                    cout<<"NO\n";
                    return;
                }else{
                    int x=v[i][j]-k;
                    if(i>0){
                        if(v[i-1][j]==0){
                            v[i-1][j]=1;
                            i--;
                            j--;
                            continue;
                        }
                    }
                    if(j>0){
                        if(v[i][j-1]==0){
                            v[i][j-1]=1;
                            j-=2;
                            continue;
                        }
                    }
                    if(i<n-1){
                        if(v[i+1][j]==0){
                            v[i+1][j]=1;
                            if(x==0)
                            continue;
                        }
                    }
                    if(j<m-1){
                        if(v[i][j+1]==0){
                            v[i][j+1]=1;
                        }
                    }

                }
            }
        }
    }
    cout<<"YES\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

}

int main(){
    int t;
    cin>>t;
    while(t--)
        solve();

    return 0;
}

//1-(3+5-2+(3+19-(3-1-4+(9-4-(4-(1+(3)-2)-5)+8-(3-5)-1)-4)-5)-4+3-9)-4-(3+2-5)-10


#include<bits/stdc++.h>
#define ll long long
#define lu long unsigned
#define ull unsigned long long
#define vi vector<int>

using namespace std;

long long int  binpow(long long int a, long long int b) {
    long long int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll int nthRoot(ll int A, ll int N){
    ll int xPre= rand() %10;
    ll int eps=1e-3;
    ll int delX =INT_MAX;
    ll int xK;
    while(delX>eps){
        xK=((N-1)*xPre+A/binpow(xPre,N-1))/N;
        delX=abs(xK-xPre);
        xPre=xK;
    }
    return xK;
}

bool sortbysec(const pair<string,int> &a,
              const pair<string,int> &b)
{
    return (a.second < b.second);
}
bool sortpair(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
        return (a.second<b.second);
    else
        return(a.first<b.first);
}
bool s(const string &a, const string &b){
    bool ans;
    return (a.size()<b.size());
}
bool isPrime(int val){
    for(int i=2;i*i<=val;i++){
        if(val%i==0)
            return false;
    }
    return true;
}
ll int conc(ll int a,ll int b){
   stringstream ss;
   ss<<a<<b;
   ll int ans;
   ss>>ans;
   ss.clear();
   return ans;
}
ll int binexp(int a,int b){
    ll int res=1;
    while(b!=0){
        if(b&1){
            res*=a;
            b--;
        }else{
            a*=a;
            b/=2;
        }
    }
    return res;
}
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }else{
        return gcd(a%b,b);
    }
}
vector<int> ar[60];
int vis[60]={0};

void dfs(int v){
    vis[v]=1;
    cout<<v<<"->";
    for(int c:ar[v]){
        if(vis[c]==0){
            dfs(c);
        }
    }
}
int mindig(ll int n){
    int c=9;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=min(c,s[i]-'0');
    }
    return c;

}

int maxdig(ll int n){
    int c=0;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=max(c,s[i]-'0');
    }
    return c;

}

void solve(){
    int n,m;
    cin>>n>>m;
    int v[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>v[i][j];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int k=0;
            if(v[i][j]==0)
                continue;
            if(i>0){
                if(v[i-1][j]!=0)
                    k++;
            }
            if(i<n-1){
                if(v[i+1][j]!=0)
                    k++;
            }
            if(j>0){
                if(v[i][j-1]!=0)
                    k++;
            }
            if(j<m-1)
                if(v[i][j+1]!=0)
                    k++;
            //cout<<k<<endl;
            if(k>v[i][j]){
                v[i][j]=k;
            }else if(k<v[i][j]){
                if(v[i][j]>4||(((i==0&&(j==0||j==m-1))||(i==n-1&&(j==0||j==m-1)))&&v[i][j]>2)||((i==0||j==0||i==n-1||j==m-1)&&v[i][j]>3)){
                    cout<<"NO\n";
                    return;
                }else{
                    int x=v[i][j]-k;
                    if(i>0){
                        if(v[i-1][j]==0){
                            v[i-1][j]=1;
                            i=-1;
                            j=-1;
                            continue;
                        }
                    }
                    if(j>0){
                        if(v[i][j-1]==0){
                            v[i][j-1]=1;
                            i=-1
                            j=-1;
                            continue;
                        }
                    }
                    if(i<n-1){
                        if(v[i+1][j]==0){
                            v[i+1][j]=1;
                            x--;
                            if(x==0)
                            continue;
                        }
                    }
                    if(j<m-1){
                        if(v[i][j+1]==0){
                            v[i][j+1]=1;
                        }
                    }

                }
            }
        }
    }
    cout<<"YES\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

}

int main(){
    int t;
    cin>>t;
    while(t--)
        solve();

    return 0;
}

//1-(3+5-2+(3+19-(3-1-4+(9-4-(4-(1+(3)-2)-5)+8-(3-5)-1)-4)-5)-4+3-9)-4-(3+2-5)-10


#include<bits/stdc++.h>
#define ll long long
#define lu long unsigned
#define ull unsigned long long
#define vi vector<int>

using namespace std;

long long int  binpow(long long int a, long long int b) {
    long long int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll int nthRoot(ll int A, ll int N){
    ll int xPre= rand() %10;
    ll int eps=1e-3;
    ll int delX =INT_MAX;
    ll int xK;
    while(delX>eps){
        xK=((N-1)*xPre+A/binpow(xPre,N-1))/N;
        delX=abs(xK-xPre);
        xPre=xK;
    }
    return xK;
}

bool sortbysec(const pair<string,int> &a,
              const pair<string,int> &b)
{
    return (a.second < b.second);
}
bool sortpair(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
        return (a.second<b.second);
    else
        return(a.first<b.first);
}
bool s(const string &a, const string &b){
    bool ans;
    return (a.size()<b.size());
}
bool isPrime(int val){
    for(int i=2;i*i<=val;i++){
        if(val%i==0)
            return false;
    }
    return true;
}
ll int conc(ll int a,ll int b){
   stringstream ss;
   ss<<a<<b;
   ll int ans;
   ss>>ans;
   ss.clear();
   return ans;
}
ll int binexp(int a,int b){
    ll int res=1;
    while(b!=0){
        if(b&1){
            res*=a;
            b--;
        }else{
            a*=a;
            b/=2;
        }
    }
    return res;
}
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }else{
        return gcd(a%b,b);
    }
}
vector<int> ar[60];
int vis[60]={0};

void dfs(int v){
    vis[v]=1;
    cout<<v<<"->";
    for(int c:ar[v]){
        if(vis[c]==0){
            dfs(c);
        }
    }
}
int mindig(ll int n){
    int c=9;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=min(c,s[i]-'0');
    }
    return c;

}

int maxdig(ll int n){
    int c=0;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=max(c,s[i]-'0');
    }
    return c;

}

void solve(){
    int n,m;
    cin>>n>>m;
    int v[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>v[i][j];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(v[i][j]>4||(((i==0&&(j==0||j==m-1))||(i==n-1&&(j==0||j==m-1)))&&v[i][j]>2)||((i==0||j==0||i==n-1||j==m-1)&&v[i][j]>3)){
                cout<<"NO\n";
                return;
            }else{
                int x=4;
                if(i==0||i==n-1)
                    x--;
                if(j==0||j==m-1)
                    x--;
                v[i][j]=x;
            }
        }
    }
    cout<<"YES\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

}

int main(){
    int t;
    cin>>t;
    while(t--)
        solve();

    return 0;
}

//1-(3+5-2+(3+19-(3-1-4+(9-4-(4-(1+(3)-2)-5)+8-(3-5)-1)-4)-5)-4+3-9)-4-(3+2-5)-10


#include<bits/stdc++.h>
#define ll long long
#define lu long unsigned
#define ull unsigned long long
#define vi vector<int>

using namespace std;

long long int  binpow(long long int a, long long int b) {
    long long int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

ll int nthRoot(ll int A, ll int N){
    ll int xPre= rand() %10;
    ll int eps=1e-3;
    ll int delX =INT_MAX;
    ll int xK;
    while(delX>eps){
        xK=((N-1)*xPre+A/binpow(xPre,N-1))/N;
        delX=abs(xK-xPre);
        xPre=xK;
    }
    return xK;
}

bool sortbysec(const pair<string,int> &a,
              const pair<string,int> &b)
{
    return (a.second < b.second);
}
bool sortpair(const pair<int,int> &a,
              const pair<int,int> &b)
{
    if(a.first==b.first)
        return (a.second<b.second);
    else
        return(a.first<b.first);
}
bool s(const string &a, const string &b){
    bool ans;
    return (a.size()<b.size());
}
bool isPrime(int val){
    for(int i=2;i*i<=val;i++){
        if(val%i==0)
            return false;
    }
    return true;
}
ll int conc(ll int a,ll int b){
   stringstream ss;
   ss<<a<<b;
   ll int ans;
   ss>>ans;
   ss.clear();
   return ans;
}
ll int binexp(int a,int b){
    ll int res=1;
    while(b!=0){
        if(b&1){
            res*=a;
            b--;
        }else{
            a*=a;
            b/=2;
        }
    }
    return res;
}
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }else{
        return gcd(a%b,b);
    }
}
vector<int> ar[60];
int vis[60]={0};

void dfs(int v){
    vis[v]=1;
    cout<<v<<"->";
    for(int c:ar[v]){
        if(vis[c]==0){
            dfs(c);
        }
    }
}
int mindig(ll int n){
    int c=9;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=min(c,s[i]-'0');
    }
    return c;

}

int maxdig(ll int n){
    int c=0;
    stringstream ss;
    ss<<n;
    string s;
    ss>>s;
    for(int i=0;i<s.size();i++){
        c=max(c,s[i]-'0');
    }
    return c;

}

void solve(){
    int n,m;
    cin>>n>>m;
    int v[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>v[i][j];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int k=0;
            if(v[i][j]==0)
                continue;
            if(i>0){
                if(v[i-1][j]!=0)
                    k++;
            }
            if(i<n-1){
                if(v[i+1][j]!=0)
                    k++;
            }
            if(j>0){
                if(v[i][j-1]!=0)
                    k++;
            }
            if(j<n-1)
                if(v[i][j+1]!=0)
                    k++;
            if(k>v[i][j]){
                v[i][j]=k;
            }else if(k<v[i][j]){
                if(v[i][j]>4||(((i==0&&(j==0||j==m-1))||(i==n-1&&(j==0||j==m-1)))&&v[i][j]>2)||((i==0||j==0||i==n-1||j==m-1)&&v[i][j]>3)){
                    cout<<"NO\n";
                    return;
                }else{
                    int x=v[i][j]-k;
                    if(i>0){
                        if(v[i-1][j]==0){
                            v[i-1][j]=1;
                            i--;
                            j--;
                            continue;
                        }
                    }
                    if(j>0){
                        if(v[i][j-1]==0){
                            v[i][j-1]=1;
                            j-=2;
                            continue;
                        }
                    }
                    if(i<n-1){
                        if(v[i+1][j]==0){
                            v[i+1][j]=1;
                            if(x==0)
                            continue;
                        }
                    }
                    if(j<m-1){
                        if(v[i][j+1]==0){
                            v[i][j+1]=1;
                        }
                    }

                }
            }
        }
    }
    cout<<"YES\n";
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

}

int main(){
    int t;
    cin>>t;
    while(t--)
        solve();

    return 0;
}

//1-(3+5-2+(3+19-(3-1-4+(9-4-(4-(1+(3)-2)-5)+8-(3-5)-1)-4)-5)-4+3-9)-4-(3+2-5)-10


/*
   And you wish I would'nt wait for you ....
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll , ll>
#define pci pair<char,int>
#define fi first
#define se second
int isqrt(int n) {
  return sqrtl(n);
}
//map<pii,int> m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y
//template<class T>
//using min_heap = priority_queue<T , vector<T> , greater<T>>;
//template<class T>
//using max_heap = priority_queue<T>;
//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});
//min_heap<pair<ll , size_t>> byx2;
//max_heap<pair<ll , size_t>> byy1;
//using vll = vector<ll>
//vi gone(n , 0);
//sortdesc(arr, arr+n, greater<int>());
// x | (1 << n) returns x with nth bit set
//  x & (-x) returns the rightmost 1 of x
//getline(cin , s);
// vector<vector<int>> dist(size of the outer vector = n , vector<int>(size of EACH VECTOR in the vector = 10));
/*for(int k=0;k<4;k++)
use this for four adjacent ;
                {
                    int ni = i + int (k = 0) - int (k = 1);
                    int nj = j + int (k = 2) - int (k = 3);
                    if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;
                }
                */
#define pb push_back
#define int long long
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
#define MAXN   100001

// stores smallest prime factor for every number
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t = 1;
   cin>>t;
    while(t--)
    {
       int n,m;
       cin>>n>>m;
       int arr[n][m];
       int f = 1;
       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int cnt = 4;
            cin>>arr[i][j];
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == m-1){cnt -= 1;}
            if(arr[i][j] > cnt){f=0;}
        }
       }
       if(f==0){cout<<"NO\n";}
       else
       {
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
                int y = 0;
                int h = max(arr[i][j] - cnt,y);
                if(i!=0 && h)
                {
                    if(arr[i-1][j] == 0){h-=1;arr[i-1][j]=1;}
                }
                if(i!=n-1 && h)
                {
                    if(arr[i+1][j] == 0){h-=1;arr[i+1][j]=1;}
                }
                if(j!=m-1 && h)
                {
                    if(arr[i][j+1] == 0){h-=1;arr[i][j+1]=1;}
                }
                if(j!=0 && h)
                {
                    if(arr[i][j-1] == 0){h-=1;arr[i][j-1]=1;}
                }

                }
            }

                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(arr[i][j]!=0)
                        {int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
            arr[i][j] = cnt;}
                cout<<arr[i][j]<<" ";
                    }
                    cout<<"\n";
                }

        }

    }
    return 0;
}
/*
     ....But I always do.....
*/


/*
   And you wish I would'nt wait for you ....
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll , ll>
#define pci pair<char,int>
#define fi first
#define se second
int isqrt(int n) {
  return sqrtl(n);
}
//map<pii,int> m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y
//template<class T>
//using min_heap = priority_queue<T , vector<T> , greater<T>>;
//template<class T>
//using max_heap = priority_queue<T>;
//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});
//min_heap<pair<ll , size_t>> byx2;
//max_heap<pair<ll , size_t>> byy1;
//using vll = vector<ll>
//vi gone(n , 0);
//sortdesc(arr, arr+n, greater<int>());
// x | (1 << n) returns x with nth bit set
//  x & (-x) returns the rightmost 1 of x
//getline(cin , s);
// vector<vector<int>> dist(size of the outer vector = n , vector<int>(size of EACH VECTOR in the vector = 10));
/*for(int k=0;k<4;k++)
use this for four adjacent ;
                {
                    int ni = i + int (k = 0) - int (k = 1);
                    int nj = j + int (k = 2) - int (k = 3);
                    if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;
                }
                */
#define pb push_back
#define int long long
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
#define MAXN   100001

// stores smallest prime factor for every number
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t = 1;
   cin>>t;
    for(int i1=0;i1<t;i1++)
    {

       int n,m;
       cin>>n>>m;
       int arr[n][m];
       int f = 1;
       int d = 0;
       if(i1 == 25){d=1;}
      // if(d == 1){cout<<n<<" "<<m<<"\n";}
       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int cnt = 4;
            cin>>arr[i][j];
            if(d){cout<<arr[i][j]<<" ";}
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == n-1){cnt -= 1;}
            if(arr[i][j] > cnt){f=0;}
        }
        if(d){cout<<"\n";}
       }
       if(f==0){cout<<"NO\n";}
       else
       {
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
                int y = 0;
                int h = max(arr[i][j] - cnt,y);
                if(i!=0 && h)
                {
                    if(arr[i-1][j] == 0){h-=1;arr[i-1][j]=1;}
                }
                if(i!=n-1 && h)
                {
                    if(arr[i+1][j] == 0){h-=1;arr[i+1][j]=1;}
                }
                if(j!=m-1 && h)
                {
                    if(arr[i][j+1] == 0){h-=1;arr[i][j+1]=1;}
                }
                if(j!=0 && h)
                {
                    if(arr[i][j-1] == 0){h-=1;arr[i][j-1]=1;}
                }

                }
            }

                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(arr[i][j]!=0)
                        {int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
            arr[i][j] = cnt;}
                cout<<arr[i][j]<<" ";
                    }
                    cout<<"\n";
                }

        }

    }
    return 0;
}
/*
     ....But I always do.....
*/


/*
   And you wish I would'nt wait for you ....
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll , ll>
#define pci pair<char,int>
#define fi first
#define se second
int isqrt(int n) {
  return sqrtl(n);
}
//map<pii,int> m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y
//template<class T>
//using min_heap = priority_queue<T , vector<T> , greater<T>>;
//template<class T>
//using max_heap = priority_queue<T>;
//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});
//min_heap<pair<ll , size_t>> byx2;
//max_heap<pair<ll , size_t>> byy1;
//using vll = vector<ll>
//vi gone(n , 0);
//sortdesc(arr, arr+n, greater<int>());
// x | (1 << n) returns x with nth bit set
//  x & (-x) returns the rightmost 1 of x
//getline(cin , s);
// vector<vector<int>> dist(size of the outer vector = n , vector<int>(size of EACH VECTOR in the vector = 10));
/*for(int k=0;k<4;k++)
use this for four adjacent ;
                {
                    int ni = i + int (k = 0) - int (k = 1);
                    int nj = j + int (k = 2) - int (k = 3);
                    if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;
                }
                */
#define pb push_back
#define int long long
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
#define MAXN   100001

// stores smallest prime factor for every number
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t = 1;
   cin>>t;
    while(t--)
    {
       int n,m;
       cin>>n>>m;
       int arr[n][m];
       int f = 1;
       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int cnt = 4;
            cin>>arr[i][j];
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == n-1){cnt -= 1;}
            if(arr[i][j] > cnt){f=0;}
        }
       }
       if(f==0){cout<<"NO\n";}
       else
       {
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=n-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
                int y = 0;
                int h = max(arr[i][j] - cnt,y);
                if(i!=0 && h)
                {
                    if(arr[i-1][j] == 0){h-=1;arr[i-1][j]=1;}
                }
                if(i!=n-1 && h)
                {
                    if(arr[i+1][j] == 0){h-=1;arr[i+1][j]=1;}
                }
                if(j!=n-1 && h)
                {
                    if(arr[i][j+1] == 0){h-=1;arr[i][j+1]=1;}
                }
                if(j!=0 && h)
                {
                    if(arr[i][j-1] == 0){h-=1;arr[i][j-1]=1;}
                }

                }
            }

                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(arr[i][j]!=0)
                        {int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=n-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
            arr[i][j] = cnt;}
                cout<<arr[i][j]<<" ";
                    }
                    cout<<"\n";
                }

        }

    }
    return 0;
}
/*
     ....But I always do.....
*/


/*
   And you wish I would'nt wait for you ....
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll , ll>
#define pci pair<char,int>
#define fi first
#define se second
int isqrt(int n) {
  return sqrtl(n);
}
//map<pii,int> m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y
//template<class T>
//using min_heap = priority_queue<T , vector<T> , greater<T>>;
//template<class T>
//using max_heap = priority_queue<T>;
//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});
//min_heap<pair<ll , size_t>> byx2;
//max_heap<pair<ll , size_t>> byy1;
//using vll = vector<ll>
//vi gone(n , 0);
//sortdesc(arr, arr+n, greater<int>());
// x | (1 << n) returns x with nth bit set
//  x & (-x) returns the rightmost 1 of x
//getline(cin , s);
// vector<vector<int>> dist(size of the outer vector = n , vector<int>(size of EACH VECTOR in the vector = 10));
/*for(int k=0;k<4;k++)
use this for four adjacent ;
                {
                    int ni = i + int (k = 0) - int (k = 1);
                    int nj = j + int (k = 2) - int (k = 3);
                    if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;
                }
                */
#define pb push_back
#define int long long
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
#define MAXN   100001

// stores smallest prime factor for every number
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t = 1;
   cin>>t;
    while(t--)
    {
       int n,m;
       cin>>n>>m;
       int arr[n][m];
       int f = 1;
       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int cnt = 4;
            cin>>arr[i][j];
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == n-1){cnt -= 1;}
            if(arr[i][j] > cnt){f=0;}
        }
       }
       if(f==0){cout<<"NO\n";}
       else
       {
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
                int y = 0;
                int h = max(arr[i][j] - cnt,y);
                if(i!=0 && h)
                {
                    if(arr[i-1][j] == 0){h-=1;arr[i-1][j]=1;}
                }
                if(i!=n-1 && h)
                {
                    if(arr[i+1][j] == 0){h-=1;arr[i+1][j]=1;}
                }
                if(j!=m-1 && h)
                {
                    if(arr[i][j+1] == 0){h-=1;arr[i][j+1]=1;}
                }
                if(j!=0 && h)
                {
                    if(arr[i][j-1] == 0){h-=1;arr[i][j-1]=1;}
                }

                }
            }

                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(arr[i][j]!=0)
                        {int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
            arr[i][j] = cnt;}
                cout<<arr[i][j]<<" ";
                    }
                    cout<<"\n";
                }

        }

    }
    return 0;
}
/*
     ....But I always do.....
*/


/*
   And you wish I would'nt wait for you ....
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll , ll>
#define pci pair<char,int>
#define fi first
#define se second
int isqrt(int n) {
  return sqrtl(n);
}
//map<pii,int> m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y
//template<class T>
//using min_heap = priority_queue<T , vector<T> , greater<T>>;
//template<class T>
//using max_heap = priority_queue<T>;
//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});
//min_heap<pair<ll , size_t>> byx2;
//max_heap<pair<ll , size_t>> byy1;
//using vll = vector<ll>
//vi gone(n , 0);
//sortdesc(arr, arr+n, greater<int>());
// x | (1 << n) returns x with nth bit set
//  x & (-x) returns the rightmost 1 of x
//getline(cin , s);
// vector<vector<int>> dist(size of the outer vector = n , vector<int>(size of EACH VECTOR in the vector = 10));
/*for(int k=0;k<4;k++)
use this for four adjacent ;
                {
                    int ni = i + int (k = 0) - int (k = 1);
                    int nj = j + int (k = 2) - int (k = 3);
                    if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;
                }
                */
#define pb push_back
#define int long long
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
#define MAXN   100001

// stores smallest prime factor for every number
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t = 1;
   cin>>t;
    for(int i1=0;i1<t;i1++)
    {

       int n,m;
       cin>>n>>m;
       int arr[n][m];
       int f = 1;
       int d = 0;
       if(i1 == 25){d=1;}
       if(d == 1){cout<<n<<" "<<m<<"\n";}
       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int cnt = 4;
            cin>>arr[i][j];
            if(d){cout<<arr[i][j]<<" ";}
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == n-1){cnt -= 1;}
            if(arr[i][j] > cnt){f=0;}
        }
        if(d){cout<<"\n";}
       }
       if(f==0){cout<<"NO\n";}
       else
       {
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
                int y = 0;
                int h = max(arr[i][j] - cnt,y);
                if(i!=0 && h)
                {
                    if(arr[i-1][j] == 0){h-=1;arr[i-1][j]=1;}
                }
                if(i!=n-1 && h)
                {
                    if(arr[i+1][j] == 0){h-=1;arr[i+1][j]=1;}
                }
                if(j!=m-1 && h)
                {
                    if(arr[i][j+1] == 0){h-=1;arr[i][j+1]=1;}
                }
                if(j!=0 && h)
                {
                    if(arr[i][j-1] == 0){h-=1;arr[i][j-1]=1;}
                }

                }
            }

                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(arr[i][j]!=0)
                        {int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
            arr[i][j] = cnt;}
                cout<<arr[i][j]<<" ";
                    }
                    cout<<"\n";
                }

        }

    }
    return 0;
}
/*
     ....But I always do.....
*/


/*
   And you wish I would'nt wait for you ....
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll , ll>
#define pci pair<char,int>
#define fi first
#define se second
int isqrt(int n) {
  return sqrtl(n);
}
//map<pii,int> m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y
//template<class T>
//using min_heap = priority_queue<T , vector<T> , greater<T>>;
//template<class T>
//using max_heap = priority_queue<T>;
//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});
//min_heap<pair<ll , size_t>> byx2;
//max_heap<pair<ll , size_t>> byy1;
//using vll = vector<ll>
//vi gone(n , 0);
//sortdesc(arr, arr+n, greater<int>());
// x | (1 << n) returns x with nth bit set
//  x & (-x) returns the rightmost 1 of x
//getline(cin , s);
// vector<vector<int>> dist(size of the outer vector = n , vector<int>(size of EACH VECTOR in the vector = 10));
/*for(int k=0;k<4;k++)
use this for four adjacent ;
                {
                    int ni = i + int (k = 0) - int (k = 1);
                    int nj = j + int (k = 2) - int (k = 3);
                    if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;
                }
                */
#define pb push_back
#define int long long
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
#define MAXN   100001

// stores smallest prime factor for every number
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t = 1;
   cin>>t;
    for(int i1=0;i1<t;i1++)
    {

       int n,m;
       cin>>n>>m;
       int arr[n][m];
       int f = 1;
       int d = 0;
       if(i1 == 25){d=1;}
      if(d == 1){cout<<"YES\n";}
       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int cnt = 4;
            cin>>arr[i][j];
            if(d){cout<<arr[i][j]<<" ";}
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == n-1){cnt -= 1;}
            if(arr[i][j] > cnt){f=0;}
        }
        if(d){cout<<"\n";}
       }
       if(f==0){cout<<"NO\n";}
       else
       {
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
                int y = 0;
                int h = max(arr[i][j] - cnt,y);
                if(i!=0 && h)
                {
                    if(arr[i-1][j] == 0){h-=1;arr[i-1][j]=1;}
                }
                if(i!=n-1 && h)
                {
                    if(arr[i+1][j] == 0){h-=1;arr[i+1][j]=1;}
                }
                if(j!=m-1 && h)
                {
                    if(arr[i][j+1] == 0){h-=1;arr[i][j+1]=1;}
                }
                if(j!=0 && h)
                {
                    if(arr[i][j-1] == 0){h-=1;arr[i][j-1]=1;}
                }

                }
            }

                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(arr[i][j]!=0)
                        {int cnt = 0;
                if(i!=0)
                {
                    if(arr[i-1][j] != 0){cnt += 1;}
                }
                if(i!=n-1)
                {
                    if(arr[i+1][j] != 0){cnt += 1;}
                }
                if(j!=m-1)
                {
                    if(arr[i][j+1] != 0){cnt += 1;}
                }
                if(j!=0)
                {
                    if(arr[i][j-1] != 0){cnt += 1;}
                }
            arr[i][j] = cnt;}
                cout<<arr[i][j]<<" ";
                    }
                    cout<<"\n";
                }

        }

    }
    return 0;
}
/*
     ....But I always do.....
*/


/*
   And you wish I would'nt wait for you ....
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll , ll>
#define pci pair<char,int>
#define fi first
#define se second
int isqrt(int n) {
  return sqrtl(n);
}
//map<pii,int> m; m[mp(0,0)]=0; pii ans=mp(1,1e9); ans.x , ans.y
//template<class T>
//using min_heap = priority_queue<T , vector<T> , greater<T>>;
//template<class T>
//using max_heap = priority_queue<T>;
//sort(B.begin() , B.end() , [](const box &l , const box &r){return l.x1 < r.x1;});
//min_heap<pair<ll , size_t>> byx2;
//max_heap<pair<ll , size_t>> byy1;
//using vll = vector<ll>
//vi gone(n , 0);
//sortdesc(arr, arr+n, greater<int>());
// x | (1 << n) returns x with nth bit set
//  x & (-x) returns the rightmost 1 of x
//getline(cin , s);
// vector<vector<int>> dist(size of the outer vector = n , vector<int>(size of EACH VECTOR in the vector = 10));
/*for(int k=0;k<4;k++)
use this for four adjacent ;
                {
                    int ni = i + int (k = 0) - int (k = 1);
                    int nj = j + int (k = 2) - int (k = 3);
                    if(ni < 0 || nj < 0 || ni >= n || nj >= m )continue;
                }
                */
#define pb push_back
#define int long long
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
#define MAXN   100001

// stores smallest prime factor for every number
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t = 1;
   cin>>t;
    while(t--)
    {
       int n,m,a;
       cin>>n>>m;
       int f = 1;
       for(int i=0;i<n;i++)
       {
        for(int j=0;j<m;j++)
        {
            int cnt = 4;
            cin>>a;
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == m-1){cnt -= 1;}
            if(a > cnt){f=0;}
        }
       }
       if(f==0){cout<<"NO\n";}
       else
       {
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int cnt = 4;
            if(i == 0 || i == n-1){cnt -= 1;}
            if(j == 0 || j == m-1){cnt -= 1;}
                cout<<cnt<<" ";
            }
            cout<<"\n";
        }

        }

    }
    return 0;
}
/*
     ....But I always do.....
*/


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }int pp=0;
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
     int cc=0;

   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
}

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;kk++;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;kk++;}

       }
      if((j+1<m))
    {if(a[i][j+1]!=0){ if(cc==kk)break; a[i][j+1]=1;kk++;}
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0){ if(cc==kk)break; a[i][j-1]=1;kk++;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int c=0;
      if(a[i][j]==0)continue;
      int k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
  if(pp==1)break;
}if(pp==1)continue;

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;kk++;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;kk++;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)break; a[i][j+1]=1;kk++;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)break; a[i][j-1]=1;kk++;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


 #include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
  if(pp==1)break;
}if(pp==1)continue;

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;

    kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }

   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)continue; a[i+1][j]=1;kk++;if(cc==kk)continue;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)continue;a[i-1][j]=1;kk++;if(cc==kk)continue;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)continue; a[i][j+1]=1;kk++;if(cc==kk)continue;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)continue; a[i][j-1]=1;kk++;if(cc==kk)continue;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }int tt=0;
           if(a[i][j]>c)
           {tt=1;
           // cout<<"NO"<<endl;pp=1;break;
           ///

               if((i+1<n))
   { if(a[i+1][j]==0){c++;a[i+1][j]=1;if(a[i][j]==c)continue;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){c++;a[i-1][j]=1;if(a[i][j]==c)continue;}
       }
      if((j+1<m))
    {if(a[i][j+1]==0){c++;a[i][j+1]=1;if(a[i][j]==c)continue;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){c++;a[i][j-1]=1;if(a[i][j]==c)continue;}
         }
           ///

           }
           if(tt==1)
           {
                 if(a[i][j]!=c)
                 {
                       cout<<"NO"<<endl;pp=1;break;
                 }
           }

        else a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


 #include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
  if(pp==1)break;
}if(pp==1)continue;

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;

    kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }

   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)continue; a[i+1][j]=1;kk++;if(cc==kk)continue;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)continue;a[i-1][j]=1;kk++;if(cc==kk)continue;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)continue; a[i][j+1]=1;kk++;if(cc==kk)continue;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)continue; a[i][j-1]=1;kk++;if(cc==kk)continue;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


 #include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
  if(pp==1)break;
}if(pp==1)continue;

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;

    kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)continue; a[i+1][j]=1;kk++;if(cc==kk)continue;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)continue;a[i-1][j]=1;kk++;if(cc==kk)continue;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)continue; a[i][j+1]=1;kk++;if(cc==kk)continue;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)continue; a[i][j-1]=1;kk++;if(cc==kk)continue;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
  if(pp==1)break;
}if(pp==1)continue;

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;
     if(pp==1)break;
    kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)continue; a[i+1][j]=1;kk++;if(cc==kk)continue;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)continue;continue;a[i-1][j]=1;kk++;if(cc==kk)continue;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)continue; a[i][j+1]=1;kk++;if(cc==kk)continue;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)continue; a[i][j-1]=1;kk++;if(cc==kk)continue;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


 #include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
  if(pp==1)break;
}if(pp==1)continue;

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;

    kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }

   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)continue; a[i+1][j]=1;kk++;if(cc==kk)continue;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)continue;a[i-1][j]=1;kk++;if(cc==kk)continue;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)continue; a[i][j+1]=1;kk++;if(cc==kk)continue;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)continue; a[i][j-1]=1;kk++;if(cc==kk)continue;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }

         a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
}

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;kk++;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;kk++;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)break; a[i][j+1]=1;kk++;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)break; a[i][j-1]=1;kk++;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }int pp=0;
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
     int cc=0;

   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
}

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;kk++;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;kk++;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)break; a[i][j+1]=1;kk++;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)break; a[i][j-1]=1;kk++;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int c=0;

      int k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }int pp=0;
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
     int cc=0;

   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<n))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
}

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<n))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;}

       }
      if((j+1<n))
    {if(a[i][j+1]!=0){ if(cc==kk)break; a[i][j+1]=1;}
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0){ if(cc==kk)break; a[i][j-1]=1;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<n;j++)
    {
      int c=0;
      if(a[i][j]==0)continue;
      int k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<n))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;int pp=0;int kk;int cc;int c,k;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
      cc=0;

   kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
  if(pp==1)break;
}if(pp==1)continue;

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       cc=0;
     if(pp==1)break;
    kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;kk++;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;kk++;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)break; a[i][j+1]=1;kk++;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)break; a[i][j-1]=1;kk++;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
       c=0;

       k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }if(pp==1)break;
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }int pp=0;
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
     int cc=0;

   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
}

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;kk++;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;kk++;}

       }
      if((j+1<m))
    {if(a[i][j+1]==0){ if(cc==kk)break; a[i][j+1]=1;kk++;}
        }
        if((j-1>=0))
    {if(a[i][j-1]==0){ if(cc==kk)break; a[i][j-1]=1;kk++;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int c=0;
      if(a[i][j]==0)continue;
      int k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }
           if(a[i][j]>c)
           {
            cout<<"NO"<<endl;pp=1;break;
           }
         a[i][j]=c;
    }
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }int pp=0;
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
     int cc=0;

   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
}

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;kk++;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;kk++;}

       }
      if((j+1<m))
    {if(a[i][j+1]!=0){ if(cc==kk)break; a[i][j+1]=1;kk++;}
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0){ if(cc==kk)break; a[i][j-1]=1;kk++;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int c=0;
      if(a[i][j]==0)continue;
      int k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }

         a[i][j]=c;
    }
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fi first
#define se second
#define pb push_back
#define so(v) sort(v.begin(),v.end())
#define lb(v,x) lower_bound(v.begin(),v.end())
#define ub(v,x) upper_bound(v.begin(),v.end())
#define endl '\n'
#define find(v,x) find(v.begin(),v.end(),x)
#define dbv(v) cout<<endl;cout<<#v<<"-->> ";for(auto it:v){cout<<it<<" ";}cout<<endl;
#define dbm(m)cout<<endl;cout<<#m<<"-->> ";for(auto it:m){cout<<it.fi<<" "<<it.se<<endl;}
#define dbs(s)cout<<endl;cout<<#s<<"-->> ";for(auto it:s){cout<<it<<" ";}cout<<endl;
#define mod 1000000007
#define db1(x) cout<<#x<<"="<<x<<endl;
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl;
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<endl;
#define mx 1e18
#define io std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
int32_t main()
{
  io
int t;cin>>t;
while(t--)
{
  int n,m;cin>>n>>m;
  int a[n][m];
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)
   {
    cin>>a[i][j];
  }int pp=0;
for(int i=0;i<n;i++)
{
  for(int j=0;j<m;j++)
  {
     int cc=0;

   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
  }
}

  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
     if(pp==1)break;
   int kk=a[i][j];
    if((i+1<n))
   { cc++;
        }
     if((i-1>=0))
  { cc++;
       }
      if((j+1<m))
    {cc++;
        }
        if((j-1>=0))
    {cc++;
         }
    if(cc<a[i][j])
    {
      cout<<"NO"<<endl;
      pp=1;break;
    }
   //
 if((i+1<n))
   { if(a[i+1][j]==0){ if(cc==kk)break; a[i+1][j]=1;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)break; a[i-1][j]=1;}

       }
      if((j+1<m))
    {if(a[i][j+1]!=0){ if(cc==kk)break; a[i][j+1]=1;}
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0){ if(cc==kk)break; a[i][j-1]=1;}
         }

   //

    }
    if(pp==1)break;
  }
  if(pp==1)continue;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int c=0;
      if(a[i][j]==0)continue;
      int k=a[i][j];
    if((i+1<n))
   { if(a[i+1][j]!=0)c++;
        }
     if((i-1>=0))
  { if(a[i-1][j]!=0)c++;
       }
      if((j+1<m))
    {if(a[i][j+1]!=0)c++;
        }
        if((j-1>=0))
    {if(a[i][j-1]!=0)c++;
         }

         a[i][j]=c;
    }
  }
  if(pp==1)continue;
  cout<<"YES"<<endl;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      cout<<a[i][j]<<" ";
    }
    cout<<endl;
  }

}

}