#include <bits/stdc++.h>
using namespace std;
const long long mxN = 1e5 + 7;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(3);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s = "";
    for (long long i = 0; i < n; ++i) s += '0';
    vector<string> ans(n, s);
    for (long long curdiag = 0; k > 0; curdiag++) {
      for (long long j = 0; j < n && k > 0; ++j) {
        ans[(curdiag + j) % n][j] = '1';
        --k;
      }
    }
    long long mincol = 1e9, maxcol = 0, minrow = 1e9, maxrow = 0;
    for (long long i = 0; i < n; ++i) {
      long long currow = 0;
      for (long long j = 0; j < n; ++j) {
        if (ans[i][j] == '1') currow++;
      }
      minrow = min(minrow, currow);
      maxrow = max(maxrow, currow);
    }
    for (long long i = 0; i < n; ++i) {
      long long curcol = 0;
      for (long long j = 0; j < n; ++j) {
        if (ans[j][i] == '1') curcol++;
      }
      mincol = min(mincol, curcol);
      maxcol = max(maxcol, curcol);
    }
    cout << (long long)(pow((maxcol - mincol), 2) + pow((maxrow - minrow), 2))
         << "\n";
    for (auto x : ans) cout << x << "\n";
  }
  return 0;
}
