#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)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;
   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];int kk=0;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
      cin>>a[i][j];
      if(i-1>=0)cc++;
      if(i+1<n)cc++;
      if(j-1>=0)cc++;
      if(j+1<m)cc++;
      if(a[i][j]>cc)kk=1;
    }
  }
  if(kk==1)cout<<"NO"<<endl;
  else
  {
    cout<<"YES"<<endl;
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
      {
        int c=0;
         if(i-1>=0){c++;}
      if(i+1<n){c++;}
      if(j-1>=0){c++;}
      if(j+1<m){c++;}
      cout<<c<<" ";

      }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++;continue;}
        }
     if((i-1>=0))
  { if(a[i-1][j]==0){ if(cc==kk)continue;continue;a[i-1][j]=1;kk++;continue;}

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

  ///
   /*

  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;
        }
  }
  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];int kk=0;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
      int cc=0;
      cin>>a[i][j];
      if(i-1>=0)cc++;
      if(i+1<n)cc++;
      if(j-1>=0)cc++;
      if(j+1<n)cc++;
      if(a[i][j]>cc)kk=1;
    }
  }
  if(kk==1)cout<<"NO"<<endl;
  else
  {
    cout<<"YES"<<endl;
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
      {
        int c=0;
         if(i-1>=0){c++;}
      if(i+1<n){c++;}
      if(j-1>=0){c++;}
      if(j+1<n){c++;}
      cout<<c<<" ";

      }cout<<endl;
    }
  }

}

}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this(for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>max(r,c) or a[i][j]>4)
            flag=0;
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else a[i][j]=3;
            if(i==0 and j==0)
            a[i][j]=2;
            else if(i==0 and j==c-1)
            a[i][j]=2;
            else if(i==r-1 and j==0)
            a[i][j]=2;
            else if(i==r-1 and j==c-1)
            a[i][j]=2;
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this (for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>4)
            {
                flag=0;
                break;
            }
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else if(i==0 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==0 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else
            {
                if(a[i][j]>3)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=3;
            }
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this(for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>max(r,c) or a[i][j]>4)
            flag=0;
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else if(i==0 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==0 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else
            {
                if(a[i][j]>3)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=3;
            }
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this(for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>r or a[i][j]>c or a[i][j]>4)
            flag=0;
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else a[i][j]=3;
            if(i==0 and j==0)
            a[i][j]=2;
            else if(i==0 and j==c-1)
            a[i][j]=2;
            else if(i==r-1 and j==0)
            a[i][j]=2;
            else if(i==r-1 and j==c-1)
            a[i][j]=2;
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this(for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>r or a[i][j]>c or a[i][j]>4)
            flag=0;
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else a[i][j]=3;
            if(i==0 and j==0)
            a[i][j]=2;
            else if(i==0 and j==c-1)
            a[i][j]=2;
            else if(i==r-1 and j==0)
            a[i][j]=2;
            else if(i==r-1 and j==c-1)
            a[i][j]=2;
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j];
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this(for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>max(r,c) or a[i][j]>4)
            {
                flag=0;
                break;
            }
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else if(i==0 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==0 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else
            {
                if(a[i][j]>3)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=3;
            }
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this(for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>4)
            {
                flag=0;
                break;
            }
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else if(i==0 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==0 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==0)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else if(i==r-1 and j==c-1)
            {
                if(a[i][j]>2)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=2;
            }
            else
            {
                if(a[i][j]>3)
                {
                    flag=0;
                    continue;
                }
                a[i][j]=3;
            }
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/*
one code/msg for my crush :-p
bool imblock=false;
while(imblock)
{
    if(ReplyWithinOneDay)
    GiveHerNumToBackbenchers=false;
    else //  {
    GiveHerNumToBackbenchers=true;
    imblock=true;//;-)
    }
}

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

TIPS for good rating:-
    if(timeinsecondQues<=20mins)
    {
        submititandsolvefirstQues=true;
        givecontest=true;
    }
    else givecontest=false;

*Don't Quote me on this(for newbies only :p).
*/

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define P push
#define vvi vector<vector<int> >
#define vi vector<int>
#define sz(a) (int)a.size()
#define for(i,n) for(int i=0;i<n;i++)
// #define for(j,n) for(int j=0;j<n;j++)
const int N = 1e9 + 7 , N1 = 1e7 + 5 ;

void solve()
{
    int r,c,flag=1;
    cin>>r>>c;
    int a[r][c];
    for(i,r){
        for(j,c)cin>>a[i][j];
    }
    for(i,r)
    {
        for(j,c)
        {
            if(a[i][j]>max(r,c) or a[i][j]>4)
            flag=0;
            if(i!=0 and i!=r-1 and j!=0 and j!=c-1)
            a[i][j]=4;
            else a[i][j]=3;
            if(i==0 and j==0)
            a[i][j]=2;
            else if(i==0 and j==c-1)
            a[i][j]=2;
            else if(i==r-1 and j==0)
            a[i][j]=2;
            else if(i==r-1 and j==c-1)
            a[i][j]=2;
        }
    }
    if(flag==1)
    {
        cout<<"YES\n";
        for(i,r)
        {
            for(j,c)cout<<a[i][j];
            cout<<endl;
        }
    }
    else cout<<"NO\n";
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t1=1;
    cin>>t1;
    //cout<<fixed<<setprecision(6);
    while(t1--){
        solve();
    }
}


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int arr[n][m];
        int p=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>arr[i][j];
                if(arr[i][j]>4)
                    p=1;
            }
        }
        int r=0,s=0;
        for(int i=1;i<n-1;i++)
        {
            if(arr[i][0]==4||arr[i][m-1]==4)
            {
                r=1;
                break;
            }
        }
        for(int i=1;i<m-1;i++)
        {
            if(arr[0][i]==4||arr[n-1][i]==4)
            {
                s=1;
                break;
            }
        }
        if(p==1)
            cout<<"NO"<<endl;
        else if(arr[0][0]==3||arr[0][m-1]==3||arr[n-1][0]==3||arr[n-1][m-1]==3)
        {
            cout<<"NO"<<endl;
        }
        else if(r==1||s==1)
            cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            arr[0][0] = 2;
            arr[0][m-1] = 2;
            arr[n-1][0] =2;
            arr[n-1][m-1] = 2;
            for(int i=1;i<n-1;i++)
            {
                arr[i][0]=3;
                arr[i][m-1]=3;
            }
            for(int i=1;i<m-1;i++)
            {
                arr[0][i]=3;
                arr[n-1][i]=3;
            }
            for(int i=1;i<n-1;i++)
            {
                for(int j=1;j<m-1;j++)
                {
                    arr[i][j]=4;
                }
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    cout<<arr[i][j]<<" ";
                cout<<endl;
            }
            cout<<endl;
        }
    }
    return 0;
}


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int arr[n][m];
        int p=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>arr[i][j];
                if(arr[i][j]>4)
                    p=1;
            }
        }
        int r=0,s=0;
        for(int i=1;i<n-1;i++)
        {
            if(arr[i][0]==4||arr[i][m-1]==4)
            {
                r=1;
                break;
            }
        }
        for(int i=1;i<m-1;i++)
        {
            if(arr[0][i]==4||arr[n-1][i]==4)
            {
                s=1;
                break;
            }
        }
        if(p==1)
            cout<<"NO"<<endl;
        else if(arr[0][0]==3||arr[0][m-1]==3||arr[n-1][0]==3||arr[n-1][m-1]==3)
        {
            cout<<"NO"<<endl;
        }
        else if(r==1||s==1)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }
    return 0;
}


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
    int r,c; cin >> r >>c;
    int a[r][c];
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++) cin >> a[i][j];
    }
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++) {
            if (i==0||i==r-1){
                if (j==0 || j==c-1){
                    if (a[i][j]>2){ cout << "NO\n";return 0;}
                    else a[i][j]=2;
                }else{
                    if (a[i][j] >3){ cout << "NO\n";return 0;}
                    else a[i][j]=3;
                }
            }else{
                if (j==0 || j==c-1){
                    if (a[i][j]>3){ cout << "NO\n";return 0;}
                    else a[i][j]=3;
                }else{
                    if (a[i][j] >4){ cout << "NO\n";return 0;}
                    else a[i][j]=4;
                }
            }
        }
    }
    cout << "YES\n";
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++) cout << a[i][j] << " ";
        cout << "\n";
    }
    }
    return 0;
}


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int arr[n][m];
        int p=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>arr[i][j];
                if(arr[i][j]>4)
                    p=1;
            }
        }
        int r=0,s=0;
        for(int i=1;i<n-1;i++)
        {
            if(arr[i][0]==4||arr[i][m-1]==4)
            {
                r=1;
                break;
            }
            else
            {
                arr[i][0]=3;
                arr[i][m-1]=3;
            }
        }
        for(int i=1;i<m-1;i++)
        {
            if(arr[0][i]==4||arr[n-1][i]==4)
            {
                s=1;
                break;
            }
            else
            {
                arr[0][i] = 3;
                arr[n-1][i] = 3;
            }
        }
        if(p==1)
            cout<<"NO"<<endl;
        else if(arr[0][0]==3||arr[0][m-1]==3||arr[n-1][0]==3||arr[n-1][m-1]==3)
        {
            cout<<"NO"<<endl;
        }
        else if(r==1||s==1)
            cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            arr[0][0] = 2;
            arr[0][m-1] = 2;
            arr[n-1][0] =2;
            arr[n-1][m-1] = 2;
            for(int i=1;i<n-1;i++)
            {
                for(int j=1;j<m-1;j++)
                {
                    arr[i][j]=4;
                }
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    cout<<arr[i][j]<<" ";
                cout<<endl;
            }
        }
    }
    return 0;
}


    /* ****Yash_mittal**** */

    #include<bits/stdc++.h>
    #define ll          long long
    #define pb          push_back
    #define ppb         pop_back
    #define endl        '\n'
    #define pii         pair<ll int,ll int>
    #define vi          vector<ll int>
    #define all(a)      (a).begin(),(a).end()
    #define F           first
    #define S           second
    #define sz(x)       (ll int)x.size()
    #define hell        1000000007
    #define rep(i,a,b)  for(ll int i=a;i<b;i++)
    #define lbnd        lower_bound
    #define ubnd        upper_bound
    #define bs          binary_search
    #define mp          make_pair
    using namespace std;

    #define N  100005;
    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        int t;
        cin>>t;
        while(t--)
        {
            int n,m;
            cin>>n>>m;
            int arr[n][m];
            int p=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    cin>>arr[i][j];
                    if(arr[i][j]>4)
                        p=1;
                }
            }
            int r=0,s=0;
            for(int i=1;i<n-1;i++)
            {
                if(arr[i][0]==4||arr[i][m-1]==4)
                {
                    r=1;
                    break;
                }
            }
            for(int i=1;i<m-1;i++)
            {
                if(arr[0][i]==4||arr[n-1][i]==4)
                {
                    s=1;
                    break;
                }
            }
            if(p==1)
                cout<<"NO"<<endl;
            else if(arr[0][0]>=3||arr[0][m-1]>=3||arr[n-1][0]>=3||arr[n-1][m-1]>=3)
            {
                cout<<"NO"<<endl;
            }
            else if(r==1||s==1)
                cout<<"NO"<<endl;
            else
            {
                cout<<"YES"<<endl;
                arr[0][0] = 2;
                arr[0][m-1] = 2;
                arr[n-1][0] =2;
                arr[n-1][m-1] = 2;
                for(int i=1;i<n-1;i++)
                {
                    arr[i][0]=3;
                    arr[i][m-1]=3;
                }
                for(int i=1;i<m-1;i++)
                {
                    arr[0][i]=3;
                    arr[n-1][i]=3;
                }
                for(int i=1;i<n-1;i++)
                {
                    for(int j=1;j<m-1;j++)
                    {
                        arr[i][j]=4;
                    }
                }
                for(int i=0;i<n;i++)
                {
                    for(int j=0;j<m;j++)
                        cout<<arr[i][j]<<" ";
                    cout<<endl;
                }
            }
        }
        return 0;
    }


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
void solve()
{
    int r,c; cin >> r >>c;
    int a[r][c];
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++) cin >> a[i][j];
    }
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++) {
            if (i==0||i==r-1){
                if (j==0 || j==c-1){
                    if (a[i][j]>2){ cout << "NO\n";return;}
                    else a[i][j]=2;
                }else{
                    if (a[i][j] >3){ cout << "NO\n";return;}
                    else a[i][j]=3;
                }
            }else{
                if (j==0 || j==c-1){
                    if (a[i][j]>3){ cout << "NO\n";return;}
                    else a[i][j]=3;
                }else{
                    if (a[i][j] >4){ cout << "NO\n";return;}
                    else a[i][j]=4;
                }
            }
        }
    }
    cout << "YES\n";
    for (int i=0;i<r;i++){
        for (int j=0;j<c;j++) cout << a[i][j] << " ";
        cout << "\n";
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int arr[n][m];
        int p=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>arr[i][j];
                if(arr[i][j]>4)
                    p=1;
            }
        }
        int r=0,s=0;
        for(int i=1;i<n-1;i++)
        {
            if(arr[i][0]==4||arr[i][m-1]==4)
            {
                r=1;
                break;
            }
        }
        for(int i=1;i<m-1;i++)
        {
            if(arr[0][i]==4||arr[n-1][i]==4)
            {
                s=1;
                break;
            }
        }
        if(p==1)
            cout<<"NO"<<endl;
        else if(arr[0][0]==3||arr[0][m-1]==3||arr[n-1][0]==3||arr[n-1][m-1]==3)
        {
            cout<<"NO"<<endl;
        }
        else if(r==1||s==1)
            cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            arr[0][0] = 2;
            arr[0][m-1] = 2;
            arr[n-1][0] =2;
            arr[n-1][m-1] = 2;
            for(int i=1;i<n-1;i++)
            {
                arr[i][0]=3;
                arr[i][m-1]=3;
            }
            for(int i=1;i<m-1;i++)
            {
                arr[0][i]=3;
                arr[n-1][i]=3;
            }
            for(int i=1;i<n-1;i++)
            {
                for(int j=1;j<m-1;j++)
                {
                    arr[i][j]=4;
                }
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    cout<<arr[i][j]<<" ";
                cout<<endl;
            }
        }
    }
    return 0;
}


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int arr[n][m];
        int p=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>arr[i][j];
                if(arr[i][j]>4)
                    p=1;
            }
        }
        int r=0,s=0;
        for(int i=1;i<n-1;i++)
        {
            if(arr[i][0]==4||arr[i][m-1]==4)
            {
                r=1;
                break;
            }
            else
            {
                arr[i][0]=3;
                arr[i][m-1]=3;
            }
        }
        for(int i=1;i<m-1;i++)
        {
            if(arr[0][i]==4||arr[n-1][i]==4)
            {
                s=1;
                break;
            }
            else
            {
                arr[0][i] = 3;
                arr[n-1][i] = 3;
            }
        }
        if(p==1)
            cout<<"NO"<<endl;
        else if(arr[0][0]==3||arr[0][m-1]==3||arr[n-1][0]==3||arr[n-1][m-1]==3)
        {
            cout<<"NO"<<endl;
        }
        else if(r==1&&s==1)
            cout<<"NO"<<endl;
        else
        {
            cout<<"YES"<<endl;
            arr[0][0] = 2;
            arr[0][m-1] = 2;
            arr[n-1][0] =2;
            arr[n-1][m-1] = 2;
            for(int i=1;i<n-1;i++)
            {
                for(int j=1;j<m-1;j++)
                {
                    arr[i][j]=4;
                }
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    cout<<arr[i][j]<<" ";
                cout<<endl;
            }
        }
    }
    return 0;
}


