#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#define mt make_tuple
#define ll long long
#define eb emplace_back
#define fi first
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define scanVec(vec , n) for(int i = 0; i < n ; i++){ scanf("%d",&vec[i]);}
#define printVec(vec , n) for(int i = 0; i < n ; i++){ cout<<vec[i]<<" ";}
#define mod(a,b) ((a%b +b)%b)
#define bit(x,i) (x&(1<<i))  //select the bit of position i of x
#define lowbit(x) ((x)&((x)^((x)-1))) //get the lowest bit of x
#define hBit(msb,n) asm("bsrl %1,%0" : "=r"(msb) : "r"(n)) //get the highest bit of x, maybe the fastest
#define max(a,b) (a<b?b:a)
#define min(a,b) (a>b?b:a)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define IN(i,l,r) (l<i&&i<r)
#define LINR(i,l,r) (l<=i&&i<=r)
#define LIN(i,l,r) (l<=i&&i<r)
#define INR(i,l,r) (l<i&&i<=r)
#define lastEle(vec) vec[vec.size()-1]
#define SZ(x) ((int)((x).size()))
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define INF 1001001001
#define PI 3.1415926535897932384626

using namespace std;
 using namespace __gnu_pbds;
 template <typename T>
 using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}

//----------------------------------------------------------------------------------------------------------------------

void swap(int & a, int & b){
    auto temp = a;
    a = b;
    b = temp;
}

const ll MOD = 1e9 + 7;

ll inf = 1LL*1e11;

void no(){
    cout <<"REBEL" << endl;
}

void yes(){
    cout <<"OBEY" << endl;
}

// int gcdExtended(int a, int b, int *x, int *y);

// // Function to find modulo inverse of a
// void modInverse(int a, int m)
// {
//     int x, y;
//     int g = gcdExtended(a, m, &x, &y);
//     if (g != 1)
//         return INF;
//     else
//     {
//         // m is added to handle negative x
//         int res = (x%m + m) % m;
//         return res;
//     }
// }

// // C function for extended Euclidean Algorithm
// int gcdExtended(int a, int b, int *x, int *y)
// {
//     // Base Case
//     if (a == 0)
//     {
//         *x = 0, *y = 1;
//         return b;
//     }

//     int x1, y1; // To store results of recursive call
//     int gcd = gcdExtended(b%a, a, &x1, &y1);

//     // Update x and y using results of recursive
//     // call
//     *x = y1 - (b/a) * x1;
//     *y = x1;

//     return gcd;
// }

ll fnd(ll a ,ll b, ll g){
	ll left = 0;
	ll right = 1e9;
	ll mx = 0;
	while(left <= right){
		ll c = left + (right - left)/2;
		if(g + c*a < b){
			mx = max(mx,c);
			left = c +1;
		}else{
			right = c -1;
		}
	}
	return mx + 1;

}

void solve(){
	ll a,b,k;
	cin >> a >> b>> k;
	if(a > b) swap(a,b);
	ll mx = 0;
	ll g = __gcd(a,b);
	ll cnt = fnd(a,b,g);
	//cout << cnt << endl;
	if(cnt < k) yes();
	 else no();
}

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


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#define ll long long
#define ull unsigned long long
using namespace std;
inline long long getint()
{
	long long x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return f?x:-x;
}
long long T,p1,p2,k;
int main()
{
//	freopen("color.in","r",stdin);
//  freopen("color.out","w",stdout);
	T=getint();
	while(T--)
	{
		p1=getint(),p2=getint(),k=getint();
		if(p1<p2)swap(p1,p2);
		int gcd=__gcd(p1,p2);
		p1/=gcd,p2/=gcd;
		if(p1==p2&&k!=1)printf("OBEY\n");
		else if(k==1)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2>k)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2<=k)printf("OBEY\n");
		else
		{
			int now=p1/p2;
			if(now>=k)printf("REBEL\n");
			else printf("OBEY\n");
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#define ll long long
#define ull unsigned long long
using namespace std;
inline long long getint()
{
	long long x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return f?x:-x;
}
long long T,p1,p2,k;
int main()
{
//	freopen("color.in","r",stdin);
//  freopen("color.out","w",stdout);
	T=getint();
	while(T--)
	{
		p1=getint(),p2=getint(),k=getint();
		int gcd=__gcd(p1,p2);
		p1/=gcd,p2/=gcd;
		if(p1<p2)swap(p1,p2);
		if(p1==p2&&k!=1)printf("OBEY\n");
		else if(k==1)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2>k)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2<=k)printf("OBEY\n");
		else
		{
			int now=(p1-2)/p2+1;
			if(now>=k)printf("REBEL\n");
			else printf("OBEY\n");
		}
	}
	return 0;
}


