#include <bits/stdc++.h>
void rd(int &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
void lrd(long long &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
const int INF = 1e9;
const long long LINF = 1e18;
const int N = 305;
using namespace std;
int T;
int n, k;
bool a[N][N];
int main() {
  rd(T);
  while (T--) {
    rd(n);
    rd(k);
    memset(a, 0, sizeof(a));
    printf("%d\n", k % n ? 2 : 0);
    int u1 = k / n, u2 = k % n;
    for (int i = 1; i <= n; i++) {
      int now = i;
      for (int j = 1; j <= u1 + (i <= u2); j++) {
        a[i][now] = 1;
        now++;
        if (now == n + 1) now = 1;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) printf("%d", a[i][j]);
      puts("");
    }
  }
  return 0;
}
