#include <bits/stdc++.h>
using namespace std;
map<long long, long long> prime;
long long powerFunction(long long x, long long y) {
  long long res = 1;
  long long p = 1000000007;
  ;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void sieve(long long n) {
  for (int p = 2; p * p <= n; p++) {
    if (prime[p]) {
      for (int i = p * p; i <= n; i += p) prime[i] = 0;
    }
  }
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    long long a[n + 1][n + 1];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        a[i][j] = 0;
        a[i][n] = 0;
      }
    }
    if (k % n == 0)
      cout << 0 << '\n';
    else
      cout << 2 << '\n';
    long long p = 0, q = 0;
    while (k > 0) {
      k--;
      a[p][q] = 1;
      p++;
      q++;
      q %= n;
      if (p == n) {
        p = 0;
        q++;
        q %= n;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cout << a[i][j];
      }
      cout << '\n';
    }
  }
}
