#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    long long rr = k / n;
    long long extra = (k % n);
    long long till = n - extra;
    long long ans = 0;
    if (extra) ans = 2 * ((rr + 1) - (rr)) * ((rr + 1) - (rr));
    cout << ans << "\n";
    long long a[305][305] = {};
    long long count = 0;
    long long i = 0, j = 0;
    while (i != n) {
      if (i < till) {
        for (long long k = 0; k < rr; k++, j++) {
          j = j % n;
          a[i][j] = 1;
        }
      } else {
        for (long long k = 0; k < rr + 1; k++, j++) {
          j = j % n;
          a[i][j] = 1;
        }
      }
      i++;
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) cout << a[i][j];
      cout << "\n";
    }
  }
}
