#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 3e2 + 7;
const int maxm = maxn * 30;
const int mod = 20090126;
int a[maxn][maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    memset((a), (0), sizeof(a));
    int num1 = 0, num2 = 0;
    int cnt = 0;
    while (k) {
      for (int j = 1; j <= n && k; j++, k--) {
        int x = j + cnt;
        int y = j;
        if (x > n) x = x - n;
        if (y > n) y = y - n;
        a[x][y] = 1;
        num1 = max(num1, ++a[x][0]);
        num2 = max(num2, ++a[0][y]);
      }
      cnt++;
    }
    int minn1 = INF, minn2 = INF;
    for (int i = 1; i <= n; ++i)
      minn1 = min(minn1, a[i][0]), minn2 = min(minn2, a[0][i]);
    minn1 -= num1;
    minn2 -= num2;
    cout << minn1 * minn1 + minn2 * minn2 << '\n';
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) cout << a[i][j];
      cout << '\n';
    }
  }
  return 0;
}