#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<set>
#define re register
#define FOR(i,a,b)	for(re int i=(a);i<=(b);i++)
#define ROF(i,a,b)	for(re int i=(a);i>=(b);i--)
#define gc _p1==_p2&&(_p2=(_p1=_buf)+fread(_buf,1,1<<15,stdin),_p1==_p2)?EOF:*_p1++
#define pc(x) (_p3-_obuf<(1<<15))?(*_p3++=x):(fwrite(_obuf,1,_p3-_obuf,stdout),_p3=_obuf,*_p3++=x)
#define endwt (fwrite(_obuf,_p3-_obuf,1,stdout),_p3=_obuf)
using namespace std;
typedef long long ll;

char _buf[1<<15],*_p1,*_p2;
template<typename T>inline void rd(T &x){
	x=0;	static char t;	t=gc;
	static bool flag;	flag=false;
	while(!isdigit(t) && t!='-')	t=gc;
	if(t=='-')	flag=true,t=gc;
	while(isdigit(t))	x=(x<<3)+(x<<1)+t-'0',t=gc;
	return (flag?x=-x:x),void();
}
char _obuf[1<<15],*_p3=_obuf;
template<typename T>inline void wt(T x){
	static char st[129];	static int tp;	tp=0;
	if(x<0)	pc('-'),x=-x;
	if(!x)	return pc('0'),void();
	while(x)	st[++tp]=x%10+'0',x/=10;
	while(tp)	pc(st[tp--]);
	return ;
}
inline void swt(char *x){
	while((*x)!=0)	pc(*x++);
	return ;
}

ll _gcd(ll x,ll y){	return (!x)?y:_gcd(y%x,x);	}
inline ll gcd(ll x,ll y){	return (x>y)?_gcd(y,x):_gcd(x,y);	}

int T;
ll n,m,k,g,t1,t2;

int main(){
//	freopen("color.in","r",stdin);
//	freopen("bf2.out","w",stdout);
	rd(T);
	while(T--){
		rd(n),rd(m),rd(k);
		if(n>m)	swap(n,m);
		g=gcd(n,m);
		n/=g,m/=g;
		if(k==1LL){	swt("REBEL\n");	continue;	}
		if((m-2)/n+1>=k)	swt("REBEL\n");
		else	swt("OBEY\n");
	}
	return endwt,0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#define ll long long
#define ull unsigned long long
using namespace std;
inline long long getint()
{
	long long x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return f?x:-x;
}
long long T,p1,p2,k;
int main()
{
//	freopen("color.in","r",stdin);
//  freopen("color.out","w",stdout);
	T=getint();
	while(T--)
	{
		p1=getint(),p2=getint(),k=getint();
		if(p1<p2)swap(p1,p2);
		int gcd=__gcd(p1,p2);
		p1/=gcd,p2/=gcd;
		if(p1==p2&&k!=1)printf("OBEY\n");
		else if(k==1)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2>k)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2<=k)printf("OBEY\n");
		else
		{
			p1-=2;
			int now=p1/p2+1;
			if(now>=k)printf("REBEL\n");
			else printf("OBEY\n");
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#define ll long long
#define ull unsigned long long
using namespace std;
inline long long getint()
{
	long long x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return f?x:-x;
}
long long T,p1,p2,k;
int main()
{
//	freopen("color.in","r",stdin);
//  freopen("color.out","w",stdout);
	T=getint();
	while(T--)
	{
		p1=getint(),p2=getint(),k=getint();
		if(p1<p2)swap(p1,p2);
		if(p1==p2&&k!=1)printf("OBEY\n");
		else if(k==1)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2>k)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2<=k)printf("OBEY\n");
		else
		{
			int now=p1/p2+1;
			if(now>=k)printf("REBEL\n");
			else printf("OBEY\n");
		}
	}
	return 0;
}


#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<set>
#define re register
#define FOR(i,a,b)	for(re int i=(a);i<=(b);i++)
#define ROF(i,a,b)	for(re int i=(a);i>=(b);i--)
#define gc _p1==_p2&&(_p2=(_p1=_buf)+fread(_buf,1,1<<15,stdin),_p1==_p2)?EOF:*_p1++
#define pc(x) (_p3-_obuf<(1<<15))?(*_p3++=x):(fwrite(_obuf,1,_p3-_obuf,stdout),_p3=_obuf,*_p3++=x)
#define endwt (fwrite(_obuf,_p3-_obuf,1,stdout),_p3=_obuf)
using namespace std;
typedef long long ll;

