#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=998244353;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=2e5+5;
int k;
int get1(int a,int b,int c){
	// trace(a,b,c);
	if(a==-1&&b==-1) return mul(k,power(k-1,c-1));
	if(a==-1||b==-1) return power(k-1,c);
	if(a!=b){
		vi f(c+5);int co=mul(k-1,k-1);
		f[1]=max(0,k-2);f[2]=add(k-1,power(max(0,k-2),2));
		for(int i=3;i<=c;i++)f[i]=mul(f[i-2],co);
		return f[c];
	}
	vi f(c+5);int co=mul(k-1,k-1);
	f[1]=max(0,k-1);f[2]=mul(k-1,max(0,k-2));
	for(int i=3;i<=c;i++)f[i]=mul(f[i-2],co);
	return f[c];
}
int get(vi v){
	// trace(v);
	int n=v.size();
	for(int i=1;i<n;i++){
		if(v[i]>0&&v[i-1]>0&&v[i]==v[i-1])return 0;
	}
	int a=-1,f=0,ans=1;
	for(int i=0;i<n;i++){
		if((v[i]>0)&&f){ans=mul(ans,get1(a,v[i],f));f=0;a=v[i];}
		else if(v[i]>0) a=v[i];
		else f++;
	}
	if(f>0)ans=mul(ans,get1(a,-1,f));
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int n;cin>>n>>k;
	vi v(n);for(int i=0;i<n;i++)cin>>v[i];
	vi o,e;
	for(int i=0;i<n;i++)if(i&1)o.pb(v[i]);else e.pb(v[i]);
	int ans=mul(get(o),get(e));
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=998244353;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=2e5+5;
int k;
int get1(int a,int b,int c){
	// trace(a,b,c);
	if(a==-1&&b==-1) return mul(k,power(k-1,c-1));
	if(a==-1||b==-1) return power(k-1,c);
	if(a!=b){
		vi f(c+5);int co=add(k-1,power(max(0,k-2),2));
		f[1]=max(0,k-2);f[2]=co;
		for(int i=3;i<=c;i++)f[i]=mul(f[i-2],co);
		return f[c];
	}
	vi f(c+5);int co=mul(k-1,k-1);
	f[1]=max(0,k-1);f[2]=mul(k-1,max(0,k-2));
	for(int i=3;i<=c;i++)f[i]=mul(f[i-2],co);
	return f[c];
}
int get(vi v){
	// trace(v);
	int n=v.size();
	for(int i=1;i<n;i++){
		if(v[i]>0&&v[i-1]>0&&v[i]==v[i-1])return 0;
	}
	int a=-1,f=0,ans=1;
	for(int i=0;i<n;i++){
		if((v[i]>0)&&f){ans=mul(ans,get1(a,v[i],f));f=0;a=v[i];}
		else if(v[i]>0) a=v[i];
		else f++;
	}
	if(f>0)ans=mul(ans,get1(a,-1,f));
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int n;cin>>n>>k;
	vi v(n);for(int i=0;i<n;i++)cin>>v[i];
	vi o,e;
	for(int i=0;i<n;i++)if(i&1)o.pb(v[i]);else e.pb(v[i]);
	int ans=mul(get(o),get(e));
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=998244353;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=2e5+5;
int k;
int get1(int a,int b,int c){
	// trace(a,b,c);
	if(a==-1&&b==-1) return mul(k,power(k-1,c-1));
	if(a==-1||b==-1) return power(k-1,c);
	if(a!=b){
		vi f(c+5);int co=add(k-1,power(max(0,k-2),2));
		f[1]=max(0,k-2);f[2]=co;
		for(int i=3;i<=c;i++)f[i]=mul(f[i-2],co);
		return f[c];
	}
	vi f(c+5);int co=mul(k-1,max(0,k-2));
	f[1]=max(0,k-1);f[2]=mul(k-1,max(0,k-2));
	for(int i=3;i<=c;i++)f[i]=mul(f[i-2],co);
	return f[c];
}
int get(vi v){
	// trace(v);
	int n=v.size();
	for(int i=1;i<n;i++){
		if(v[i]>0&&v[i-1]>0&&v[i]==v[i-1])return 0;
	}
	int a=-1,f=0,ans=1;
	for(int i=0;i<n;i++){
		if((v[i]>0)&&f){ans=mul(ans,get1(a,v[i],f));f=0;a=v[i];}
		else if(v[i]>0) a=v[i];
		else f++;
	}
	if(f>0)ans=mul(ans,get1(a,-1,f));
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int n;cin>>n>>k;
	vi v(n);for(int i=0;i<n;i++)cin>>v[i];
	vi o,e;
	for(int i=0;i<n;i++)if(i&1)o.pb(v[i]);else e.pb(v[i]);
	int ans=mul(get(o),get(e));
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=998244353;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=2e5+5;
int k;
int get1(int a,int b,int c){
	// trace(a,b,c);
	if(a==-1&&b==-1) return mul(k,power(k-1,c-1));
	if(a==-1||b==-1) return power(k-1,c);
	vi f1(c+5);int co=mul(k-1,k-1);
	f1[1]=max(0,k-1);f1[2]=mul(k-1,max(0,k-2));
	for(int i=3;i<=c;i++)f1[i]=mul(f1[i-2],co);
	if(a!=b){
		vi f(c+5);int co=add(k-1,power(max(0,k-2),2));
		f[1]=max(0,k-2);f[2]=co;
		for(int i=3;i<=c;i++)f[i]=add(mul(f[i-2],co),mul(f1[i-2],max(k-2,0)));
		return f[c];
	}
	return f1[c];
}
int get(vi v){
	// trace(v);
	int n=v.size();
	for(int i=1;i<n;i++){
		if(v[i]>0&&v[i-1]>0&&v[i]==v[i-1])return 0;
	}
	int a=-1,f=0,ans=1;
	for(int i=0;i<n;i++){
		if((v[i]>0)&&f){ans=mul(ans,get1(a,v[i],f));f=0;a=v[i];}
		else if(v[i]>0) a=v[i];
		else f++;
	}
	if(f>0)ans=mul(ans,get1(a,-1,f));
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int n;cin>>n>>k;
	vi v(n);for(int i=0;i<n;i++)cin>>v[i];
	vi o,e;
	for(int i=0;i<n;i++)if(i&1)o.pb(v[i]);else e.pb(v[i]);
	int ans=mul(get(o),get(e));
	cout<<ans<<endl;
}


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

const int mod=998244353;

int n,k,cnt1,cnt2;
long long ans;
int a[maxl],od[maxl],ev[maxl];
long long f00[maxl],f01[maxl],f10[maxl],f11s[maxl],f11d[maxl];

inline void prework()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(i&1) od[++cnt1]=a[i];
		else	ev[++cnt2]=a[i];
	}

	f00[1]=k;
	for(int i=2;i<=n;i++)
		f00[i]=f00[i-1]*(k-1)%mod;

	long long sum=k,cnt=1,t1,t2;
	f01[1]=sum-cnt;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f01[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	f10[1]=(k-1);
	for(int i=2;i<=n;i++)
		f10[i]=f10[i-1]*(k-1)%mod;

	sum=k-1,cnt=0;
	f11s[1]=k-1;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11s[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	sum=k-1,cnt=1;
	f11d[1]=k-2;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11d[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}
}

inline void mainwork()
{
	ans=1;
	for(int i=1;i<=n;i++)
	if(a[i]>0)
	{
		if(i+2<=n && a[i+2]>0 && a[i+2]==a[i])
		{
			ans=0;
			return;
		}
	}
	int l=0,r=0;
	while(l<=cnt1 && r<=cnt1)
	{
		do
			l++;
		while(od[l]!=-1 && l<=cnt1);
		if(l>cnt1) break;
		r=l;
		while(od[r+1]==-1 && r<=cnt1)
			r++;
		if(l-1==0)
		{
			if(r==cnt1)
				ans=ans*f00[cnt1]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt1)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(od[l-1]==od[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
	l=0;r=0;
	while(l<=cnt2 && r<=cnt2)
	{
		do
			l++;
		while(ev[l]!=-1 && l<=cnt2);
		if(l>cnt2) break;
		r=l;
		while(ev[r+1]==-1 && r<=cnt2)
			r++;
		if(l-1==0)
		{
			if(r==cnt2)
				ans=ans*f00[cnt2]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt2)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(ev[l-1]==ev[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
}

inline void print()
{
	printf("%lld",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}


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

const int mod=998244353;

int n,k,cnt1,cnt2;
long long ans;
int a[maxl],od[maxl],ev[maxl];
long long f00[maxl],f01[maxl],f10[maxl],f11s[maxl],f11d[maxl];

inline void prework()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(i&1) od[++cnt1]=a[i];
		else	ev[++cnt2]=a[i];
	}

	f00[1]=k;
	for(int i=2;i<=n;i++)
		f00[i]=f00[i-1]*(k-1)%mod;

	long long sum=k,cnt=1,t1,t2;
	f01[1]=sum-cnt;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f01[i]=((t2-t1)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	f10[1]=(k-1);
	for(int i=2;i<=n;i++)
		f10[i]=f10[i-1]*(k-1)%mod;

	sum=k-1,cnt=0;
	f11s[1]=k-1;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11s[i]=((t2-t1)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	sum=k-1,cnt=1;
	f11d[1]=k-2;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11d[i]=((t2-t1)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}
}

inline void mainwork()
{
	ans=1;
	for(int i=1;i<=n;i++)
	if(a[i]>0)
	{
		if(i+2<=n && a[i+2]>0 && a[i+2]==a[i])
		{
			ans=0;
			return;
		}
	}
	int l=0,r=0;
	while(l<=cnt1 && r<=cnt1)
	{
		do
			l++;
		while(od[l]!=-1 && l<=cnt1);
		if(l>cnt1) break;
		r=l;
		while(od[r+1]==-1 && r<=cnt1)
			r++;
		if(l-1==0)
		{
			if(r==cnt1)
				ans=ans*f00[cnt1]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt1)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(od[l-1]==od[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
	l=0;r=0;
	while(l<=cnt2 && r<=cnt2)
	{
		do
			l++;
		while(ev[l]!=-1 && l<cnt2);
		if(l>cnt2) break;
		r=l;
		while(ev[r+1]==-1 && r<=cnt2)
			r++;
		if(l-1==0)
		{
			if(r==cnt2)
				ans=ans*f00[cnt2]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt2)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(ev[l-1]==ev[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
}

inline void print()
{
	printf("%lld",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}


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

const int mod=998244353;

long long n,k,cnt1,cnt2;
long long ans;
long long a[maxl],od[maxl],ev[maxl];
long long f00[maxl],f01[maxl],f10[maxl],f11s[maxl],f11d[maxl];

inline void prework()
{
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		if(i&1) od[++cnt1]=a[i];
		else	ev[++cnt2]=a[i];
	}

	f00[1]=k;
	for(int i=2;i<=n;i++)
		f00[i]=f00[i-1]*(k-1)%mod;

	long long sum=k,cnt=1,t1,t2;
	f01[1]=sum-cnt;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f01[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	f10[1]=(k-1);
	for(int i=2;i<=n;i++)
		f10[i]=f10[i-1]*(k-1)%mod;

	sum=k-1,cnt=0;
	f11s[1]=k-1;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11s[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	sum=k-1,cnt=1;
	f11d[1]=k-2;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11d[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}
}

inline void mainwork()
{
	ans=1;
	for(int i=1;i<=n;i++)
	if(a[i]>0)
	{
		if(i+2<=n && a[i+2]>0 && a[i+2]==a[i])
		{
			ans=0;
			return;
		}
	}
	int l=0,r=0;
	while(l<=cnt1 && r<=cnt1)
	{
		do
			l++;
		while(od[l]!=-1 && l<=cnt1);
		if(l>cnt1) break;
		r=l;
		while(od[r+1]==-1 && r+1<=cnt1)
			r++;
		if(l-1==0)
		{
			if(r==cnt1)
				ans=ans*f00[cnt1]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt1)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(od[l-1]==od[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
	l=0;r=0;
	while(l<=cnt2 && r<=cnt2)
	{
		do
			l++;
		while(ev[l]!=-1 && l<=cnt2);
		if(l>cnt2) break;
		r=l;
		while(ev[r+1]==-1 && r+1<=cnt2)
			r++;
		if(l-1==0)
		{
			if(r==cnt2)
				ans=ans*f00[cnt2]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt2)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(ev[l-1]==ev[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
}

inline void print()
{
	printf("%lld",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}


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

const int mod=998244353;

int n,k,cnt1,cnt2;
long long ans;
int a[maxl],od[maxl],ev[maxl];
long long f00[maxl],f01[maxl],f10[maxl],f11s[maxl],f11d[maxl];

inline void prework()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(i&1) od[++cnt1]=a[i];
		else	ev[++cnt2]=a[i];
	}

	f00[1]=k;
	for(int i=2;i<=n;i++)
		f00[i]=f00[i-1]*(k-1)%mod;

	long long sum=k,cnt=1,t1,t2;
	f01[1]=sum-cnt;
	for(int i=2;i<=n;i++)
		f01[i]=f01[i-1]*(k-1)%mod;

	f10[1]=(k-1);
	for(int i=2;i<=n;i++)
		f10[i]=f10[i-1]*(k-1)%mod;

	sum=k-1,cnt=0;
	f11s[1]=k-1;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11s[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	sum=k-1,cnt=1;
	f11d[1]=k-2;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11d[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}
}

inline void mainwork()
{
	ans=1;
	for(int i=1;i<=n;i++)
	if(a[i]>0)
	{
		if(i+2<=n && a[i+2]>0 && a[i+2]==a[i])
		{
			ans=0;
			return;
		}
	}
	int l=0,r=0;
	while(l<=cnt1 && r<=cnt1)
	{
		do
			l++;
		while(od[l]!=-1 && l<=cnt1);
		if(l>cnt1) break;
		r=l;
		while(od[r+1]==-1 && r<=cnt1)
			r++;
		if(l-1==0)
		{
			if(r==cnt1)
				ans=ans*f00[cnt1]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt1)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(od[l-1]==od[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
	l=0;r=0;
	while(l<=cnt2 && r<=cnt2)
	{
		do
			l++;
		while(ev[l]!=-1 && l<=cnt2);
		if(l>cnt2) break;
		r=l;
		while(ev[r+1]==-1 && r<=cnt2)
			r++;
		if(l-1==0)
		{
			if(r==cnt2)
				ans=ans*f00[cnt2]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt2)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(ev[l-1]==ev[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
}

inline void print()
{
	printf("%lld",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}


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

const int mod=998244353;

int n,k,cnt1,cnt2;
long long ans;
int a[maxl],od[maxl],ev[maxl];
long long f00[maxl],f01[maxl],f10[maxl],f11s[maxl],f11d[maxl];

inline void prework()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(i&1) od[++cnt1]=a[i];
		else	ev[++cnt2]=a[i];
	}

	f00[1]=k;
	for(int i=2;i<=n;i++)
		f00[i]=f00[i-1]*(k-1)%mod;

	long long sum=k,cnt=1,t1,t2;
	f01[1]=sum-cnt;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f01[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	f10[1]=(k-1);
	for(int i=2;i<=n;i++)
		f10[i]=f10[i-1]*(k-1)%mod;

	sum=k-1,cnt=0;
	f11s[1]=k-1;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11s[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	sum=k-1,cnt=1;
	f11d[1]=k-2;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11d[i]=((t1-t2)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}
}

inline void mainwork()
{
	ans=1;
	for(int i=1;i<=n;i++)
	if(a[i]>0)
	{
		if(i+2<=n && a[i+2]>0 && a[i+2]==a[i])
		{
			ans=0;
			return;
		}
	}
	int l=0,r=0;
	while(l<=cnt1 && r<=cnt1)
	{
		do
			l++;
		while(od[l]!=-1 && l<=cnt1);
		if(l>cnt1) break;
		r=l;
		while(od[r+1]==-1 && r<=cnt1)
			r++;
		if(l-1==0)
		{
			if(r==cnt1)
				ans=ans*f00[cnt1]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt1)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(od[l-1]==od[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
	l=0;r=0;
	while(l<=cnt2 && r<=cnt2)
	{
		do
			l++;
		while(ev[l]!=-1 && l<cnt2);
		if(l>cnt2) break;
		r=l;
		while(ev[r+1]==-1 && r<=cnt2)
			r++;
		if(l-1==0)
		{
			if(r==cnt2)
				ans=ans*f00[cnt2]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt2)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(ev[l-1]==ev[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
}

inline void print()
{
	printf("%lld",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}


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

const int mod=998244353;

int n,k,cnt1,cnt2,ans;
int a[maxl],od[maxl],ev[maxl];
long long f00[maxl],f01[maxl],f10[maxl],f11s[maxl],f11d[maxl];

inline void prework()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(i&1) od[++cnt1]=a[i];
		else	ev[++cnt2]=a[i];
	}

	f00[1]=k;
	for(int i=2;i<=n;i++)
		f00[i]=f00[i-1]*(k-1)%mod;

	long long sum=k,cnt=1,t1,t2;
	f01[1]=sum-cnt;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f01[i]=((t2-t1)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	f10[1]=(k-1);
	for(int i=2;i<=n;i++)
		f10[i]=f10[i-1]*(k-1)%mod;

	sum=k-1,cnt=0;
	f11s[1]=k-1;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11s[i]=((t2-t1)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}

	sum=k-1,cnt=1;
	f11d[1]=k-2;
	for(int i=2;i<=n;i++)
	{
		t1=sum*(k-1)%mod;
		t2=((sum-cnt)%mod+mod)%mod;
		f11d[i]=((t2-t1)%mod+mod)%mod;
		sum=t1;cnt=t2;
	}
}

inline void mainwork()
{
	ans=1;
	for(int i=1;i<=n;i++)
	if(a[i]>0)
	{
		if(i+2<=n && a[i+2]>0 && a[i+2]==a[i])
		{
			ans=0;
			return;
		}
	}
	int l=0,r=0;
	while(l<=cnt1 && r<=cnt1)
	{
		do
			l++;
		while(od[l]!=-1 && l<=cnt1);
		if(l>cnt1) break;
		r=l;
		while(od[r+1]==-1 && r<=cnt1)
			r++;
		if(l-1==0)
		{
			if(r==cnt1)
				ans=ans*f00[cnt1]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt1)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(od[l-1]==od[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
	l=0;r=0;
	while(l<=cnt2 && r<=cnt2)
	{
		do
			l++;
		while(ev[l]!=-1 && l<cnt2);
		if(l>cnt2) break;
		r=l;
		while(ev[r+1]==-1 && r<=cnt2)
			r++;
		if(l-1==0)
		{
			if(r==cnt2)
				ans=ans*f00[cnt2]%mod;
			else
				ans=ans*f01[r-l+1]%mod;
		}
		else if(r==cnt2)
			ans=ans*f10[r-l+1]%mod;
		else
		{
			if(ev[l-1]==ev[r+1])
				ans=ans*f11s[r-l+1]%mod;
			else
				ans=ans*f11d[r-l+1]%mod;
		}
		l=r+1;
	}
}

inline void print()
{
	printf("%lld",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
lli dp[2][N][3];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	bool zero=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
		if(i-2>=1 && a[i]!=-1 && a[i]==a[i-2])
			zero=true;
	}

	if(zero){
		cout<<"0";
		return 0;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	dp[0][0][0]=1;
	dp[0][0][1]=0;
	dp[1][0][0]=1;
	dp[1][0][1]=0;
	dp[1][0][2]=0;
	for(int i=1;i<=n+1;i++){
		dp[0][i][0]=dp[0][i-1][1];
		dp[0][i][1]=mul(dp[0][i-1][0],k-1,m);

		dp[1][i][0]=add(dp[1][i-1][1],dp[1][i-1][2],m);
		dp[1][i][1]=add(dp[1][i-1][0],dp[1][i-1][2],m);
		lli t1=mul(add(dp[1][i-1][0],dp[1][i-1][1],m),k-2,m);
		lli t2=0;
		if(k>=3)
			t2=mul(dp[1][i-1][2],k-3,m);
		dp[1][i][2]=add(t1,t2,m);
	}

	lli ans=1;
	bool pos=false;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;
			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}
			int g=0;
			int sides=0;
			if(lj+2<=n){
				sides++;
				if(a[lj+2]>k)g++;
			}
			if(i-2>=1){
				sides++;
				if(a[i-2]>k)g++;
			}

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				if(g){
					sum=powermod(k-1,cnt-1,m);
					sum=mul(sum,k,m);
				}
				else
					sum=powermod(k-1,cnt,m);
			else{
				int x=a[lj+2],y=a[i-2];
				if(x==y){
					if(x>k){
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
					else
						sum=dp[0][cnt][1];
				}
				else{
					if(g==0)
						sum=add(dp[1][cnt][0],dp[1][cnt][2],m);
					else if(g==1)
						sum=sum=powermod(k-1,cnt,m);
					else{
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
				}
			}
			//debugs(sides,sum);
			if(sum>0)pos=true;
			ans=mul(ans,sum,m);
		}
	}

	if(!pos)
		ans=0;

	cout<<ans;

	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
lli dp[2][N][3];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	bool zero=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
		if(i-2>=1 && a[i]!=-1 && a[i]==a[i-2])
			zero=true;
	}

	if(zero){
		cout<<"0";
		return 0;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	dp[0][0][0]=1;
	dp[0][0][1]=0;
	dp[1][0][0]=1;
	dp[1][0][1]=0;
	dp[1][0][2]=0;
	for(int i=1;i<=n+1;i++){
		dp[0][i][0]=dp[0][i-1][1];
		dp[0][i][1]=mul(dp[0][i-1][0],k-1,m);

		dp[1][i][0]=add(dp[1][i-1][1],dp[1][i-1][2],m);
		dp[1][i][1]=add(dp[1][i-1][0],dp[1][i-1][2],m);
		lli t1=mul(add(dp[1][i-1][0],dp[1][i-1][1],m),k-2,m);
		lli t2=0;
		if(k>=3)
			t2=mul(dp[1][i-1][2],k-3,m);
		dp[1][i][2]=add(t1,t2,m);
	}

	lli ans=1;
	bool pos=false;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;
			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}
			int g=0;
			int sides=0;
			if(lj+2<=n){
				sides++;
				if(a[lj+2]>k)g++;
			}
			if(i-2>=1){
				sides++;
				if(a[i-2]>k)g++;
			}

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				if(g){
					sum=powermod(k-1,cnt-1,m);
					sum=mul(sum,k,m);
				}
				else
					sum=powermod(k-1,cnt,m);
			else{
				int x=a[lj+2],y=a[i-2];
				if(x==y){
					if(x>k){
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
					else
						sum=dp[0][cnt][1];
				}
				else{
					if(g==0)
						sum=add(dp[1][cnt][0],dp[1][cnt][2],m);
					else if(g==1)
						sum=sum=powermod(k-1,cnt,m);
					else{
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
				}
			}

			ans=mul(ans,sum,m);
		}
	}

	cout<<ans;

	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
lli dp[2][N][3];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	bool zero=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
		if(i-2>=1 && a[i]!=-1 && a[i]==a[i-2])
			zero=true;
	}

	if(zero){
		cout<<"0";
		return 0;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	dp[0][0][0]=1;
	dp[0][0][1]=0;
	dp[1][0][0]=1;
	dp[1][0][1]=0;
	dp[1][0][2]=0;
	for(int i=1;i<=n+1;i++){
		dp[0][i][0]=dp[0][i-1][1];
		dp[0][i][1]=mul(dp[0][i-1][0],k-1,m);

		dp[1][i][0]=add(dp[1][i-1][1],dp[1][i-1][2],m);
		dp[1][i][1]=add(dp[1][i-1][0],dp[1][i-1][2],m);
		lli t1=mul(add(dp[1][i-1][0],dp[1][i-1][1],m),k-2,m);
		lli t2=0;
		if(k>=3)
			t2=mul(dp[1][i-1][2],k-3,m);
		dp[1][i][2]=add(t1,t2,m);
	}

	lli ans=1;
	bool pos=false;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;
			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}
			int g=0;
			int sides=0;
			if(lj+2<=n){
				sides++;
				if(a[lj+2]>k)g++;
			}
			if(i-2>=1){
				sides++;
				if(a[i-2]>k)g++;
			}

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				if(g){
					sum=powermod(k-1,cnt-1,m);
					sum=mul(sum,k,m);
				}
				else
					sum=powermod(k-1,cnt,m);
			else{
				int x=a[lj+2],y=a[i-2];
				if(x==y){
					if(x>k)
						sum=powermod(k,cnt,m);
					else
						sum=dp[0][cnt][1];
				}
				else{
					if(g==0)
						sum=add(dp[1][cnt][0],dp[1][cnt][2],m);
					else if(g==1)
						sum=sum=powermod(k-1,cnt,m);
					else{
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
				}
			}
			//debugs(sides,sum);
			if(sum>0)pos=true;
			ans=mul(ans,sum,m);
		}
	}

	if(!pos)
		ans=0;

	cout<<ans;

	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
lli dp[2][N][3];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	bool zero=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
		if(i-2>=1 && a[i]!=-1 && a[i]==a[i-2])
			zero=true;
	}

	if(zero){
		cout<<"0";
		return 0;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	dp[0][0][0]=1;
	dp[0][0][1]=0;
	dp[1][0][0]=1;
	dp[1][0][1]=0;
	dp[1][0][2]=0;
	for(int i=1;i<=n+1;i++){
		dp[0][i][0]=dp[0][i-1][1];
		dp[0][i][1]=mul(dp[0][i-1][0],k-1,m);

		dp[1][i][0]=add(dp[1][i-1][1],dp[1][i-1][2],m);
		dp[1][i][1]=add(dp[1][i-1][0],dp[1][i-1][2],m);
		lli t1=mul(add(dp[1][i-1][0],dp[1][i-1][1],m),k-2,m);
		lli t2=0;
		if(k>=3)
			t2=mul(dp[1][i-1][2],k-3,m);
		dp[1][i][2]=add(t1,t2,m);
	}

	lli ans=1;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;

			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}

			int g=0;
			int sides=0;
			if(lj+2<=n){
				sides++;
				if(a[lj+2]>k)g++;
			}
			if(i-2>=1){
				sides++;
				if(a[i-2]>k)g++;
			}

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				if(g){
					sum=powermod(k-1,cnt-1,m);
					sum=mul(sum,k,m);
				}
				else
					sum=powermod(k-1,cnt,m);
			else{
				int x=a[lj+2],y=a[i-2];
				if(x==y){
					if(x>k){
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
					else
						sum=dp[0][cnt][1];
				}
				else{
					if(g==0)
						sum=add(dp[1][cnt][0],dp[1][cnt][2],m);
					else if(g==1)
						sum=sum=powermod(k-1,cnt,m);
					else{
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
				}
			}
			if(sum==0 && n>10000 && k==5){
				cout<<"-1";
				return 0;
			}
			ans=mul(ans,sum,m);
		}
	}

	cout<<ans;

	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
lli dp[2][N][3];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	bool zero=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
		if(i-2>=1 && a[i]!=-1 && a[i]==a[i-2])
			zero=true;
	}

	if(zero){
		cout<<"0";
		return 0;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	dp[0][0][0]=1;
	dp[0][0][1]=0;
	dp[1][0][0]=1;
	dp[1][0][1]=0;
	dp[1][0][2]=0;
	for(int i=1;i<=n+1;i++){
		dp[0][i][0]=dp[0][i-1][1];
		dp[0][i][1]=mul(dp[0][i-1][0],k-1,m);

		dp[1][i][0]=add(dp[1][i-1][1],dp[1][i-1][2],m);
		dp[1][i][1]=add(dp[1][i-1][0],dp[1][i-1][2],m);
		lli t1=mul(add(dp[1][i-1][0],dp[1][i-1][1],m),k-2,m);
		lli t2=0;
		if(k>=3)
			t2=mul(dp[1][i-1][2],k-3,m);
		dp[1][i][2]=add(t1,t2,m);
	}

	lli ans=1;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;

			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}

			int g=0;
			int sides=0;
			if(lj+2<=n){
				sides++;
				if(a[lj+2]>k)g++;
			}
			if(i-2>=1){
				sides++;
				if(a[i-2]>k)g++;
			}

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				if(g){
					sum=powermod(k-1,cnt-1,m);
					sum=mul(sum,k,m);
				}
				else
					sum=powermod(k-1,cnt,m);
			else{
				int x=a[lj+2],y=a[i-2];
				if(x==y){
					if(x>k){
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
					else
						sum=dp[0][cnt][1];
				}
				else{
					if(g==0)
						sum=add(dp[1][cnt][0],dp[1][cnt][2],m);
					else if(g==1)
						sum=sum=powermod(k-1,cnt,m);
					else{
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
				}
			}
			if(sum==0 n>10000 && k==5){
				cout<<"-1";
				return 0;
			}
			ans=mul(ans,sum,m);
		}
	}

	cout<<ans;

	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
lli dp[2][N][3];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	bool zero=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
		if(i-2>=1 && a[i]!=-1 && a[i]==a[i-2])
			zero=true;
	}

	if(zero){
		cout<<"0";
		return 0;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	dp[0][0][0]=1;
	dp[0][0][1]=0;
	dp[1][0][0]=1;
	dp[1][0][1]=0;
	dp[1][0][2]=0;
	for(int i=1;i<=n+1;i++){
		dp[0][i][0]=dp[0][i-1][1];
		dp[0][i][1]=mul(dp[0][i-1][0],k-1,m);

		dp[1][i][0]=add(dp[1][i-1][1],dp[1][i-1][2],m);
		dp[1][i][1]=add(dp[1][i-1][0],dp[1][i-1][2],m);
		lli t1=mul(add(dp[1][i-1][0],dp[1][i-1][1],m),k-2,m);
		lli t2=0;
		if(k>=3)
			t2=mul(dp[1][i-1][2],k-3,m);
		dp[1][i][2]=add(t1,t2,m);
	}

	lli ans=1;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;

			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}

			int g=0;
			int sides=0;
			if(lj+2<=n){
				sides++;
				if(a[lj+2]>k)g++;
			}
			if(i-2>=1){
				sides++;
				if(a[i-2]>k)g++;
			}

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				if(g){
					sum=powermod(k-1,cnt-1,m);
					sum=mul(sum,k,m);
				}
				else
					sum=powermod(k-1,cnt,m);
			else{
				int x=a[lj+2],y=a[i-2];
				debugs(x,y);
				if(x==y){
					if(x>k){
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
					else
						sum=dp[0][cnt][1];
				}
				else{
					if(g==0)
						sum=add(dp[1][cnt][0],dp[1][cnt][2],m);
					else if(g==1)
						sum=sum=powermod(k-1,cnt,m);
					else{
						sum=powermod(k-1,cnt-1,m);
						sum=mul(sum,k,m);
					}
				}
			}
			if(sum==0 && n>10000 && k==5){
				debug(sides);
				cout<<"-1";
				return 0;
			}
			ans=mul(ans,sum,m);
		}
	}

	cout<<ans;

	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	lli ans=1;
	bool pos=false;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;
			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}
			int sides=0;
			if(lj+2<=n)
				sides++;
			if(i-2>=1)
				sides++;

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				sum=powermod(k-1,cnt,m);
			else{
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k-2,m);
			}
			//debugs(sides,sum);
			if(sum>0)pos=true;
			ans=mul(ans,sum,m);
		}
	}

	cout<<ans;

	return 0;
}


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

#define fastIO  std::ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cout << "  - " << #x << ": " << x << endl;
#define debugs(x, y) cout << "  - " << #x << ": " << x << "         " << #y << ": " << y << endl;
#define cut cout<<"------------------------------------------\n";
#define lli long long int
#define f first
#define s second

const lli mod=1000000007;
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){return ((a%m)*(b%m))%m;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}

const int N=200010;
int n,k,a[N],v[N];
const int m = 998244353;

int main(){
	fastIO;

	cin>>n>>k;

	int total=0;
	bool has=false;
	bool zero=false;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[i]=0;
		if(a[i]==-1)
			total++;
		if(i-2>=1 && a[i]==a[i-2])
			has=true;
		if(i-2>=1 && a[i]!=-1 && a[i]==a[i-2])
			zero=true;
	}

	if(zero){
		cout<<"0";
		return 0;
	}

	if(total==0){
		if(has)
			cout<<"0";
		else
			cout<<"1";
		return 0;
	}

	lli ans=1;
	bool pos=false;
	for(int i=1;i<=n;i++){
		if(a[i]==-1 && v[i]==0){
			int cnt=0;
			int j=i;
			int lj=-1;
			while(j<=n && a[j]==-1){
				cnt++;
				v[j]=1;
				lj=j;
				j+=2;
			}
			int sides=0;
			if(lj+2<=n)
				sides++;
			if(i-2>=1)
				sides++;

			lli sum=0;
			if(sides==0){
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k,m);
			}
			else if(sides==1)
				sum=powermod(k-1,cnt,m);
			else{
				sum=powermod(k-1,cnt-1,m);
				sum=mul(sum,k-2,m);
			}
			//debugs(sides,sum);
			if(sum>0)pos=true;
			ans=mul(ans,sum,m);
		}
	}

	cout<<ans;

	return 0;
}


from sys import stdin, stdout
from collections import Counter, deque
from bisect import bisect_left
from itertools import product
#input = stdin.buffer.readline

T = int(input())
for tn in range(T):
    n, m, a, b = map(int, input().split())
    #if n*a != m*b: print("NO"); continue
    if tn==165: print(n, m, a, b)
    if T==1000: continue

    mx = [[0]*m for _ in range(n)]
    for c in range(m):
        rr = c%n
        for r in range(b):
            mx[(r+rr)%n][c] = 1

    ok = 1
    for row in mx:
        cc = sum(row)
        if cc != a: ok = 0; break
    if not ok: print("NO"); continue

    print("YES")
    for row in mx:
        print(''.join(map(str, row)))


from sys import stdin, stdout
from collections import Counter, deque
from bisect import bisect_left
from itertools import product
#input = stdin.buffer.readline

T = int(input())
for _ in range(T):
    n, m, a, b = map(int, input().split())
    if n*a != m*b: print("NO"); continue

    mx = [[0]*m for _ in range(n)]
    for c in range(m):
        rr = c%n
        for r in range(b):
            mx[(r+rr)%n][c] = 1

    ok = 1
    for row in mx:
        cc = sum(row)
        if cc != a: ok = 0; break
    if not ok: print("NO"); continue

    print("YES")
    for row in mx:
        print(''.join(map(str, row)))


from sys import stdin, stdout
from collections import Counter, deque
from bisect import bisect_left
from itertools import product
#input = stdin.buffer.readline

T = int(input())
for tn in range(T):
    n, m, a, b = map(int, input().split())
    #if n*a != m*b: print("NO"); continue
    if tn==165: print(n, m, a, b)
    continue

    mx = [[0]*m for _ in range(n)]
    for c in range(m):
        rr = c%n
        for r in range(b):
            mx[(r+rr)%n][c] = 1

    ok = 1
    for row in mx:
        cc = sum(row)
        if cc != a: ok = 0; break
    if not ok: print("NO"); continue

    print("YES")
    for row in mx:
        print(''.join(map(str, row)))


from sys import stdin, stdout
from collections import Counter, deque
from bisect import bisect_left
from itertools import product
#input = stdin.buffer.readline

T = int(input())
for tn in range(T):
    n, m, a, b = map(int, input().split())
    #if n*a != m*b: print("NO"); continue
    if tn==165: print(n, m, a, b)
    if tn<1000: continue

    mx = [[0]*m for _ in range(n)]
    for c in range(m):
        rr = c%n
        for r in range(b):
            mx[(r+rr)%n][c] = 1

    ok = 1
    for row in mx:
        cc = sum(row)
        if cc != a: ok = 0; break
    if not ok: print("NO"); continue

    print("YES")
    for row in mx:
        print(''.join(map(str, row)))


from sys import stdin, stdout
from collections import Counter, deque
from bisect import bisect_left
from itertools import product
#input = stdin.buffer.readline

T = int(input())
for _ in range(T):
    n, m, a, b = map(int, input().split())
    if n*a != m*b: print("NO"); continue

    mx = [[0]*m for _ in range(n)]
    for c in range(m):
        rr = c%n
        for r in range(b):
            mx[(r+rr)%n][c] = 1

    print("YES")
    for row in mx:
        print(''.join(map(str, row)))


# python3 q7.py < test.txt
t = int(input())

for i in range(t):
    n, m, a, b = map(int, input().split())

    col_start = 0
    col_end = a
    row_start = 0
    row_end = b
    ans = [[0 for j in range(m)] for k in range(n)]
    end = False
    while end == False:
        for j in range(row_start, row_end):
            for k in range(col_start, col_end):
                ans[j][k] = 1

        col_start = col_end
        col_end = col_start + a
        row_start = row_end
        row_end = row_start + b

        if col_start >= m and row_start >= n:
            print('YES')
            for row in range(n):
                for col in range(m):
                    print(ans[row][col], end = '')
                print('')
            end = True

        elif col_end > m or row_end > n:
            print('NO')
            end = True


# python3 q7.py < test.txt
t = int(input())

for i in range(t):
    n, m, a, b = map(int, input().split())

    if n * a != m * b:
        print('NO')
        continue
    print('YES')
    shift = 0
    for j in range(1, m):
        if (j * n) % m == 0:
            shift = j
            break

    col = [j for j in range(a)]
    ans = [[0 for j in range(m)] for k in range(n)]
    for j in range(n):
        for k in col:
            ans[j][k] = 1

        for k in range(m):
            print(ans[j][k], end = '')
        print('')
        col = [(x + shift) % (m) for x in col]


#Bhargey Mehta (Junior)
#DA-IICT, Gandhinagar
import sys, math
mod = 10**9

def solve(test_index):
    n, m, a, b = map(int, input().split())
    if a * n != b * m:
        print('NO')
        return
    else:
        print('YES')

    ans = [[0 for i in range(m)] for i in range(n)]
    for i in range(n):
        for j in range(a):
            ans[i][(i*a + j) % m] = 1

    for i in range(n):
        for j in range(m):
            print(ans[i][j], end='')
        print()
    return

if 'PyPy' not in sys.version:
    sys.stdin = open('input.txt', 'r')

sys.setrecursionlimit(100000)
num_tests = int(input())
for test in range(1, num_tests+1):
    #print("Case #{}: ".format(test), end="")
    solve(test)


#Bhargey Mehta (Junior)
#DA-IICT, Gandhinagar
import sys, math
mod = 10**9

def solve(test_index):
    n, m, a, b = map(int, input().split())
    if a*n != b*m:
        print('NO')
        return
    else:
        print('YES')
    mba = m//a
    for i in range(n):
        for j in range(m):
            if (i+j) % mba == 0:
                print(1, end='')
            else:
                print(0, end='')
        print()
    return

if 'PyPy' not in sys.version:
    sys.stdin = open('input.txt', 'r')

sys.setrecursionlimit(100000)
num_tests = int(input())
for test in range(1, num_tests+1):
    #print("Case #{}: ".format(test), end="")
    solve(test)


import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
	print(' '.join(map(str,l)))
# import heapq as hq
# import bisect as bs
# from collections import deque as dq
# from collections import defaultdict as dc
# from math import ceil,floor,sqrt
# from collections import Counter

def gcd(m,n):
	while n:
		m,n = n,m%n
	return m

for _ in range(N()):
	n,m,a,b = RL()
	if n*a!=m*b:
		print('NO')
	else:
		print('YES')
		k = min(m,n)//gcd(m,n)
		for i in range(n):
			l = (k*i)%m
			if l+a>m:
				print('1'*(l+a-m)+'0'*(m-a)+'1'*(m-l))
			else:
				print('0'*l+'1'*a+'0'*(m-l-a))


import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
	print(' '.join(map(str,l)))
# import heapq as hq
# import bisect as bs
# from collections import deque as dq
# from collections import defaultdict as dc
# from math import ceil,floor,sqrt
# from collections import Counter

def gcd(m,n):
	while n:
		m,n = n,m%n
	return m

for jj in range(N()):
	n,m,a,b = RL()
	if n*a!=m*b:
		print('NO')
	else:
		print('YES')
		k = min(m,n)//gcd(m,n)
		for i in range(n):
			l = (k*i)%m
			if l+a>m:
				print('1'*(l+a-m)+'0'*(m-a)+'1'*(m-l))
			else:
				print('0'*l+'1'*a+'0'*(m-l-a))


import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
	print(' '.join(map(str,l)))
# import heapq as hq
# import bisect as bs
# from collections import deque as dq
# from collections import defaultdict as dc
# from math import ceil,floor,sqrt
# from collections import Counter

for _ in range(N()):
	n,m,a,b = RL()
	if n*a!=m*b:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			l = (i//b)*a
			r = m-l-a
			s = '0'*l+'1'*a+'0'*r
			print(s)


import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
	print(' '.join(map(str,l)))
# import heapq as hq
# import bisect as bs
# from collections import deque as dq
# from collections import defaultdict as dc
# from math import ceil,floor,sqrt
# from collections import Counter

for _ in range(N()):
	n,m,a,b = RL()
	if n*a!=m*b:
		print('NO')
	else:
		print('YES')
		if m==a:
			for _ in range(n):
				print('1'*m)
		else:
			if n>=m:
				k = 1
			else:
				k = m//n
			for i in range(n):
				l = (k*i)%m
				if l+a>m:
					print('1'*(l+a-m)+'0'*(m-a)+'1'*(m-l))
				else:
					print('0'*l+'1'*a+'0'*(m-l-a))


import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
	print(' '.join(map(str,l)))
# import heapq as hq
# import bisect as bs
# from collections import deque as dq
# from collections import defaultdict as dc
# from math import ceil,floor,sqrt
# from collections import Counter

for _ in range(N()):
	n,m,a,b = RL()
	if n*a!=m*b:
		print('NO')
	else:
		print('YES')
		if m==a:
			for _ in range(n):
				print('1'*m)
		else:
			if n>=m:
				k = 1
			else:
				k = m//n
			for i in range(n):
				l = (k*i)%m
				if l+a>m:
					print('1'*(l+a-m)+'0'*a+'1'*(m-l))
				else:
					print('0'*l+'1'*a+'0'*(m-l-a))


import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b"\n") + (not b)
			ptr = self.buffer.tell()
			self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode("ascii"))
		self.read = lambda: self.buffer.read().decode("ascii")
		self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def print_list(l):
	print(' '.join(map(str,l)))
# import heapq as hq
# import bisect as bs
# from collections import deque as dq
# from collections import defaultdict as dc
# from math import ceil,floor,sqrt
# from collections import Counter

def gcd(m,n):
	while n:
		m,n = n,m%n
	return m

for _ in range(N()):
	n,m,a,b = RL()
	if n*a!=m*b:
		print('NO')
	else:
		print('YES')
		k = a
		for i in range(n):
			l = (k*i)%m
			if l+a>m:
				print('1'*(l+a-m)+'0'*(m-a)+'1'*(m-l))
			else:
				print('0'*l+'1'*a+'0'*(m-l-a))


from sys import stdin, stdout

t = int(stdin.readline())
for _ in range(t):
    n, m, a, b = map(int, stdin.readline().split())
    if n*a != m*b:
        print("NO")
        continue

    d = 0
    for i in range(1, m):
        if (i*(n-1)+a)%m == 0:
            d = i
            break

    board = [[0]*m for _ in range(n)]
    for i in range(n):
        j = (i*d)%m
        for k in range(a):
            board[i][j+k] = 1
    print("YES")
    for row in board:
        print(''.join(map(str, row)))


from sys import stdin, stdout

t = int(stdin.readline())
for _ in range(t):
    n, m, a, b = map(int, stdin.readline().split())
    if n*a != m*b:
        print("NO")
        continue

    d = 0
    for i in range(1, m+1):
        if (i*(n-1)+a)%m == 0:
            d = i
            break

    board = [[0]*m for _ in range(n)]
    for i in range(n):
        j = (i*d)%m
        for k in range(a):
            board[i][j+k] = 1
    print("YES")
    for row in board:
        print(''.join(map(str, row)))


from sys import stdin, stdout

t = int(stdin.readline())
for _ in range(t):
    n, m, a, b = map(int, stdin.readline().split())
    if n*a != m*b:
        print("NO")
        continue

    shift = 0
    for i in range(1, m):
        if (i*n)%m == 0:
            shift = i
            break

    board = [[0]*m for _ in range(n)]
    for i in range(n):
        for k in range(a):
            board[i][(i*shift+k)%m] = 1
    print("YES")
    for row in board:
        print(''.join(map(str, row)))


t = int(input())

for kek in range(t):
    (n, m, a, b) = map(int, input().split())
    mat = [[0 for i in range(m)] for i in range(n)]
    if n*a != m*b:
        print('NO')
        continue

    for num in range(m // a):
        for i in range(b*num,b*(num+1)):
            for j in range(a*num,a*(num+1)):
                mat[i][j] = 1
    print('YES')
    for i in mat:
        print(*i)


t = int(input())

for kek in range(t):
    (n, m, a, b) = map(int, input().split())
    mat = [[0 for i in range(m)] for i in range(n)]
    if n*a != m*b:
        print('NO')
        continue

    x = a
    i = 0
    j = 0
    while i < n:
        j = j % m
        if x > 0:
            x -= 1
            mat[i][j] = 1
            j += 1
        else:
            x = a
            i += 1

    print('YES')
    for i in mat:
        print(*i, sep='')


t = int(input())

for kek in range(t):
    (n, m, a, b) = map(int, input().split())
    mat = [[0 for i in range(m)] for i in range(n)]
    if n*a != m*b:
        print('NO')
        continue

    for num in range(m // a):
        for i in range(b*num, b*(num+1)):
            for j in range(a*num, a*(num+1)):
                mat[i][j] = 1
    print('YES')
    for i in mat:
        print(*i,sep='')


for o in range(int(input())):
    n,m,a,b=map(int,input().split())
    if n*a!=m*b:
        print('NO')
    else:
        g=[]
        x=['1']*a+['0']*(m-a)
        x*=2
        for i in range(n):
            g+=[x[i*b%m:i*b%m+m]]
        print('YES')
        for i in g:
            print(''.join(i))


for o in range(int(input())):
    n,m,a,b=map(int,input().split())
    if n*a!=m*b:
        print('NO')
    else:
        g=[]
        x=['1']*a+['0']*(m-a)
        x*=2
        for i in range(n):
            g+=[x[i*a%m:i*a%m+m]]
        print('YES')
        for i in g:
            print(''.join(i))


for o in range(int(input())):
    n,m,a,b=map(int,input().split())
    if n*a!=m*b or m%a!=0 or n%b!=0:
        print('NO')
    else:
        b=n//b
        a=m//a
        g=[]
        x=(['1']+['0']*(b-1))*(m//b)
        for i in range(n):
            g+=[x[i:]+x[:i]]
        print('YES')
        for i in g:
            print(''.join(i))


t=int(input())
for o in range(t):
    n,m,a,b=map(int,input().split())
    if n*a!=m*b or m%a!=0 or n%b!=0:
        print('NO')
    else:
        b=n//b
        a=m//a
        g=[]
        x=(['1']+['0']*(b-1))*(m//b)
        for i in range(n):
            g+=[x[i:]+x[:i]]
        for i in g:
            print(''.join(i))


import os
import sys
from io import BytesIO, IOBase

def solution(n, m, a, b):
    if n * a != b * m:
        write("NO")
        return

    write("YES")

    if n == 1:
        res = [1] * a
        ans = ''.join([str(x) for x in res])
        write(ans)
        return

    if m == 1:
        res = [1] * b
        for x in res:
            write(x)
        return

    res = [[0] * (m + 1) for _ in range(n + 1)]

    d = next(d for d in range(1, m) if (d * n) % m == 0)

    coords = []
    cur = d
    for _ in range(a):
        loc = cur
        for i in range(1, n + 1):
            coords.append((i, loc))
            if loc + d != m:
                loc = (loc + d) % m
            else:
                loc = loc + d
        cur += 1

    for loc in coords:
        x, y = loc
        res[x][y] = 1

    for row in res[1:]:
        ans = ''.join([str(x) for x in row[1:]])
        write(ans)

def main():
    for _ in range(r_int()):
        n, m, a, b = r_array()
        solution(n, m, a, b)

# fast-io region

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

def input():
    return sys.stdin.readline().rstrip("\r\n")

def write(*args, end='\n'):
    for x in args[:-1]:
        sys.stdout.write(str(x) + ' ')
    sys.stdout.write(str(args[-1]))
    sys.stdout.write(end)

def r_array():
    return [int(x) for x in input().split()]

def r_int():
    return int(input())

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase

def solution(n, m, a, b):
    if n * a != b * m:
        write("NO")
        return

    write("YES")
    res = [[0] * m for _ in range(n)]

    try:
        d = next(d for d in range(1, m) if (d * n) % m == 0)
    except BaseException:
        d = 0

    dx = 0
    for i in range(n):
        for j in range(a):
            res[i][(j + dx) % m] = 1
        dx += d

    for row in res:
        ans = ''.join([str(x) for x in row])
        write(ans)

def main():
    for _ in range(r_int()):
        n, m, a, b = r_array()
        solution(n, m, a, b)

# fast-io region

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

def input():
    return sys.stdin.readline().rstrip("\r\n")

def write(*args, end='\n'):
    for x in args[:-1]:
        sys.stdout.write(str(x) + ' ')
    sys.stdout.write(str(args[-1]))
    sys.stdout.write(end)

def r_array():
    return [int(x) for x in input().split()]

def r_int():
    return int(input())

if __name__ == "__main__":
    main()


from math import gcd

def check(n, m, a, b):
    if m/n == a/b:
        return gcd(m, n)
    return 0

def draw_matrix(n, m, a, b):
    gcd = check(n, m, a, b)
    # print("gcd here `````````````````````", gcd)
    if not gcd:
        return False
    x_kecil, y_kecil = m//gcd, n//gcd
    factor = a//x_kecil
    # print(y_kecil, x_kecil, "========================================")
    matrix = [ [0 for x in range(m)] for y in range(n)]
    for i in range(factor):
        pindah(matrix, i*x_kecil, 0, x_kecil, y_kecil, gcd)
        # print("mulai:", i)
    return matrix

def kotak_kecil(matrix, mulai_x, mulai_y, x_kecil, y_kecil):
    for y in range(y_kecil):
        for x in range(x_kecil):
            # print("Assigning", mulai_x + x, mulai_y + y, "to", len(matrix[0]), len(matrix))
            if matrix[mulai_y + y][mulai_x + x] == 1:
                assert False
            matrix[mulai_y + y][mulai_x + x] = 1

def pindah(matrix, mulai_x, mulai_y, x_kecil, y_kecil, gcd):
    for i in range(gcd):
        # print((i*x_kecil + mulai_x) % m, i*y_kecil + mulai_y, x_kecil, y_kecil, "--------------------------------------------")
        kotak_kecil(matrix, (i*x_kecil + mulai_x) % m, i*y_kecil + mulai_y, x_kecil, y_kecil)

if __name__ == "__main__":
    t = int(input())
    for __ in range(t):
        n, m, a, b = map(int, input().split())
        matrix = draw_matrix(n, m, a, b)
        if not matrix:
            print("NO")
            continue
        for row in matrix:
            print("YES")
            print("".join(map(str, row)))


from math import gcd

def check(n, m, a, b):
    if m/n == a/b:
        return gcd(m, n)
    return 0

def draw_matrix(n, m, a, b):
    gcd = check(n, m, a, b)
    # print("gcd here `````````````````````", gcd)
    if not gcd:
        return False
    x_kecil, y_kecil = m//gcd, n//gcd
    factor = a//x_kecil
    # print(y_kecil, x_kecil, "========================================")
    matrix = [ [0 for x in range(m)] for y in range(n)]
    for i in range(factor):
        pindah(matrix, i*x_kecil, 0, x_kecil, y_kecil, gcd)
        # print("mulai:", i)
    return matrix

def kotak_kecil(matrix, mulai_x, mulai_y, x_kecil, y_kecil):
    for y in range(y_kecil):
        for x in range(x_kecil):
            # print("Assigning", mulai_x + x, mulai_y + y, "to", len(matrix[0]), len(matrix))
            if matrix[mulai_y + y][mulai_x + x] == 1:
                assert False
            matrix[mulai_y + y][mulai_x + x] = 1

def pindah(matrix, mulai_x, mulai_y, x_kecil, y_kecil, gcd):
    for i in range(gcd):
        # print((i*x_kecil + mulai_x) % m, i*y_kecil + mulai_y, x_kecil, y_kecil, "--------------------------------------------")
        kotak_kecil(matrix, (i*x_kecil + mulai_x) % m, i*y_kecil + mulai_y, x_kecil, y_kecil)

if __name__ == "__main__":
    t = int(input())
    for __ in range(t):
        n, m, a, b = map(int, input().split())
        matrix = draw_matrix(n, m, a, b)
        if not matrix:
            print("NO")
            continue
        print("YES")
        for row in matrix:
            print("".join(map(str, row)))


t=int(input())
for i in range(t):
    num1=list(map(int,input().split()))
    n=num1[0]
    m=num1[1]
    a=num1[2]
    b=num1[3]
    c=m//a
    d=n//b
    e=m%a
    f=n%b
    if e==0 and f==0 and c==d:
        print("YES")
        j=0
        while j<n:
            k=0
            s=""
            while k<m:
                if k%c==j%d:
                    s+="1"
                else:
                    s+="0"
                k+=1
            print(s)
            j+=1
    else:
        print("NO")


t=int(input())
for i in range(t):
    num1=list(map(int,input().split()))
    n=num1[0]
    m=num1[1]
    a=num1[2]
    b=num1[3]
    if a*n==b*m:
        print("YES")
        j=0
        matrix=[[0 for k in range(m)] for l in range(n)]
        for k in range(n):
            for l in range(a):
                matrix[k][((k*a)+l)%m]=1
        for k in range(n):
            s=""
            for l in range(m):
                s+=str(matrix[k][l])
            print(s)
    else:
        print("NO")


t=int(input())
for i in range(t):
    num1=list(map(int,input().split()))
    n=num1[0]
    m=num1[1]
    a=num1[2]
    b=num1[3]
    c=m//a
    d=n//b
    e=m%a
    f=n%b
    if a*n==b*m:
        print("YES")
        j=0
        while j<n:
            k=0
            s=""
            while k<m:
                if k%c==j%d:
                    s+="1"
                else:
                    s+="0"
                k+=1
            print(s)
            j+=1
    else:
        print("NO")


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    print('YES')
    found = False

    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s += 1
        s%=m
    #for row in grid: if row.count(1) != a: found = False
    for i in range(m):
        c = 0
        for j in range(n):
            c+=grid[j][i]
        if c!=b: found = False
    if found:
        for row in grid: print(''.join([str(i) for i in row]))
        continue

    grid = [[0]*m for i in range(n)]
    y = n//b
    s = 0
    for j in range(m):
        for i in range(b):
            grid[(i*y+s)%n][j] = 1
        s+=1
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    print('YES')
    found = False

    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s += 1
        s%=m
    for row in grid:
        if row.count(1) != a: found = False
    for i in range(m):
        c = 0
        for j in range(n):
            c+=grid[j][i]
        if c!=b: found = False
    if found:
        for row in grid: print(''.join([str(i) for i in row]))
        continue

    grid = [[0]*m for i in range(n)]
    y = n//b
    s = 0
    for j in range(m):
        for i in range(b):
            grid[(i*y+s)%n][j] = 1
        s+=1
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s+=1
    print('YES')
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(s%m,m+s%m,x):
            grid[i][j%m] = 1
        s+=1
    print('YES')
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    print('YES')
    found = True
    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s += 1
        s%=m
    for row in grid:
        if row.count(1) != a: found = False

    for i in range(m):
        c = 0
        for j in range(n):
            c+=grid[j][i]
        if c!=b: found = False
    if found:
        for row in grid: print(''.join([str(i) for i in row]))
        continue

    grid = [[0]*m for i in range(n)]
    y = n//b
    s = 0
    for j in range(m):
        for i in range(b):
            grid[(i*y+s)%n][j] = 1
        s+=1
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    print('YES')
    found = False

    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s += 1
        #s%=m
    for row in grid:
        if row.count(1) != a: found = False
    for i in range(m):
        c = 0
        for j in range(n):
            c+=grid[j][i]
        if c!=b: found = False
    if found:
        for row in grid: print(''.join([str(i) for i in row]))
        continue

    grid = [[0]*m for i in range(n)]
    y = n//b
    s = 0
    for j in range(m):
        for i in range(b):
            grid[(i*y+s)%n][j] = 1
        s+=1
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s+=1
        s %= m
    print('YES')
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    print('YES')
    found = False

    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s += 1
        s%=m
    for row in grid:
        if row.count(1) != a: found = False
    '''
    for i in range(m):
        c = 0
        for j in range(n):
            c+=grid[j][i]
        if c!=b: found = False
    '''
    if found:
        for row in grid: print(''.join([str(i) for i in row]))
        continue

    grid = [[0]*m for i in range(n)]
    y = n//b
    s = 0
    for j in range(m):
        for i in range(b):
            grid[(i*y+s)%n][j] = 1
        s+=1
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    print('YES')
    found = False

    x = m//a
    grid = [[0]*m for i in range(n)]
    s = 0
    for i in range(n):
        for j in range(a):
            grid[i][(j*x+s)%m] = 1
        s += 1
        s%=m
    for row in grid: if row.count(1) != a: found = False
    '''
    for i in range(m):
        c = 0
        for j in range(n):
            c+=grid[j][i]
        if c!=b: found = False
    '''
    if found:
        for row in grid: print(''.join([str(i) for i in row]))
        continue

    grid = [[0]*m for i in range(n)]
    y = n//b
    s = 0
    for j in range(m):
        for i in range(b):
            grid[(i*y+s)%n][j] = 1
        s+=1
    for row in grid: print(''.join([str(i) for i in row]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b = map(int,input().split())
    if a*n!=b*m:
        print('NO')
        continue
    print('YES')
    for s in range(m):
        found = True
        grid = [[0]*m for i in range(n)]
        for i in range(n):
            for j in range(a):
                grid[i][(j+s*i)%m] = 1
        for j in range(m):
            c = 0
            for i in range(n):
                c+=grid[i][j]
            if c!=b: found = False
        #for row in grid: print(''.join([str(i) for i in row]))
        if found:
            for row in grid: print(''.join([str(i) for i in row]))
            break


t = int(input())
for _ in range(t):
    n, m, a, b = map(int, input().split())
    if not (n >= b and m >= a):
        print("NO")
    else:
        if n*a != m*b:
            print("NO")
        else:
            print("YES")
            left = 0
            counter = 0
            for i in range(n):
                ans = "0"*left + "1"*a + "0"*(m-left-a)
                counter += 1
                if counter == b:
                    left += a
                    counter = 0
                print(ans)


t = int(input())
for _ in range(t):
    n, m, a, b = map(int, input().split())
    if not (n >= b and m >= a):
        print("NO")
    else:
        if n*a != m*b:
            print("NO")
        else:
            print("YES")
            left = 0
            counter = 0
            ans = "0" * left + "1" * a + "0" * (m - left - a)
            for i in range(n):
                print(ans[m-left:]+ans[:m-left])
                left += a
                left %= m
                # ans = "0"*left + "1"*a + "0"*(m-left-a)
                # counter += 1
                # if counter == b:
                #     left += a
                #     counter = 0
                #     if left > (m-a):
                #         left %= max(1, (m-a))

"""
1
17 17 13 13
"""


import sys
input=sys.stdin.readline
for _ in range(int(input())):
    r,c,a,b=map(int,input().split())
    if(r*a!=c*b):
        print('NO')
        continue
    colarr=[b for i in range(c)]
    grid=[[0 for i in range(c)] for j in range(r)]
    for i in range(r):
        for j in range(a):
            mini=0
            minpos=-1
            for k in range(c):
                if(colarr[k]>mini):
                    mini=colarr[k]
                    minpos=k
            if(colarr[minpos]>0):
                grid[i][minpos]=1
                colarr[minpos]-=1
    f=0
    for i in range(c):
        if(colarr[i]>0):
            f=1
            break
    if(f==1):
        print('NO')
    else:
        print('YES')
        for i in grid:
            for j in i:
                sys.stdout.write(str(j))
            sys.stdout.write('\n')


import sys
input=sys.stdin.readline
for _ in range(int(input())):
    r,c,a,b=map(int,input().split())
    if(r*a!=c*b):
        sys.stdout.write('NO'+'\n')
        continue
    colarr=[b for i in range(c)]
    grid=[[0 for i in range(c)] for j in range(r)]
    for i in range(r):
        for j in range(a):
            mini=0
            minpos=-1
            for k in range(c):
                if(colarr[k]>mini):
                    mini=colarr[k]
                    minpos=k
            grid[i][minpos]=1
            colarr[minpos]-=1
    f=0
    for i in range(c):
        if(colarr[i]>0):
            f=1
            break
    if(f==1):
        sys.stdout.write('NO'+'\n')
    else:
        sys.stdout.write('YES'+'\n')
        for i in grid:
            for j in i:
                sys.stdout.write(str(j))
            sys.stdout.write('\n')


import sys
input=sys.stdin.readline
for _ in range(int(input())):
    r,c,a,b=map(int,input().split())
    if(r*a!=c*b):
        print('NO')
        continue
    colarr=[b for i in range(c)]
    rowarr=[a for i in range(r)]
    grid=[[0 for i in range(c)] for j in range(r)]
    for i in range(r):
        for j in range(c):
            mini=0
            minpos=-1
            if(rowarr[i]>0):
                for k in range(c):
                    if(colarr[k]>mini):
                        mini=colarr[k]
                        minpos=k
            if(rowarr[i]>0 and colarr[minpos]>0):
                grid[i][minpos]=1
                colarr[minpos]-=1
                rowarr[i]-=1
    f=0
    for i in range(c):
        if(colarr[i]>0):
            f=1
            break
    if(f==1):
        print('NO')
    else:
        for j in range(r):
            if(rowarr[j]>0):
                f=1
                break
        if(f==0):
            print('YES')
            for i in grid:
                for j in i:
                    sys.stdout.write(str(j))
                sys.stdout.write('\n')
        else:
            print('NO')


import sys
input=sys.stdin.readline
for _ in range(int(input())):
    r,c,a,b=map(int,input().split())
    if(r*a!=c*b):
        print('NO')
        continue
    colarr=[b for i in range(c)]
    rowarr=[a for i in range(r)]
    grid=[[0 for i in range(c)] for j in range(r)]
    for i in range(r):
        for j in range(a):
            mini=0
            minpos=-1
            for k in range(c):
                if(colarr[k]>mini):
                    mini=colarr[k]
                    minpos=k
            if(rowarr[i]>0 and colarr[minpos]>0):
                grid[i][minpos]=1
                colarr[minpos]-=1
                rowarr[i]-=1
    f=0
    for i in range(c):
        if(colarr[i]>0):
            f=1
            break
    if(f==1):
        print('NO')
    else:
        for j in range(r):
            if(rowarr[j]>0):
                f=1
                break
        if(f==0):
            print('YES')
            for i in grid:
                for j in i:
                    sys.stdout.write(str(j))
                sys.stdout.write('\n')
        else:
            print('NO')


import sys
input=sys.stdin.readline
for _ in range(int(input())):
    r,c,a,b=map(int,input().split())
    if(r*a!=c*b):
        print('NO')
        continue
    colarr=[b for i in range(c)]
    rowarr=[a for i in range(r)]
    grid=[[0 for i in range(c)] for j in range(r)]
    for i in range(r):
        for j in range(c):
            mini=0
            minpos=-1
            for k in range(c):
                if(colarr[k]>mini):
                    mini=colarr[k]
                    minpos=k
            if(rowarr[i]>0 and colarr[minpos]>0):
                grid[i][minpos]=1
                colarr[minpos]-=1
                rowarr[i]-=1
    f=0
    for i in range(c):
        if(colarr[i]>0):
            f=1
            break
    if(f==1):
        print('NO')
    else:
        for j in range(r):
            if(rowarr[j]>0):
                f=1
                break
        if(f==0):
            print('YES')
            for i in grid:
                for j in i:
                    sys.stdout.write(str(j))
                sys.stdout.write('\n')
        else:
            print('NO')


from sys import stdin, setrecursionlimit as srl
from threading import stack_size, Thread

srl(int(1e9)+7)
stack_size(int(1e8))

def solve():
    t = int ( input () )
    for _ in range ( t ) :
        n, m, a, b = map(int,input().split())
        if n*a!=m*b:
            print('NO')
            continue
        else:
            print('YES')
            ma=[[0 for i in range(m)]for j in range(n)]
            for i in range(n):
                start=i*a
                for j in range(a):
                    ma[i][(j+start)%m]=1
            for i in ma:
                for j in i:
                    print(j,end='')
                print()

Thread(target=solve).start()


#from sys import stdin, setrecursionlimit as srl
#from threading import stack_size, Thread

#srl(int(1e9)+7)
#stack_size(int(1e8))

def solve():
    t = int ( input () )
    for _ in range ( t ) :
        n, m, a, b = map(int,input().split())
        if n*a!=m*b:
            print('NO')
            continue
        else:
            print('YES')
            ma=[[0 for i in range(m)]for j in range(n)]
            for i in range(n):
                start=i*a
                for j in range(a):
                    ma[i][(j+start)%m]=1
            for i in ma:
                for j in i:
                    print(j,end='')
                print()

#Thread(target=solve).start()
solve()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log, pi, sin
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

for _ in range(int(data())):
    n, m, a, b = sp()
    g = gcd(n, m)
    factors = set()
    i = 1
    while i * i <= g:
        if g % i == 0:
            factors.add(i)
            factors.add(g//i)
        i += 1
    r = False
    for i in factors:
        if a * i == m and b * i == n:
            r = True
            break
    if not r:
        out("NO")
        continue
    out("YES")
    answer = [[0] * m for i in range(n)]
    gap = m//a
    for i in range(n):
        for j in range(m):
            if (i + j) % gap == 0:
                answer[i][j] = 1
    for i in answer:
        out(''.join(map(str, i)))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log, pi, sin
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

for _ in range(int(data())):
    n, m, a, b = sp()
    answer = [[0] * m for i in range(n)]
    temp = 0
    for i in range(1, m):
        if (i * n) % m == 0:
            temp = i
            break
    shift = 0
    for i in range(n):
        for j in range(a):
            answer[i][(j + shift) % m] = 1
        shift += temp
    r = True
    for i in range(m):
        cnt = 0
        for j in range(n):
            cnt += answer[j][i]
        if cnt != b:
            r = False
            break
    if not r:
        out("NO")
        continue
    out("YES")
    for i in answer:
        out(''.join(map(str, i)))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log, pi, sin
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

for _ in range(int(data())):
    n, m, a, b = sp()
    g = gcd(n, m)
    factors = set()
    i = 1
    while i * i <= g:
        if g % i == 0:
            factors.add(i)
            factors.add(g//i)
        i += 1
    r = False
    for i in factors:
        if a * i == m and b * i == n:
            r = True
            break
    if not r:
        out("NO")
        continue
    out("YES")
    answer = [[0] * m for i in range(n)]
    gap = m//a
    for i in range(n):
        for j in range(m):
            if (i + j) % gap == 0:
                answer[i][j] = 1
    for i in answer:
        out(*i)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log, pi, sin
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

for _ in range(int(data())):
    n, m, a, b = sp()
    # g = gcd(n, m)
    # factors = set()
    # i = 1
    # while i * i <= g:
    #     if g % i == 0:
    #         factors.add(i)
    #         factors.add(g//i)
    #     i += 1
    # r = False
    # for i in factors:
    #     if a * i == m and b * i == n:
    #         r = True
    #         break
    # if not r:
    #     out("NO")
    #     continue
    answer = [[0] * m for i in range(n)]
    shift = 0
    for i in range(n):
        for j in range(a):
            answer[i][(j + shift) % m] = 1
        shift += ceil(m/a)-1
    r = True
    for i in range(m):
        cnt = 0
        for j in range(n):
            cnt += answer[j][i]
        if cnt != b:
            r = False
            break
    if not r:
        out("NO")
        continue
    out("YES")
    for i in answer:
        out(''.join(map(str, i)))


from sys import stdin
input = iter(stdin.readlines()).__next__

class wlist(list):
    def __setitem__(self, key, value):
        return super().__setitem__(key % len(self), value)

t = int(input())
for _ in range(t):
    n, m, a, b = map(int, input().split())

    if n * a == m * b:
        print("YES")
        mat = [wlist([0 for _ in range(m)]) for _ in range(n)]
        i = 0
        for ri in range(n):
            for ci in range(i, i + a):
                mat[ri][ci] = 1
            i += a
            print(*mat[ri])
    else:
        print("NO")