#include <bits/stdc++.h>
int modspecial(int n, int p) {
  if (n >= 0) return n % p;
  while (n < 0) n += p;
  return n;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);
  int t;
  std::cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    std::cin >> n >> k;
    int mat[n * n];
    int numofOnes = k / n;
    int remaining = k % n;
    for (int j = 0; j < n; j++) {
      int extra = 0;
      if (remaining > 0) extra = 1;
      for (int l = 0; l < n; l++) {
        if (modspecial(l - j, n) < (numofOnes + extra))
          mat[j * n + l] = 1;
        else
          mat[j * n + l] = 0;
      }
      if (remaining > 0) remaining--;
    }
    if (k % n == 0)
      std::cout << "0\n";
    else
      std::cout << "2\n";
    for (int j = 0; j < n; j++) {
      for (int l = 0; l < n; l++) {
        std::cout << mat[j * n + l];
      }
      std::cout << "\n";
    }
  }
  return 0;
}
