#include <bits/stdc++.h>
using namespace std;
int num[510][510], n, k;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k), printf("%d\n", k % n ? 2 : 0);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) num[i][j] = 0;
    for (int i = 1; i <= n && k; i++)
      for (int j = 1; j <= n && k; j++) k--, num[j][(i + j - 2) % n + 1] = 1;
    for (int i = 1; i <= n; printf("\n"), i++)
      for (int j = 1; j <= n; j++) printf("%d", num[i][j]);
  }
}