char _buf[1<<15],*_p1,*_p2;
template<typename T>inline void rd(T &x){
	x=0;	static char t;	t=gc;
	static bool flag;	flag=false;
	while(!isdigit(t) && t!='-')	t=gc;
	if(t=='-')	flag=true,t=gc;
	while(isdigit(t))	x=(x<<3)+(x<<1)+t-'0',t=gc;
	return (flag?x=-x:x),void();
}
char _obuf[1<<15],*_p3=_obuf;
template<typename T>inline void wt(T x){
	static char st[129];	static int tp;	tp=0;
	if(x<0)	pc('-'),x=-x;
	if(!x)	return pc('0'),void();
	while(x)	st[++tp]=x%10+'0',x/=10;
	while(tp)	pc(st[tp--]);
	return ;
}
inline void swt(char *x){
	while((*x)!=0)	pc(*x++);
	return ;
}

ll _gcd(ll x,ll y){	return (!x)?y:_gcd(y%x,x);	}
inline ll gcd(ll x,ll y){	return (x>y)?_gcd(y,x):_gcd(x,y);	}

int T;
ll n,m,k,g,t1,t2;

int main(){
//	freopen("color.in","r",stdin);
//	freopen("bf2.out","w",stdout);
	rd(T);
	while(T--){
		rd(n),rd(m),rd(k);
		if(n>m)	swap(n,m);
		g=gcd(n,m);
		n/=g,m/=g;
		if(k==1LL){	swt("NO\n");	continue;	}
		if((m-2)/n+1>=k)	swt("NO\n");
		else	swt("YES\n");
	}
	return endwt,0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#define ll long long
#define ull unsigned long long
using namespace std;
inline long long getint()
{
	long long x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return f?x:-x;
}
long long T,p1,p2,k;
int main()
{
//	freopen("color.in","r",stdin);
//  freopen("color.out","w",stdout);
	T=getint();
	while(T--)
	{
		p1=getint(),p2=getint(),k=getint();
		if(p1<p2)swap(p1,p2);
		if(p1==p2&&k!=1)printf("OBEY\n");
		else if(k==1)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2>k)printf("REBEL\n");
		else if(p1%p2==0&&p1/p2<=k)printf("OBEY\n");
		else
		{
			int now=p1/p2;
			if(now>=k)printf("REBEL\n");
			else printf("OBEY\n");
		}
	}
	return 0;
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(r==b){
            cout<<"OBEY"<<endl;
            continue;
        }
        if(b>r) swap(r,b);  //r>b
        ll temp = 0;
        temp = (r+(r%b)-1)/b;

        if(temp>=k){
            cout<<"REBEL"<<endl;
        }
        else{
            cout<<"OBEY"<<endl;
        }
    }
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(r==b){
            cout<<"OBEY"<<endl;
            continue;
        }
        if(b>r) swap(r,b);  //r>b
        lcm = (r*b)/gcd(r,b);
        stp = lcm/r;
        ll del = 0;
        if((2*r)%b>r%b){
            del = stp-1;
        }
        else {
            del = 1;
        }
        ll temp = 0;
        temp = (r+(del*r%b)-1)/b;

        if(temp>=k){
            cout<<"REBEL"<<endl;
        }
        else{
            cout<<"OBEY"<<endl;
        }
    }
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(r==b){
            cout<<"OBEY"<<endl;
            continue;
        }
        if(b>r) swap(r,b);  //r>b
        lcm = r*b/gcd(r,b);
        stp = lcm/r;
        ll temp = 0;
        for(ll i=1;i<=stp;i++){
            temp = i*r - ((i-1)*r - (i-1)*r%b) - 1;
            if(temp/b>=k){
                cout<<"REBEL"<<endl;
                break;
            }
            if(i==stp){
                cout<<"OBEY"<<endl;
            }
        }
    }
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(r==b){
            cout<<"OBEY"<<endl;
            continue;
        }
        if(b>r) swap(r,b);  //r>b
        ll temp = 0;
        if(r%b>1){
            temp = r/b + 1;
        }
        else if(r%b==1){
            temp = r/b;
        }
        else{
            temp = r/b - 1;
        }

        if(temp>=k){
            cout<<"REBEL"<<endl;
        }
        else{
            cout<<"OBEY"<<endl;
        }
    }
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(r==b){
            cout<<"OBEY"<<endl;
            continue;
        }
        if(b>r) swap(r,b);  //r>b
        lcm = (r*b)/gcd(r,b);
        stp = lcm/r;
        ll del = b/stp;
        ll temp = 0;
        temp = (r+b-del-1)/b;

        if(temp>=k){
            cout<<"REBEL"<<endl;
        }
        else{
            cout<<"OBEY"<<endl;
        }
    }
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(r==b){
            cout<<"OBEY"<<endl;
            continue;
        }
        if(b>r) swap(r,b);  //r>b
        lcm = r*b/gcd(r,b);
        stp = lcm/r;
        ll temp = 0;
        if(stp>b){
            temp = r + (b-1)*r%b-1;
        }
        else{
            temp = r+stp*r%b-1;
        }
        if(temp/b>=k){
            cout<<"REBEL"<<endl;
        }
        else{
            cout<<"OBEY"<<endl;
        }
    }
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(b>r) swap(r,b);  //r>b
        lcm = r*b/gcd(r,b);
        stp = lcm/r;
        ll temp = 0;
        for(int i=1;i<=stp;i++){
            temp = (i*r)%b;
            if(temp==0) temp = b;
            if((r-temp)/b>=k){
                cout<<"REBEL"<<endl;
                break;
            }
            if(i==stp){
                cout<<"OBEY"<<endl;
            }
        }
    }
}


