#include<bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt" , "w" , stdout);
#endif
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int ans[n][n];
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < n ; ++j)
{
ans[i][j] = 0;
}
}
if(n == 2 && k == 2)
{
cout<<"Yes"<<endl;
cout<<"1 0"<<endl<<"1 0"<<endl;
continue;
}
if(k%2 != 0)
{
cout<<"No"<<endl;
}
else
{
if((k == 2) || ((n*n - k) == 2))
{
if(n == 2)
{
cout<<"Yes"<<endl;
ans[0][0] = 1;
ans[0][1] = 1;
ans[1][0]= 1;
ans[1][1] = 1;
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < n ; ++j)
{
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
}
else
{
cout<<"No"<<endl;
}
}
else
{
if(k%4 == 0)
{
int cnt = k/4;
for(int i = 0 ; i < n ; i = i + 2)
{
if(!cnt) break;
for(int j = 0 ; j < n ; j = j + 2)
{
if(cnt == 0) break;
ans[i][j] = 1;
ans[i+1][j] = 1;
ans[i][j+1] = 1;
ans[i+1][j+1] = 1;
cnt--;
}
if(cnt == 0) break;
}
}
else{
ans[0][0] = 1;
ans[0][1] = 1;
ans[1][0] = 1;
ans[2][1] = 1;
ans[1][2] = 1;
ans[2][2] = 1;
int cnt = k - 6;
if(k == (n*n - 6))
{
ans[0][2] = 1;
ans[0][3] = 1;
ans[3][2] = 1;
ans[3][3] = 1;
cnt = cnt - 4;
}
cnt = cnt/4;
for(int i = 0 ; i < n ; i = i + 2)
{
if(!cnt) break;
for(int j = 0 ; j < n ; j = j + 2)
{
if(i <4 && j < 4) continue;
ans[i][j] = 1;
ans[i+1][j] = 1;
ans[i][j+1] = 1;
ans[i+1][j+1] = 1;
cnt--;
if(!cnt) break;
}
if(!cnt) break;
}
}
cout<<"Yes"<<endl;
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < n ; ++j)
{
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
}
}
}
}
102. Binary Tree Level Order Traversal | 96. Unique Binary Search Trees |
75. Sort Colors | 74. Search a 2D Matrix |
71. Simplify Path | 62. Unique Paths |
50. Pow(x, n) | 43. Multiply Strings |
34. Find First and Last Position of Element in Sorted Array | 33. Search in Rotated Sorted Array |
17. Letter Combinations of a Phone Number | 5. Longest Palindromic Substring |
3. Longest Substring Without Repeating Characters | 1312. Minimum Insertion Steps to Make a String Palindrome |
1092. Shortest Common Supersequence | 1044. Longest Duplicate Substring |
1032. Stream of Characters | 987. Vertical Order Traversal of a Binary Tree |
952. Largest Component Size by Common Factor | 212. Word Search II |
174. Dungeon Game | 127. Word Ladder |
123. Best Time to Buy and Sell Stock III | 85. Maximal Rectangle |
84. Largest Rectangle in Histogram | 60. Permutation Sequence |
42. Trapping Rain Water | 32. Longest Valid Parentheses |
Cutting a material | Bubble Sort |