/* ****Yash_mittal**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int arr[n][m];
        int p=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>arr[i][j];
                if(arr[i][j]>4)
                    p=1;
            }
        }
        //int r=0,s=0;
        for(int i=1;i<n-1;i++)
        {
            if(arr[i][0]==4||arr[i][m-1]==4)
            {
                cout<<"NO"<<endl;
                break;
            }
            else
            {
                arr[i][0]=3;
                arr[i][m-1]=3;
            }
        }
        for(int i=1;i<m-1;i++)
        {
            if(arr[0][i]==4||arr[n-1][i]==4)
            {
                cout<<"NO"<<endl;
                break;
            }
            else
            {
                arr[0][i] = 3;
                arr[n-1][i] = 3;
            }
        }
        if(p==1)
            cout<<"NO"<<endl;
        else if(arr[0][0]==3||arr[0][m-1]==3||arr[n-1][0]==3||arr[n-1][m-1]==3)
        {
            cout<<"NO"<<endl;
        }
        else
        {
            cout<<"YES"<<endl;
            arr[0][0] = 2;
            arr[0][m-1] = 2;
            arr[n-1][0] =2;
            arr[n-1][m-1] = 2;
            for(int i=1;i<n-1;i++)
            {
                for(int j=1;j<m-1;j++)
                {
                    arr[i][j]=4;
                }
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    cout<<arr[i][j]<<" ";
                cout<<endl;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>

#define ELEMENT_2DIM(_array, _row, _col, _cols) *(_array + (_row) * (_cols) + (_col))

#define SIB_NONE -1
#define SIB_LEFT 0
#define SIB_TOP 1
#define SIB_RIGHT 2
#define SIB_BOTTOM 3

using namespace std;

enum position
{
    LEFT_TOP,
    TOP,
    RIGHT_TOP,
    RIGHT,
    RIGHT_BOTTOM,
    BOTTOM,
    LEFT_BOTTOM,
    LEFT,
    CENTER
};

void input(long int *arr, long int rows, long int columns);
void process(long int rows, long int columns);
void output(long int *arr, long int rows, long int columns);
position get_position(long int rows, long int columns, long int row, long int column);
int validate_position(long int rows, long int columns, long int row, long int column, long int value);
int validate(long int *arr, long int rows, long int columns);
int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column);
void adjust_all(long int *arr, long int rows, long int columns);

int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        long int lines, columns;
        cin >> lines >> columns;
        process(lines, columns);
    }
}

void process(long int rows, long int columns)
{
    long int array[rows][columns];
    input(&array[0][0], rows, columns);
    if (!validate(&array[0][0], rows, columns))
    {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
    adjust_all(&array[0][0], rows, columns);
    output(&array[0][0], rows, columns);
}
void input(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cin >> ELEMENT_2DIM(arr, i, j, columns);
        }
    }
}
void output(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cout << ELEMENT_2DIM(arr, i, j, columns);
            if (j == columns - 1)
            {
                cout << endl;
            }
            else
            {
                cout << " ";
            }
        }
    }
}

position get_position(long int rows, long int columns, long int row, long int column)
{
    if (row == 0)
    {
        if (column == 0)
        {
            return LEFT_TOP;
        }
        else if (column == columns - 1)
        {
            return RIGHT_TOP;
        }
        return TOP;
    }
    if (column == 0)
    {
        if (row == rows - 1)
        {
            return LEFT_BOTTOM;
        }
        return LEFT;
    }
    if (row == rows - 1)
    {
        if (column == columns - 1)
        {
            return RIGHT_BOTTOM;
        }
        return BOTTOM;
    }
    if (column == columns - 1)
    {
        return RIGHT;
    }
    return CENTER;
}

int validate_position(long int rows, long int columns, long int row, long int column, long int value)
{
    if (value < 0)
    {
        return 0;
    }
    position pos = get_position(rows, columns, row, column);
    switch (pos)
    {
    case LEFT_TOP:
    case LEFT_BOTTOM:
    case RIGHT_TOP:
    case RIGHT_BOTTOM:
        return value < 3;
    case LEFT:
    case RIGHT:
    case TOP:
    case BOTTOM:
        return value < 4;
    default:
        return value < 5;
    }
}

int validate(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            if (!validate_position(rows, columns, i, j, ELEMENT_2DIM(arr, i, j, columns)))
            {
                return 0;
            }
        }
    }
    return 1;
}

void adjust_all(long int *arr, long int rows, long int columns)
{
    long int i = 0;
    while (i < rows)
    {
        long int j = 0;
        while (j < columns)
        {
            int sib = adjust_position(arr, rows, columns, i, j);
            if (sib != SIB_NONE) {
                i = j = 0;
                continue;
            }
            j++;
        }
        i++;
    }
}

int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column)
{
    int value = ELEMENT_2DIM(arr, row, column, columns);
    if (value != 0)
    {
        long int siblings[4] = {-1, -1, -1, -1};
        position pos = get_position(rows, columns, row, column);
        switch (pos)
        {
        case LEFT_TOP:
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            break;
        case BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT_BOTTOM:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;

        case CENTER:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        }
        long int siblings_count = (siblings[SIB_LEFT] > 0) +
                                  (siblings[SIB_TOP] > 0) +
                                  (siblings[SIB_RIGHT] > 0) +
                                  (siblings[SIB_BOTTOM] > 0);
        if (value <= siblings_count)
        {
            ELEMENT_2DIM(arr, row, column, columns) = siblings_count;
            return SIB_NONE;
        }
        for (int i = 0; i < 4; i++)
        {
            if (siblings[i] == 0)
            {
                switch (i)
                {
                case SIB_LEFT:
                    ELEMENT_2DIM(arr, row, column - 1, columns) = 1;
                    break;
                case SIB_TOP:
                    ELEMENT_2DIM(arr, row - 1, column, columns) = 1;
                    break;
                case SIB_RIGHT:
                    ELEMENT_2DIM(arr, row, column + 1, columns) = 1;
                    break;
                case SIB_BOTTOM:
                    ELEMENT_2DIM(arr, row + 1, column, columns) = 1;
                    break;
                }
                return i;
            }
        }
    }
    return SIB_NONE;
}


#include <iostream>
using namespace std;
void matrica(int lines, int columns)
{
    int a[lines][columns];
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cin >> a[i][j];
        }
    }
    if (a[0][0] >= 3 || a[0][columns - 1] >= 3 || a[lines - 1][0] >= 3 || a[lines - 1][columns - 1] >= 3)
    {
        cout << "NO" << endl;
        return;
    }
    else
    {
        a[0][0] = a[0][columns - 1] = a[lines - 1][0] = a[lines - 1][columns - 1] = 2;
    }
    for (int j = 1; j < lines - 1; j++)
    {
        if (a[j][0] >= 4 || a[j][columns - 1] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[j][0] = a[j][columns - 1] = 3;
        }
    }
    for (int k = 1; k < columns - 1; k++)
    {
        if (a[0][k] >= 4 || a[lines - 1][k] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[0][k] = a[lines - 1][k] = 3;
        }
    }
    for (int i = 1; i < lines - 1; i++) {
        for (int j = 1; j < columns - 1; j++) {
            if (a[i][j] >= 5) {
                cout << "NO" << endl;
            }
            else {
                a[i][j] = 4;
            }
        }
    }
    cout << "YES" << endl;
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}
int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        int n, m;
        cin >> n >> m;
        matrica(n, m);
    }
}


#include <iostream>
using namespace std;
void matrica( int lines, int columns)
{
    long int a[lines][columns];
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cin >> a[i][j];
        }
    }
    if (a[0][0] >= 3 || a[0][columns - 1] >= 3 || a[lines - 1][0] >= 3 || a[lines - 1][columns - 1] >= 3)
    {
        cout << "NO" << endl;
        return;
    }
    else
    {
        a[0][0] = a[0][columns - 1] = a[lines - 1][0] = a[lines - 1][columns - 1] = 2;
    }
    for (int j = 1; j < lines - 1; j++)
    {
        if (a[j][0] >= 4 || a[j][columns - 1] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[j][0] = a[j][columns - 1] = 3;
        }
    }
    for (int k = 1; k < columns - 1; k++)
    {
        if (a[0][k] >= 4 || a[lines - 1][k] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[0][k] = a[lines - 1][k] = 3;
        }
    }
    for (int i = 1; i < lines - 1; i++)
    {
        for (int j = 1; j < columns - 1; j++)
        {
            if (a[i][j] >= 5)
            {
                cout << "NO" << endl;
            }
            else
            {
                a[i][j] = 4;
            }
        }
    }
    cout << "YES" << endl;
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}
int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        int n, m;
        cin >> n >> m;
        matrica(n, m);
    }
}


#include <iostream>
using namespace std;
void matrica(long int lines,long int columns)
{
    int a[lines][columns];
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cin >> a[i][j];
        }
    }
    if (a[0][0] >= 3 || a[0][columns - 1] >= 3 || a[lines - 1][0] >= 3 || a[lines - 1][columns - 1] >= 3)
    {
        cout << "NO" << endl;
        return;
    }
    else
    {
        a[0][0] = a[0][columns - 1] = a[lines - 1][0] = a[lines - 1][columns - 1] = 2;
    }
    for (int j = 1; j < lines - 1; j++)
    {
        if (a[j][0] >= 4 || a[j][columns - 1] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[j][0] = a[j][columns - 1] = 3;
        }
    }
    for (int k = 1; k < columns - 1; k++)
    {
        if (a[0][k] >= 4 || a[lines - 1][k] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[0][k] = a[lines - 1][k] = 3;
        }
    }
    for (int i = 1; i < lines - 1; i++)
    {
        for (int j = 1; j < columns - 1; j++)
        {
            if (a[i][j] >= 5)
            {
                cout << "NO" << endl;
            }
            else
            {
                a[i][j] = 4;
            }
        }
    }
    cout << "YES" << endl;
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}
int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        int n, m;
        cin >> n >> m;
        matrica(n, m);
    }
}


#include <iostream>
using namespace std;
void matrica( long int lines,long  int columns)
{
    long int a[lines][columns];
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cin >> a[i][j];
        }
    }
    if (a[0][0] >= 3 || a[0][columns - 1] >= 3 || a[lines - 1][0] >= 3 || a[lines - 1][columns - 1] >= 3)
    {
        cout << "NO" << endl;
        return;
    }
    else
    {
        a[0][0] = a[0][columns - 1] = a[lines - 1][0] = a[lines - 1][columns - 1] = 2;
    }
    for (int j = 1; j < lines - 1; j++)
    {
        if (a[j][0] >= 4 || a[j][columns - 1] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[j][0] = a[j][columns - 1] = 3;
        }
    }
    for (int k = 1; k < columns - 1; k++)
    {
        if (a[0][k] >= 4 || a[lines - 1][k] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[0][k] = a[lines - 1][k] = 3;
        }
    }
    for (int i = 1; i < lines - 1; i++)
    {
        for (int j = 1; j < columns - 1; j++)
        {
            if (a[i][j] >= 5)
            {
                cout << "NO" << endl;
            }
            else
            {
                a[i][j] = 4;
            }
        }
    }
    cout << "YES" << endl;
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}
int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        int n, m;
        cin >> n >> m;
        matrica(n, m);
    }
}


#include <iostream>
#include <algorithm>

#define ELEMENT_2DIM(_array, _row, _col, _cols) *(_array + (_row) * (_cols) + (_col))

#define SIB_NONE -1
#define SIB_LEFT 0
#define SIB_TOP 1
#define SIB_RIGHT 2
#define SIB_BOTTOM 3

using namespace std;

enum position
{
    LEFT_TOP,
    TOP,
    RIGHT_TOP,
    RIGHT,
    RIGHT_BOTTOM,
    BOTTOM,
    LEFT_BOTTOM,
    LEFT,
    CENTER
};

void input(long int *arr, long int rows, long int columns);
void process(long int rows, long int columns);
void output(long int *arr, long int rows, long int columns);
position get_position(long int rows, long int columns, long int row, long int column);
int validate_position(long int rows, long int columns, long int row, long int column, long int value);
int validate(long int *arr, long int rows, long int columns);
int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column);
void adjust_all(long int *arr, long int rows, long int columns);

int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        long int lines, columns;
        cin >> lines >> columns;
        process(lines, columns);
    }
}

void process(long int rows, long int columns)
{
    long int array[rows][columns];
    input(&array[0][0], rows, columns);
    if (!validate(&array[0][0], rows, columns))
    {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
    adjust_all(&array[0][0], rows, columns);
    output(&array[0][0], rows, columns);
}
void input(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cin >> ELEMENT_2DIM(arr, i, j, columns);
        }
    }
}
void output(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cout << ELEMENT_2DIM(arr, i, j, columns) << " ";
        }
        cout << endl;
    }
}

position get_position(long int rows, long int columns, long int row, long int column)
{
    if (row == 0)
    {
        if (column == 0)
        {
            return LEFT_TOP;
        }
        else if (column == columns - 1)
        {
            return RIGHT_TOP;
        }
        return TOP;
    }
    if (column == 0)
    {
        if (row == rows - 1)
        {
            return LEFT_BOTTOM;
        }
        return LEFT;
    }
    if (row == rows - 1)
    {
        if (column == columns - 1)
        {
            return RIGHT_BOTTOM;
        }
        return RIGHT;
    }
    if (column == columns - 1)
    {
        return BOTTOM;
    }
    return CENTER;
}

int validate_position(long int rows, long int columns, long int row, long int column, long int value)
{
    if (value < 0)
    {
        return 0;
    }
    position pos = get_position(rows, columns, row, column);
    switch (pos)
    {
    case LEFT_TOP:
    case LEFT_BOTTOM:
    case RIGHT_TOP:
    case RIGHT_BOTTOM:
        return value < 3;
    case LEFT:
    case RIGHT:
    case TOP:
    case BOTTOM:
        return value < 4;
    default:
        return value < 5;
    }
}

int validate(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            if (!validate_position(rows, columns, i, j, ELEMENT_2DIM(arr, i, j, columns)))
            {
                return 0;
            }
        }
    }
    return 1;
}

void adjust_all(long int *arr, long int rows, long int columns)
{
    long int i = 0;
    while (i < rows)
    {
        long int j = 0;
        while (j < columns)
        {
            int sib = adjust_position(arr, rows, columns, i, j);
            switch (sib)
            {
            case SIB_LEFT:
                i = max(i - 1, 0L);
                j = max(j - 2, 0L);
                continue;
            case SIB_TOP:
                i = max(i - 2, 0L);
                j = max(j - 1, 0L);
                continue;
            case SIB_RIGHT:
                i = max(i - 1, 0L);
                j++;
                continue;
            }
            j++;
        }
        i++;
    }
}

int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column)
{
    int value = ELEMENT_2DIM(arr, row, column, columns);
    if (value != 0)
    {
        long int siblings[4] = {-1, -1, -1, -1};
        position pos = get_position(rows, columns, row, column);
        switch (pos)
        {
        case LEFT_TOP:
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            break;
        case BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT_BOTTOM:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;

        case CENTER:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        }
        long int siblings_count = (siblings[SIB_LEFT] > 0) +
                                  (siblings[SIB_TOP] > 0) +
                                  (siblings[SIB_RIGHT] > 0) +
                                  (siblings[SIB_BOTTOM] > 0);
        if (value <= siblings_count)
        {
            ELEMENT_2DIM(arr, row, column, columns) = siblings_count;
            return SIB_NONE;
        }
        for (int i = 0; i < 4; i++)
        {
            if (siblings[i] == 0)
            {
                switch (i)
                {
                case SIB_LEFT:
                    ELEMENT_2DIM(arr, row, column - 1, columns) = 1;
                    break;
                case SIB_TOP:
                    ELEMENT_2DIM(arr, row - 1, column, columns) = 1;
                    break;
                case SIB_RIGHT:
                    ELEMENT_2DIM(arr, row, column + 1, columns) = 1;
                    break;
                case SIB_BOTTOM:
                    ELEMENT_2DIM(arr, row + 1, column, columns) = 1;
                    break;
                }
                return i;
            }
        }
    }
    return SIB_NONE;
}


#include <iostream>
using namespace std;
void matrica( long int lines,long  int columns)
{
    long int a[lines][columns];
    for (long int i = 0; i < lines; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cin >> a[i][j];
        }
    }
    if (a[0][0] >= 3 || a[0][columns - 1] >= 3 || a[lines - 1][0] >= 3 || a[lines - 1][columns - 1] >= 3)
    {
        cout << "NO" << endl;
        return;
    }
    else
    {
        a[0][0] = a[0][columns - 1] = a[lines - 1][0] = a[lines - 1][columns - 1] = 2;
    }
    for (long int j = 1; j < lines - 1; j++)
    {
        if (a[j][0] >= 4 || a[j][columns - 1] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[j][0] = a[j][columns - 1] = 3;
        }
    }
    for (long int k = 1; k < columns - 1; k++)
    {
        if (a[0][k] >= 4 || a[lines - 1][k] >= 4)
        {
            cout << "NO" << endl;
            return;
        }
        else
        {
            a[0][k] = a[lines - 1][k] = 3;
        }
    }
    for (long int i = 1; i < lines - 1; i++)
    {
        for (long int j = 1; j < columns - 1; j++)
        {
            if (a[i][j] >= 5)
            {
                cout << "NO" << endl;
            }
            else
            {
                a[i][j] = 4;
            }
        }
    }
    cout << "YES" << endl;
    for (long int i = 0; i < lines; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}
int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        long int n, m;
        cin >> n >> m;
        matrica(n, m);
    }
}


#include <iostream>
using namespace std;
long int get_position(long int lines, long int columns, long int line, long int column)
{
    if (line == 0)
    {
        if (column == 0)
        {
            return 2;
        }
        else if (column == columns - 1)
        {
            return 2;
        }
        return 3;
    }
    if (column == 0)
    {
        if (line == lines - 1)
        {
            return 2;
        }
        return 3;
    }
    if (line == lines - 1)
    {
        if (column == columns - 1)
        {
            return 2;
        }
        return 3;
    }
    if (column == columns - 1)
    {
        return 3;
    }
    return 4;
}
void matrica( long int lines,long  int columns)
{
    long int a[lines][columns];
    for (long int i = 0; i < lines; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cin >> a[i][j];
        }
    }
    for (long int i = 0; i < lines; i++  ) {
        for (long int j = 0; j < columns; j++) {
            long int maximum = get_position (lines, columns, i, j);
            if (a[i][j] > maximum ) {
                cout << "NO" << endl;
                return;
            }
            a[i][j] = maximum;
        }
    }

    cout << "YES" << endl;
    for (long int i = 0; i < lines; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}
int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        long int n, m;
        cin >> n >> m;
        matrica(n, m);
    }
}


#include <iostream>
#include <algorithm>

#define ELEMENT_2DIM(_array, _row, _col, _cols) *(_array + (_row) * (_cols) + (_col))

#define SIB_NONE -1
#define SIB_LEFT 0
#define SIB_TOP 1
#define SIB_RIGHT 2
#define SIB_BOTTOM 3

using namespace std;

enum position
{
    LEFT_TOP,
    TOP,
    RIGHT_TOP,
    RIGHT,
    RIGHT_BOTTOM,
    BOTTOM,
    LEFT_BOTTOM,
    LEFT,
    CENTER
};

void input(long int *arr, long int rows, long int columns);
void process(long int rows, long int columns);
void output(long int *arr, long int rows, long int columns);
position get_position(long int rows, long int columns, long int row, long int column);
int validate_position(long int rows, long int columns, long int row, long int column, long int value);
int validate(long int *arr, long int rows, long int columns);
int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column);
void adjust_all(long int *arr, long int rows, long int columns);

int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        long int lines, columns;
        cin >> lines >> columns;
        process(lines, columns);
    }
}

void process(long int rows, long int columns)
{
    long int array[rows][columns];
    input(&array[0][0], rows, columns);
    if (!validate(&array[0][0], rows, columns))
    {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
    adjust_all(&array[0][0], rows, columns);
    output(&array[0][0], rows, columns);
}
void input(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cin >> ELEMENT_2DIM(arr, i, j, columns);
        }
    }
}
void output(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cout << ELEMENT_2DIM(arr, i, j, columns);
            if (j == columns - 1)
            {
                cout << endl;
            }
            else
            {
                cout << " ";
            }
        }
    }
}

position get_position(long int rows, long int columns, long int row, long int column)
{
    if (row == 0)
    {
        if (column == 0)
        {
            return LEFT_TOP;
        }
        else if (column == columns - 1)
        {
            return RIGHT_TOP;
        }
        return TOP;
    }
    if (column == 0)
    {
        if (row == rows - 1)
        {
            return LEFT_BOTTOM;
        }
        return LEFT;
    }
    if (row == rows - 1)
    {
        if (column == columns - 1)
        {
            return RIGHT_BOTTOM;
        }
        return BOTTOM;
    }
    if (column == columns - 1)
    {
        return RIGHT;
    }
    return CENTER;
}

int validate_position(long int rows, long int columns, long int row, long int column, long int value)
{
    if (value < 0)
    {
        return 0;
    }
    position pos = get_position(rows, columns, row, column);
    switch (pos)
    {
    case LEFT_TOP:
    case LEFT_BOTTOM:
    case RIGHT_TOP:
    case RIGHT_BOTTOM:
        return value < 3;
    case LEFT:
    case RIGHT:
    case TOP:
    case BOTTOM:
        return value < 4;
    default:
        return value < 5;
    }
}

int validate(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            if (!validate_position(rows, columns, i, j, ELEMENT_2DIM(arr, i, j, columns)))
            {
                return 0;
            }
        }
    }
    return 1;
}

void adjust_all(long int *arr, long int rows, long int columns)
{
    long int i = 0;
    while (i < rows)
    {
        long int j = 0;
        while (j < columns)
        {
            int sib = adjust_position(arr, rows, columns, i, j);
            switch (sib)
            {
            case SIB_LEFT:
                i = max(i - 1, 0L);
                j = max(j - 2, 0L);
                continue;
            case SIB_TOP:
                i = max(i - 2, 0L);
                j = max(j - 1, 0L);
                continue;
            case SIB_RIGHT:
                i = max(i - 1, 0L);
                j++;
                continue;
            }
            j++;
        }
        i++;
    }
}

int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column)
{
    int value = ELEMENT_2DIM(arr, row, column, columns);
    if (value != 0)
    {
        long int siblings[4] = {-1, -1, -1, -1};
        position pos = get_position(rows, columns, row, column);
        switch (pos)
        {
        case LEFT_TOP:
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            break;
        case BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT_BOTTOM:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;

        case CENTER:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        }
        long int siblings_count = (siblings[SIB_LEFT] > 0) +
                                  (siblings[SIB_TOP] > 0) +
                                  (siblings[SIB_RIGHT] > 0) +
                                  (siblings[SIB_BOTTOM] > 0);
        if (value <= siblings_count)
        {
            ELEMENT_2DIM(arr, row, column, columns) = siblings_count;
            return SIB_NONE;
        }
        for (int i = 0; i < 4; i++)
        {
            if (siblings[i] == 0)
            {
                switch (i)
                {
                case SIB_LEFT:
                    ELEMENT_2DIM(arr, row, column - 1, columns) = 1;
                    break;
                case SIB_TOP:
                    ELEMENT_2DIM(arr, row - 1, column, columns) = 1;
                    break;
                case SIB_RIGHT:
                    ELEMENT_2DIM(arr, row, column + 1, columns) = 1;
                    break;
                case SIB_BOTTOM:
                    ELEMENT_2DIM(arr, row + 1, column, columns) = 1;
                    break;
                }
                return i;
            }
        }
    }
    return SIB_NONE;
}


#include <iostream>
#include <algorithm>

#define ELEMENT_2DIM(_array, _row, _col, _cols) *(_array + (_row) * (_cols) + (_col))

#define SIB_NONE -1
#define SIB_LEFT 0
#define SIB_TOP 1
#define SIB_RIGHT 2
#define SIB_BOTTOM 3

using namespace std;

enum position
{
    LEFT_TOP,
    TOP,
    RIGHT_TOP,
    RIGHT,
    RIGHT_BOTTOM,
    BOTTOM,
    LEFT_BOTTOM,
    LEFT,
    CENTER
};

void input(long int *arr, long int rows, long int columns);
void process(long int rows, long int columns);
void output(long int *arr, long int rows, long int columns);
position get_position(long int rows, long int columns, long int row, long int column);
int validate_position(long int rows, long int columns, long int row, long int column, long int value);
int validate(long int *arr, long int rows, long int columns);
int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column);
void adjust_all(long int *arr, long int rows, long int columns);

int main()
{
    int x;
    cin >> x;
    for (int i = 0; i < x; i++)
    {
        long int lines, columns;
        cin >> lines >> columns;
        process(lines, columns);
    }
}

void process(long int rows, long int columns)
{
    long int array[rows][columns];
    input(&array[0][0], rows, columns);
    if (!validate(&array[0][0], rows, columns))
    {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
    adjust_all(&array[0][0], rows, columns);
    output(&array[0][0], rows, columns);
}
void input(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cin >> ELEMENT_2DIM(arr, i, j, columns);
        }
    }
}
void output(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            cout << ELEMENT_2DIM(arr, i, j, columns);
            if (j == columns - 1)
            {
                cout << endl;
            }
            else
            {
                cout << " ";
            }
        }
    }
}

position get_position(long int rows, long int columns, long int row, long int column)
{
    if (row == 0)
    {
        if (column == 0)
        {
            return LEFT_TOP;
        }
        else if (column == columns - 1)
        {
            return RIGHT_TOP;
        }
        return TOP;
    }
    if (column == 0)
    {
        if (row == rows - 1)
        {
            return LEFT_BOTTOM;
        }
        return LEFT;
    }
    if (row == rows - 1)
    {
        if (column == columns - 1)
        {
            return RIGHT_BOTTOM;
        }
        return BOTTOM;
    }
    if (column == columns - 1)
    {
        return RIGHT;
    }
    return CENTER;
}

int validate_position(long int rows, long int columns, long int row, long int column, long int value)
{
    if (value < 0)
    {
        return 0;
    }
    position pos = get_position(rows, columns, row, column);
    switch (pos)
    {
    case LEFT_TOP:
    case LEFT_BOTTOM:
    case RIGHT_TOP:
    case RIGHT_BOTTOM:
        return value < 3;
    case LEFT:
    case RIGHT:
    case TOP:
    case BOTTOM:
        return value < 4;
    default:
        return value < 5;
    }
}

int validate(long int *arr, long int rows, long int columns)
{
    for (long int i = 0; i < rows; i++)
    {
        for (long int j = 0; j < columns; j++)
        {
            if (!validate_position(rows, columns, i, j, ELEMENT_2DIM(arr, i, j, columns)))
            {
                return 0;
            }
        }
    }
    return 1;
}

void adjust_all(long int *arr, long int rows, long int columns)
{
    long int i = 0;
    while (i < rows)
    {
        long int j = 0;
        while (j < columns)
        {
            int sib = adjust_position(arr, rows, columns, i, j);
            if (sib != SIB_NONE)
            {
                i = max(i - 2, 0L);
                j = max(j - 2, 0L);
                continue;
            }
            j++;
        }
        i++;
    }
}

int adjust_position(long int *arr, long int rows, long int columns, long int row, long int column)
{
    int value = ELEMENT_2DIM(arr, row, column, columns);
    if (value != 0)
    {
        long int siblings[4] = {-1, -1, -1, -1};
        position pos = get_position(rows, columns, row, column);
        switch (pos)
        {
        case LEFT_TOP:
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_TOP:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        case RIGHT_BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            break;
        case BOTTOM:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT_BOTTOM:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            break;
        case LEFT:
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;

        case CENTER:
            siblings[SIB_LEFT] = ELEMENT_2DIM(arr, row, column - 1, columns);
            siblings[SIB_TOP] = ELEMENT_2DIM(arr, row - 1, column, columns);
            siblings[SIB_RIGHT] = ELEMENT_2DIM(arr, row, column + 1, columns);
            siblings[SIB_BOTTOM] = ELEMENT_2DIM(arr, row + 1, column, columns);
            break;
        }
        long int siblings_count = (siblings[SIB_LEFT] > 0) +
                                  (siblings[SIB_TOP] > 0) +
                                  (siblings[SIB_RIGHT] > 0) +
                                  (siblings[SIB_BOTTOM] > 0);
        if (value <= siblings_count)
        {
            ELEMENT_2DIM(arr, row, column, columns) = siblings_count;
            return SIB_NONE;
        }
        for (int i = 0; i < 4; i++)
        {
            if (siblings[i] == 0)
            {
                switch (i)
                {
                case SIB_LEFT:
                    ELEMENT_2DIM(arr, row, column - 1, columns) = 1;
                    break;
                case SIB_TOP:
                    ELEMENT_2DIM(arr, row - 1, column, columns) = 1;
                    break;
                case SIB_RIGHT:
                    ELEMENT_2DIM(arr, row, column + 1, columns) = 1;
                    break;
                case SIB_BOTTOM:
                    ELEMENT_2DIM(arr, row + 1, column, columns) = 1;
                    break;
                }
                return i;
            }
        }
    }
    return SIB_NONE;
}


#include <bits/stdc++.h>
using namespace std;

int calng(int i,int j,int h,int w){
    //cout<<r<<" "<<c<<" "<<n<<" "<<m;
    int limit = 0;
        if (i > 0) ++limit;
        if (i < h - 1) ++limit;
        if (j > 0) ++limit;
        if (j < w - 1) ++limit;
    return limit;
}
void modi(vector< vector<int> > &arr, int i, int j ,int n,int m){
    //cout<<"inside modi "<<i<<" "<<j<<" "<<n<<" "<<" "<<m<<endl;
    int cnt=0;
    if(i-1>=0  ){
        //cout<<1<<" ";
        if( arr[i-1][j]>0)
            cnt++;
    }
    if(j-1>=0){
       // cout<<2<<" ";
        if(arr[i][j-1]>0)
            cnt++;
    }
    if(i+1<n ){
       // cout<<3<<" ";
        if( arr[i+1][j]>0)
            cnt++;
    }
    if(j+1<m ){
        //cout<<4<<" ";
        if( arr[i][j+1]>0)
            cnt++;
    }
    //cout<<"cnt "<<cnt<<endl;
    arr[i][j]=cnt;
    return;
}
void check(vector< vector<int> > &arr, int i,int j,int n ,int m){
    int cnt=0;
    int c=arr[i][j];
    if(i-1>=0){
        //cout<<"1"<<endl;
        if( arr[i-1][j]>0){
            modi(arr,i-1,j,n,m);
            cnt++;
            if(cnt==c)
                return;
        }
    }

    if(j-1>=0){
        //cout<<2<<endl;
        if( arr[i][j-1]>0){
            modi(arr,i,j-1,n,m);
            cnt++;
            if(cnt==c)
                return;
        }
    }

    if(i+1<n){
        //cout<<3<<endl;
        if( arr[i+1][j]>0){
            modi(arr,i+1,j,n,m);
            cnt++;
            if(cnt==c)
                return;
        }
    }

    if(j+1<m){
        //cout<<4<<endl;
        if( arr[i][j+1]>0){
            modi(arr,i,j+1,n,m);
            cnt++;
            if(cnt==c)
                return;
        }
    }
    if(i-1>=0 ){
        if(arr[i-1][j]==0){
            arr[i-1][j]=1;
            cnt++;
            if(cnt==c)
                return;
        }

    }
    if(j-1>=0 ){
        if(arr[i][j-1]==0){
            arr[i][j-1]=1;
        cnt++;
        if(cnt==c)
            return;
        }

    }
    if(i+1<n ){
        if(arr[i+1][j]==0){
            arr[i+1][j]=1;
        cnt++;
        if(cnt==c)
            return;
        }

    }
    if(j+1<m ){
        if(arr[i][j+1]==0){
            arr[i][j+1]=1;
        cnt++;
        if(cnt==c)
            return;
        }

    }
}
void solve()
{
    int n,m;
    cin>>n>>m;
    vector< vector<int> > arr(n);
    vector< pair<int,int> > ind;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int x;
            cin>>x;
            arr[i].push_back(x);
            if(x>0){
                ind.push_back(make_pair(i,j));
            }
        }
    }
    //cout<<"size "<<ind.size()<<endl;
    int i,j;
    for(i=0;i<ind.size();++i){
        int r,c;
        r=ind[i].first;
        c=ind[i].second;
        //cout<<"index "<<r<<" "<<c<<" "<<arr[r][c]<<" ";
        int ng=calng(r,c,n,m);
        //cout<<ng<<" "<<endl;
        if(ng<arr[r][c]){
            //cout<<ng<<" "<<r<<" "<<c<<" "<<arr[r][c]<<endl;
            cout<<"NO"<<endl;
            return;
        }
        check(arr,r,c,n,m);
    }
    //cout<<"here"<<endl;
    for( i=0;i<ind.size();++i){
        int r,c;
        r=ind[i].first;
        c=ind[i].second;
        modi(arr,r,c,n,m);
    }
    cout<<"YES"<<endl;
    for(i=0;i<n;i++){
        for( j=0;j<m;j++){
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    return;
}
int main()
{

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

        solve();
    }
    return 0;
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProblemSolving
{
    class Program
    {
        static void Main(string[] args)
        {
            string m = Console.ReadLine();
            int rounds = Convert.ToInt16(m);
            int[] outputs = new int[2 * rounds];
            int[] tries = new int[2];
            for (int i = 0; i < 2*rounds; i += 2)
            {
                string m1 = Console.ReadLine();
                toint(m1, ref tries);
                outputs[i] = tries[0];
                outputs[i + 1] = tries[1];
                tries[0] = 0;
                tries[1] = 0;
            }
            //even numbers for Mishaka & odd for Chris
            int Mwin = 0, Cwin = 0;
            for (int i = 0; i < 2*rounds; i += 2)
            {
                if (outputs[i] > outputs[i + 1])
                    Mwin++;
                else if (outputs[i + 1] > outputs[i])
                    Cwin++;
            }
            if (Mwin > Cwin)
                Console.WriteLine("Mishka");
            else if (Cwin > Mwin)
                Console.WriteLine("Chris");
            else if (Cwin == Mwin)
                Console.WriteLine("Friendship is magic!^^");

          //  Console.ReadKey();
        }
        public static void toint(string m1, ref int[] i1)
        {
            StringBuilder in1 = new StringBuilder();
            int i = 0;
            int counter = 0;
            foreach (char c in m1)
            {

                if ((c >= '0' && c <= '9'))
                {
                    in1.Append(c);
                }
                else if (c == ' ')
                {
                    if (i1[counter] == 0)
                    {
                        string m = in1.ToString();
                        i1[counter] = Convert.ToInt32(m);
                        counter++;
                        in1.Clear();
                    }

                }

                i++;
                if (i == m1.Length)
                {
                    string m = in1.ToString();
                    i1[counter] = Convert.ToInt32(m);
                    in1.Clear();
                    break;
                }

            }
        }
    }
}