#include <bits/stdc++.h>
using namespace std;
const long long N = 305;
long long n, k;
long long a[N][N], rows[N], cols[N];
long long sqr(long long x) { return x * x; }
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (long long i = 0; i < n; i++) {
      rows[i] = cols[i] = 0;
      for (long long j = 0; j < n; j++) a[i][j] = 0;
    }
    long long sti = 0, i = 0, j = 0;
    while (k > 0) {
      if (a[i][j]) {
        sti++;
        i = sti, j = 0;
      } else {
        a[i][j] = 1;
        rows[i]++;
        cols[j]++;
        i++, j++;
        i %= n;
        j %= n;
        k--;
      }
    }
    long long ans = 0;
    sort(rows, rows + n);
    sort(cols, cols + n);
    ans += sqr(rows[n - 1] - rows[0]) + sqr(cols[n - 1] - cols[0]);
    cout << ans << "\n";
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) cout << a[i][j];
      cout << "\n";
    }
  }
  return 0;
}
