#include <bits/stdc++.h>
using namespace std;
const int Nmax = 305;
int a[Nmax][Nmax];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k, i, j;
    cin >> n >> k;
    i = j = 0;
    for (int step = 1; step <= k; ++step) {
      a[i][j] = 1;
      ++i;
      ++j;
      i %= n;
      j %= n;
      if (step % n == 0) {
        ++j;
        j %= n;
      }
    }
    if (k % n == 0)
      cout << 0 << '\n';
    else
      cout << 2 << '\n';
    for (i = 0; i < n; ++i, cout << '\n')
      for (j = 0; j < n; ++j) cout << a[i][j], a[i][j] = 0;
  }
  return 0;
}
