#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    long long grid[n + 1][n + 1];
    memset(grid, 0, sizeof(grid));
    long long d = 2 + k / n - 1;
    for (long long i = 1; i < n + 1; i++) {
      for (long long j = 1; j < n + 1; j++) {
        if (i + j <= n + 1 && i + j <= d)
          grid[i][j] = 1;
        else if (i + j > n + 1 && i + j <= n + d)
          grid[i][j] = 1;
      }
    }
    long long res = 0, rem = k % n;
    if (rem != 0) res = 2;
    for (long long i = 1; i < n + 1; i++) {
      if (rem == 0) break;
      grid[i][n + 1 - i] = 1;
      rem--;
    }
    cout << res << '\n';
    for (long long i = 1; i < n + 1; i++) {
      for (long long j = 1; j < n + 1; j++) cout << grid[i][j];
      cout << '\n';
    }
  }
  return 0;
}