#include <iostream>
using namespace std;
typedef long long ll;
ll r,b,k;
ll lcm;
ll stp;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
    int t;cin>>t;
    while(t--){
        cin>>r>>b>>k;
        if(r==b){
            cout<<"OBEY"<<endl;
            continue;
        }
        if(b>r) swap(r,b);  //r>b
        lcm = r*b/gcd(r,b);
        stp = lcm/r;
        ll temp = 0;
        if(stp>=b){
            temp = r + (b-1)*r%b-1;
        }
        else{
            temp = r+stp*r%b-1;
        }
        if(temp/b>=k){
            cout<<"REBEL"<<endl;
        }
        else{
            cout<<"OBEY"<<endl;
        }
    }
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
ll n,m;
bool ifpossible()
{
    if(matrix[1][1]>2)return false;
    if(matrix[1][m]>2)return false;
    if(matrix[n][1]>2)return false;
    if(matrix[n][m]>2)return false;
    for(int i=2;i<=m-1;i++)if(matrix[1][i]>3)return false;
    for(int i=2;i<=m-1;i++)if(matrix[n][i]>3)return false;
    for(int i=2;i<=n-1;i++)if(matrix[i][1]>3)return false;
    for(int i=2;i<=n-1;i++)if(matrix[i][m]>3)return false;
    for(int i=2;i<=n-1;i++){
        for(int j=2;j<=m-1;j++){
            if(matrix[i][j]>4)return false;
        }
    }
    return true;
}
void turn()
{
    matrix[1][1]=2;
    matrix[1][m]=2;
    matrix[n][1]=2;
    matrix[n][m]=2;
    for(int i=2;i<=m-1;i++)matrix[1][i]=3;
    for(int i=2;i<=m-1;i++)matrix[n][i]=3;
    for(int i=2;i<=n-1;i++)matrix[i][1]=3;
    for(int i=2;i<=n-1;i++)matrix[i][m]=3;
    for(int i=2;i<=n-1;i++){
        for(int j=2;j<=m-1;j++){
            matrix[i][j]=4;
        }
    }
}
void Dufaux()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    bool ans=ifpossible();
    if(!ans){
        cout<<"NO"<<endl;
    }
    else{
        cout<<"YES"<<endl;
        turn();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    memset(matrix,0,sizeof(matrix));
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
bool ageiok()
{
    vector<pii>vec;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>0)vec.pb(mp(i,j));
        }
    }
    for(int i=0;i<vec.size();i++){
        if(matrix[vec[i].ff][vec[i].ss]!=neighboursareok(vec[i].ff,vec[i].ss)){
            return false;
        }
    }
    return true;
}
void print()
{
    if(ageiok()==false){
        cout<<"NO"<<endl;
        return;
    }
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>4){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    if(ageiok()==true){
        print();
        return;
    }
    while(true){
        bool ok=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else{
                        matrix[i][j]+=1;
                        ok=false;
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                }
            }
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
ll n,m;
bool ifpossible()
{
    if(matrix[1][1]>2)return false;
    if(matrix[1][m]>2)return false;
    if(matrix[n][1]>2)return false;
    if(matrix[n][m]>2)return false;
    for(int i=2;i<=m-1;i++)if(matrix[1][i]>3)return false;
    for(int i=2;i<=m-1;i++)if(matrix[n][i]>3)return false;
    for(int i=2;i<=n-1;i++)if(matrix[i][1]>3)return false;
    for(int i=2;i<=n-1;i++)if(matrix[i][n]>3)return false;
    for(int i=2;i<=n-1;i++){
        for(int j=2;j<=m-1;j++){
            if(matrix[i][j]>4)return false;
        }
    }
    return true;
}
void turn()
{
    matrix[1][1]=2;
    matrix[1][m]=2;
    matrix[n][1]=2;
    matrix[n][m]=2;
    for(int i=2;i<=m-1;i++)matrix[1][i]=3;
    for(int i=2;i<=m-1;i++)matrix[n][i]=3;
    for(int i=2;i<=n-1;i++)matrix[i][1]=3;
    for(int i=2;i<=n-1;i++)matrix[i][m]=3;
    for(int i=2;i<=n-1;i++){
        for(int j=2;j<=m-1;j++){
            matrix[i][j]=4;
        }
    }
}
void Dufaux()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    bool ans=ifpossible();
    if(!ans){
        cout<<"NO"<<endl;
    }
    else{
        cout<<"YES"<<endl;
        turn();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    memset(matrix,0,sizeof(matrix));
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
bool ageiok()
{
    vector<pii>vec;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>0)vec.pb(mp(i,j));
        }
    }
    for(int i=0;i<vec.size();i++){
        if(matrix[vec[i].ff][vec[i].ss]!=neighboursareok(vec[i].ff,vec[i].ss)){
            return false;
        }
    }
    return true;
}
void print()
{
    if(ageiok()==false){
        cout<<"NO"<<endl;
        return;
    }
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>4){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    if(ageiok()==true){
        print();
        return;
    }
    while(true){
        bool ok=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        continue;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else{
                        matrix[i][j]+=(target-matrix[i][j]);
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                        ok=false;
                    }
                }
            }
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
bool ageiok()
{
    vector<pii>vec;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>0)vec.pb(mp(i,j));
        }
    }
    for(int i=0;i<vec.size();i++){
        if(matrix[vec[i].ff][vec[i].ss]!=neighboursareok(vec[i].ff,vec[i].ss)){
            return false;
        }
    }
    return true;
}
void print()
{
    if(ageiok()==false){
        cout<<"NO"<<endl;
        return;
    }
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>4){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    if(ageiok()==true){
        print();
        return;
    }
    while(true){
        bool ok=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                        continue;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else{
                        matrix[i][j]+=(target-matrix[i][j]);
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                        ok=false;
                    }
                }
            }
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
void print()
{
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    while(true){
        bool ok=false;
        bool dhukseatleast=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                        dhukseatleast=true;
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else {
                        matrix[i][j]+=1;
                        ok=false;
                    }
                }
            }
        }
        if(dhukseatleast==false){
            print();
            return;
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
ll n,m;
bool ifpossible()
{
    if(matrix[1][1]>2)return false;
    if(matrix[1][m]>2)return false;
    if(matrix[n][1]>2)return false;
    if(matrix[n][m]>2)return false;
    for(int i=2;i<=m-1;i++)if(matrix[1][i]>3)return false;
    for(int i=2;i<=m-1;i++)if(matrix[n][i]>3)return false;
    for(int i=2;i<=n-1;i++)if(matrix[i][1]>3)return false;
    for(int i=2;i<=n-1;i++)if(matrix[i][n]>3)return false;
    for(int i=2;i<=n-1;i++){
        for(int j=2;j<=m-1;j++){
            if(matrix[i][j]>4)return false;
        }
    }
    return true;
}
void turn()
{
    matrix[1][1]=2;
    matrix[1][m]=2;
    matrix[n][1]=2;
    matrix[n][m]=2;
    for(int i=2;i<=m-1;i++)matrix[1][i]=3;
    for(int i=2;i<=m-1;i++)matrix[n][i]=3;
    for(int i=2;i<=n-1;i++)matrix[i][1]=3;
    for(int i=2;i<=n-1;i++)matrix[i][m]=3;
    for(int i=2;i<=n-1;i++){
        for(int j=2;j<=m-1;j++){
            matrix[i][j]=4;
        }
    }
}
void Dufaux()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    bool ans=ifpossible();
    if(!ans){
        cout<<"NO"<<endl;
    }
    else{
        cout<<"YES"<<endl;
        turn();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
void print()
{
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    while(true){
        bool ok=false;
        bool dhukseatleast=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        dhukseatleast=true;
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else {
                        matrix[i][j]+=1;
                        ok=false;
                    }
                }
            }
        }
        if(dhukseatleast==false){
            print();
            return;
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
void print()
{
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
bool ageiok()
{
    vector<pii>vec;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>0)vec.pb(mp(i,j));
        }
    }
    for(int i=0;i<vec.size();i++){
        if(matrix[vec[i].ff][vec[i].ss]!=neighboursareok(vec[i].ff,vec[i].ss)){
            return false;
        }
    }
    return true;
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    if(ageiok()==true){
        print();
        return;
    }
    while(true){
        bool ok=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else {
                        matrix[i][j]+=1;
                        ok=false;
                    }
                }
            }
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const ll inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
ll n,m;
ll dx[]={1,-1,0,0};
ll dy[]={0,0,1,-1};
ll neighboursareok(ll i,ll j)
{
    ll cnt=0;
    for(ll k=0;k<4;k++){
        ll mm=i+dx[k];
        ll nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(ll i,ll j,ll target)
{
    ll cnt=0;
    for(ll k=0;k<4;k++){
        ll mm=i+dx[k];
        ll nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(ll i,ll j,ll target)
{
        ll required=matrix[i][j]-target;
        for(ll k=0;k<4 && required>0;k++){
            ll mm=i+dx[k];
            ll nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
bool ageiok()
{
    vector<pii>vec;
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            if(matrix[i][j]>0)vec.pb(mp(i,j));
        }
    }
    for(ll i=0;i<vec.size();i++){
        if(matrix[vec[i].ff][vec[i].ss]!=neighboursareok(vec[i].ff,vec[i].ss)){
            return false;
        }
    }
    return true;
}
void prll()
{
    if(ageiok()==false){
        cout<<"NO"<<endl;
        return;
    }
    cout<<"YES"<<endl;
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            if(matrix[i][j]>4){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    if(ageiok()==true){
        prll();
        return;
    }
    while(true){
        bool ok=false;
        for(ll i=1;i<=n;i++){
            for(ll j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    ll target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                        continue;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else{
                        matrix[i][j]+=(target-matrix[i][j]);
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                        ok=false;
                    }
                }
            }
        }
        if(ok==true){
            prll();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	ll test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
bool ageiok()
{
    vector<pii>vec;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>0)vec.pb(mp(i,j));
        }
    }
    for(int i=0;i<vec.size();i++){
        if(matrix[vec[i].ff][vec[i].ss]!=neighboursareok(vec[i].ff,vec[i].ss)){
            return false;
        }
    }
    return true;
}
void print()
{
    if(ageiok()==false){
        cout<<"NO"<<endl;
        return;
    }
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    if(ageiok()==true){
        print();
        return;
    }
    while(true){
        bool ok=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else {
                        matrix[i][j]+=1;
                        ok=false;
                    }
                }
            }
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 1000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
bitset<maxx/2>vis;
vector<ll>prime;
void sieve(){
    ll x=maxx/2, y=sqrt(maxx)/2;
    for(ll i=1;i<=y;i++){
        if(vis[i]==0){
            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
                vis[j]=1;
        }
    }
    prime.push_back(2);
    for(ll i=3;i<maxx;i+=2)
        if(vis[i/2]==0)
        prime.push_back(i);
}
bool isprime(ll a)
{
    if(a==2){
        return true;
    }
    if(a%2==0){
        return false;
    }
    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++){
        if(a%prime[i]==0){
            return false;
        }
    }
    return true;
}
ll lcm(ll a,ll b)
{
    return (a*b)/__gcd(a,b);
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll matrix[300][300];
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
ll neighboursareok(int i,int j)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m)
            if(matrix[mm][nn]>0)cnt++;
    }
    return cnt;
}
bool increasing(int i,int j,int target)
{
    int cnt=0;
    for(int k=0;k<4;k++){
        int mm=i+dx[k];
        int nn=j+dy[k];
        if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)cnt++;
    }
    if(cnt+target>=matrix[i][j]){
        return true;
    }
    return false;
}
void increase(int i,int j,int target)
{
        int required=matrix[i][j]-target;
        for(int k=0;k<4 && required>0;k++){
            int mm=i+dx[k];
            int nn=j+dy[k];
            if(mm>=1 && mm<=n && nn>=1 &&nn<=m && matrix[mm][nn]==0)matrix[mm][nn]++,required--;;
        }
}
bool ageiok()
{
    vector<pii>vec;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>0)vec.pb(mp(i,j));
        }
    }
    for(int i=0;i<vec.size();i++){
        if(matrix[vec[i].ff][vec[i].ss]!=neighboursareok(vec[i].ff,vec[i].ss)){
            return false;
        }
    }
    return true;
}
void print()
{
    if(ageiok()==false){
        cout<<"NO"<<endl;
        return;
    }
    cout<<"YES"<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
}
void Dufaux()
{
    cin>>n>>m;
    vector<pii>pos;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>matrix[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(matrix[i][j]>4){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    if(ageiok()==true){
        print();
        return;
    }
    while(true){
        bool ok=false;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(matrix[i][j]>0){
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                    int target=neighboursareok(i,j);
                    if(target==matrix[i][j]){
                        ok=true;
                    }
                    else if(target<matrix[i][j]){
                        if(increasing(i,j,target)==true){
                            increase(i,j,target);
                            ok=false;
                        }
                        else{
                            cout<<"NO"<<endl;
                            return;
                        }
                    }
                    else{
                        matrix[i][j]+=(target-matrix[i][j]);
                        if(matrix[i][j]>4){
                            cout<<"NO"<<endl;
                            return;
                        }
                        ok=false;
                    }
                }
            }
        }
        if(ok==true){
            print();
            return;
        }
    }
}
int main()
{
	FastIO;
	///sieve();
	int test;
	cin>>test;
	while(test--){
        Dufaux();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;
        ll x=n+m-2;
        ll j,i;
        ll a[n][m],c=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                ll y=a[i][j];
                if(y>x)
                    c=1;
            }
        }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {

                        cout<<x<<' ';

                }
                cout<<nl;
            }

        }

    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;

        ll j,i;
        ll a[n][m],c=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
     		cin>>a[i][j];
