#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, k, i, j;
    cin >> n >> k;
    if (k == 0) {
      cout << 0 << "\n";
      for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
          cout << 0;
        }
        cout << "\n";
      }
    } else {
      long long int a[n + 1][n + 1];
      for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
          a[i][j] = 0;
        }
      }
      long long int r = k / n;
      long long int r1 = k % n;
      for (i = 0; i < n; i++) {
        long long int pp = 0;
        for (j = i; j < n; j++) {
          if (pp == r) {
            break;
          } else {
            pp++;
            a[i][j] = 1;
          }
        }
        if (pp != r) {
          for (j = 0; j < i; j++) {
            if (pp == r) {
              break;
            } else {
              pp++;
              a[i][j] = 1;
            }
          }
        }
      }
      if (r1 != 0) {
        for (i = 0; i < n; i++) {
          a[i + 1][i] = 1;
          r1--;
          if (r1 == 0) {
            break;
          }
        }
      }
      long long int maxir = -100000000;
      long long int minir = 100000000;
      long long int maxic = -100000000;
      long long int minic = 100000000;
      for (i = 0; i < n; i++) {
        long long int s = 0;
        for (j = 0; j < n; j++) {
          s += a[i][j];
        }
        maxir = max(maxir, s);
        minir = min(minir, s);
      }
      for (i = 0; i < n; i++) {
        long long int s1 = 0;
        for (j = 0; j < n; j++) {
          s1 += a[j][i];
        }
        maxic = max(maxic, s1);
        minic = min(minic, s1);
      }
      cout << ((maxir - minir) * (maxir - minir)) +
                  ((maxic - minic) * (maxic - minic))
           << "\n";
      for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
          cout << a[i][j];
        }
        cout << "\n";
      }
    }
  }
  return 0;
}
