#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);

    int T, i, j, n, m, q;
    cin >> T;
    vector<int> r(T);
    for(i=0; i<T; i++)
    {
        r[i]=0;
        cin >> n >> m;
        int a[n][m];
        if(n!=m)
        {
            r[i] = -1;
        }
        for(j=0; j<n; j++)
        {
            for (q=0; q<n; q++)
            {
                if(j==0 or j==n-1)
                {
                    a[j][q] = 1;
                }
                else if(q==0 or q==n-1)
                {
                    a[j][q] = 1;
                }
                else if(j+1>n/2 and q+1>n/2)
                {
                    a[j][q] = 1;
                }
                else
                {
                    a[j][q] = 0;
                }
                cout << a[j][q] << " ";
            }
            cout << "\n";
        }
    }


}