int x=a[i][j];
if(i==0&&j==0)||==0&&j==n-1)||(i==n-1&&j==0)||(i==n-1&&j==n-1)
{
if(x>2)
c=1;
else
a[i][j]=2;
}
            }
else if (i==0||i==n-1||j==0||j==n-1)
{
if(x>3)
c=1;
else
a[i][j]=3;
}
else {
if(x>4)
c=1;
else
a[i][j]=4;
      }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {

          cout<<a[i][j]<<' ';

                }
                cout<<nl;
            }

        }

    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;

        ll j,i;
        ll a[n][m],c=0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                cin>>a[i][j];
                int x=a[i][j];
                if((i==0&&j==0)||(i==0&&j==n-1)||(i==n-1&&j==0)||(i==n-1&&j==n-1))
                {
                    if(x>2)
                        c=1;
                    else
                        a[i][j]=2;
                }

                else if (i==0||i==n-1||j==0||j==n-1)
                {
                    if(x>3)
                        c=1;
                    else
                        a[i][j]=3;
                }
                else
                {
                    if(x>4)
                        c=1;
                    else
                        a[i][j]=4;
                }
            }
        }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0; i<n; i++)
            {
                for(j=0; j<m; j++)
                {

                    cout<<a[i][j]<<' ';

                }
                cout<<nl;
            }

        }

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;
        ll x=n+m-2;
        ll j,i;
        ll a[n][m],c=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                ll y=a[i][j];
                if(y>x)
                    c=1;
            }
        }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(a[i][j]==0)
                        cout<<1<<' ';
                    else
                        cout<<a[i][j]<<' ';
                }
                cout<<nl;
            }

        }

    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;
        ll x=n+m-2;
        ll j,i;
        ll a[n][m],c=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                ll y=a[i][j];
                if(y>x)
                    c=1;
            }
        }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(a[i][j]!=0)
                    {
                        ll k=0,l,g=a[i][j];
                           for(l=i+1;l<n;l++)
                                if(a[l][j]==0&&k<g)
                                    k++,a[l][j]=1;

                                for(l=i-1;i>=0;i--)
                                    if(a[l][j]==0&&k<g)
                                    k++,a[l][j]=1;

                            for(l=j+1;l<m;l++)
                                if(a[l][j]==0&&k<g)
                                    k++,a[l][j]=1;
                            for(l=j-1;l>=0;l--)
                                if(a[l][j]==0&&k<g)
                                    k++,a[l][j]=1;

                    }

                }

            }
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    cout<<a[i][j]<<' ';
                }
                cout<<nl;
            }

        }

    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;

        ll j,i;
        ll a[n][m],c=0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                cin>>a[i][j];
                int x=a[i][j];
                if((i==0&&j==0)||(i==0&&j==m-1)||(i==n-1&&j==0)||(i==n-1&&j==m-1))
                {
                    if(x>2)
                        c=1;
                    else
                        a[i][j]=2;
                }

                else if (i==0||i==n-1||j==0||j==m-1)
                {
                    if(x>3)
                        c=1;
                    else
                        a[i][j]=3;
                }
                else
                {
                    if(x>4)
                        c=1;
                    else
                        a[i][j]=4;
                }
            }
        }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0; i<n; i++)
            {
                for(j=0; j<m; j++)
                {

                    cout<<a[i][j]<<' ';

                }
                cout<<nl;
            }

        }

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;

        ll j,i;
        ll a[n][m],c=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
     		cin>>a[i][j];
