#include <bits/stdc++.h>
using namespace std;
int t, n, k, m, c;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &k);
    if (k % n)
      printf("2\n");
    else
      printf("0\n");
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (j >= i)
          m = j - i;
        else
          m = j - i + n;
        c = m * n + i;
        if (c <= k)
          printf("1");
        else
          printf("0");
      }
      printf("\n");
    }
  }
  return 0;
}
