#include <bits/stdc++.h>
using namespace std;
int inf = 0x3f3f3f3f;
long long linf = 0x3f3f3f3f3f3f3f3f;
int popcnt(int a) { return __builtin_popcount(a); }
int popcnt(long long a) { return __builtin_popcountll(a); }
int msb(int a) { return 32 - __builtin_clz(a); }
int msb(long long a) { return 64 - __builtin_clzll(a); }
int lsb(int a) { return __builtin_ctz(a); }
int lsb(long long a) { return __builtin_ctzll(a); }
template <typename T, typename U>
T ipow(T a, U e) {
  T b = 1;
  while (e) {
    if (e & 1) b *= a;
    a *= a;
    e >>= 1;
  }
  return b;
}
template <typename T>
void tobin(T v, int len = sizeof(T) * CHAR_BIT) {
  while (--len >= 0) putchar("10"[!(v & (1 << len))]);
}
template <class InputIterator>
void prnt(InputIterator first, InputIterator last, char delim = ' ') {
  for (auto it = first; it != last; it++) cout << *it << delim;
  puts("");
}
int main() {
  int qq;
  scanf("%d", &qq);
  while (qq--) {
    int n, k;
    cin >> n >> k;
    vector<vector<int>> a(n, vector<int>(n, 0));
    int j = 0, i = 0;
    while (k) {
      a[i][(i + j) % n] = 1;
      i++;
      if (i == n) {
        i = 0;
        j++;
      }
      k--;
    }
    if (i == 0)
      puts("0");
    else
      puts("2");
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        printf("%d", a[i][j]);
      }
      puts("");
    }
  }
  return 0;
}