int x=a[i][j];
if((i==0&&j==0)||(i==0&&j==n-1)||(i==n-1&&j==0)||(i==n-1&&j==n-1))
{
if(x>2)
c=1;
else
a[i][j]=2;
}

else if (i==0||i==n-1||j==0||j==n-1)
{
if(x>3)
c=1;
else
a[i][j]=3;
}
else {
if(x>4)
c=1;
else
a[i][j]=4;
      }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {

          cout<<a[i][j]<<' ';

                }
                cout<<nl;
            }

        }

  }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define yes cout<<"YES"<<nl
#define no  cout<<"NO"<<nl
#define nl  '\n'
#define tr ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define tst int t;cin>>t;while(t--)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define vi    vector<int>
#define    vl    vector<ll>
#define    si    set<int>
#define    sl    set<ll>
#define    msi    multiset<int>
#define    msl    multiset<ll>
#define    pii    pair<int,int>
#define    pil    pair<int,ll>
#define    pli    pair<ll,int>
#define    pll    pair<ll,ll>
#define    mp    make_pair
#define    pb    push_back

int main()
{
    tr;
    tst
    {
        ll n,m;
        cin>>n>>m;
        ll x=n+m-2;
        ll j,i;
        ll a[n][m],c=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>a[i][j];
                ll y=a[i][j];
                if(y>x)
                    c=1;
            }
        }
        if(c==1)
            no;
        else
        {
            yes;
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(a[i][j]!=0)
                    {
                        ll k,l;
                        for(k=1;k<=a[i][j];k++)
                        {
                            for(l=i+1;l<n;l++)
                                if(a[l][j]==0)
                                    a[l][j]=1;

                                for(l=i-1;i>=0;i--)
                                    if(a[l][j]==0)
                                        a[l][j]=1;

                            for(l=j+1;l<m;l++)
                                if(a[i][l]==0)
                                    a[i][l]=1;
                            for(l=j-1;l>=0;l--)
                                if(a[i][l]==0)
                                    a[i][l]=1;
                        }
                    }

                }

            }
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    cout<<a[i][j]<<' ';
                }
                cout<<nl;
            }

        }

    }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        int a[n][m],b[n][m];bool ok=true;

        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++)
            {
                if((i==0||i==n-1)&&(j==0&&j==m-1))
                b[i][j]=2;
                else if(i==0||i==n-1||j==0||j==m-1)
                b[i][j]=3;
                else b[i][j]=4;
                if(a[i][j]>b[i][j])ok=false;
            }
        }

        if(!ok)cout<<"NO\n";
        else {
            cout<<"YES\n";

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


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


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        int a[n][m],b[n][m];bool ok=true;

        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++)
            {
                if((i==0&&(j==0||j==n-1))&&(i==n-1&&(j==0||j==n-1)))
                b[i][j]=2;
                else if(i==0||i==n-1||j==0||j==n-1)
                b[i][j]=3;
                else b[i][j]=4;

                if(a[i][j]>b[i][j])ok=false;
            }
        }

        if(!ok)cout<<"NO\n";
        else {
            cout<<"YES\n";

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


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        int a[n][m],b[n][m];bool ok=true;

        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++)
            {
                if((i==0&&(j==0||j==n-1))&&(i==n-1&&(j==0||j==n-1)))
                b[i][j]=2;
                else if(i==0||i==n-1||j==0||j==n-1)
                b[i][j]=3;
                else b[i][j]=4;

                if(a[i][j]>b[i][j])ok=false;
            }
        }

        if(!ok)cout<<"NO\n";
        else {
            cout<<"YES\n";

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


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

using namespace std;

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

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

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

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

}

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

}

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

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

}

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

    return 0;
}

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


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

using namespace std;

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

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

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

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

}

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

}

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

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

}

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

    return 0;
}

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


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

using namespace std;

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

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

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

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

}

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

}

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

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

}

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

    return 0;
}

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