#include <bits/stdc++.h>
using namespace std;
void setup() {}
const long long N = 300 + 5;
long long t, n, k, a[N], dm[N][N];
signed main() {
  setup();
  cin >> t;
  while (t--) {
    cin >> n >> k;
    memset(dm, 0, sizeof(dm));
    for (long long i = 1, x = 0, y = 0; i <= k; i++) {
      dm[x][y] = 1;
      x++;
      y++;
      x %= n;
      if (x == 0) {
        y++;
      }
      y %= n;
    }
    long long rmax = 0, rmin = 90000, cmax = 0, cmin = 90000;
    for (long long i = 0; i < n; i++) {
      long long row = 0;
      for (long long j = 0; j < n; j++) {
        row += dm[i][j];
      }
      rmax = max(rmax, row);
      rmin = min(rmin, row);
    }
    for (long long i = 0; i < n; i++) {
      long long col = 0;
      for (long long j = 0; j < n; j++) {
        col += dm[j][i];
      }
      cmax = max(cmax, col);
      cmin = min(cmin, col);
    }
    cout << (rmax - rmin) * (rmax - rmin) + (cmax - cmin) * (cmax - cmin)
         << '\n';
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        cout << dm[i][j];
      }
      cout << '\n';
    }
  }
}
