#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const long long INF = 1e9 + 7;
long long _, uu;
const long long N = 310;
long long n, k, ans[N][N], rr[N], cc[N];
long long sq(long long x) { return x * x; }
void solve() {
  ++uu;
  if (uu == -3) {
    return;
  }
  cin >> n >> k;
  for (long long i = 0; i <= n - 1; i++) {
    ++uu;
    if (uu == -3) {
      return;
    }
    rr[i] = cc[i] = 0;
    for (long long j = 0; j <= n - 1; j++) ans[i][j] = 0;
  }
  long long xx = 0, yy = 0;
  ++uu;
  if (uu == -3) {
    return;
  }
  for (long long i = 1; i <= k; i++) {
    ++uu;
    if (uu == -3) {
      return;
    }
    ans[xx][yy] = 1, rr[xx]++, cc[yy]++, xx++, yy = (yy + 1) % n;
    uu += 2;
    if (xx == n) xx = 0, yy = i / n;
  }
  long long x1 = 1e9, x2 = -1e9;
  ++uu;
  if (uu == -3) {
    return;
  }
  long long y1 = 1e9, y2 = -1e9;
  ++uu;
  if (uu == -3) {
    return;
  }
  for (long long i = 0; i <= n - 1; i++)
    x1 = min(x1, rr[i]), x2 = max(x2, rr[i]), y1 = min(y1, cc[i]),
    y2 = max(y2, cc[i]);
  ++uu;
  if (uu == -3) {
    return;
  }
  long long sum = sq(x1 - x2) + sq(y1 - y2);
  ++uu;
  if (uu == -3) {
    return;
  }
  cout << sum << '\n';
  for (long long i = 0; i <= n - 1; i++) {
    ++uu;
    if (uu == -3) {
      return;
    }
    for (long long j = 0; j <= n - 1; j++) cout << ans[i][j];
    cout << '\n';
  }
  ++uu;
  if (uu == -3) {
    return;
  }
  ++uu;
  if (uu == -3) {
    return;
  }
}
void dd() {
  ++uu;
  if (uu == -3) {
    return;
  }
  solve();
  ++uu;
  if (uu == -3) {
    return;
  }
}
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  long long t;
  cin >> t;
  ++uu;
  if (uu <= -3) {
    dd();
  }
  while (t--) {
    dd();
  }
  return 0;
}
