#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long int T;
  cin >> T;
  while (T--) {
    long long int n;
    cin >> n;
    ;
    long long int k;
    cin >> k;
    ;
    long long int a[500][500];
    memset(a, 0, sizeof(a));
    if (k % n == 0) {
      cout << 0 << "\n";
      long long int t = k / n;
      for (long long int i = 0; i < n; ++i) {
        long long int j = i;
        for (long long int cnt = 0; cnt < t; ++cnt, j = (j + 1) % n) {
          {} { a[i][j] = 1; }
        }
      }
      for (long long int i = 0; i < n; ++i) {
        for (long long int j = 0; j < n; ++j) {
          cout << a[i][j];
        }
        cout << "\n";
      }
    } else {
      cout << 2 << "\n";
      long long int t = k / n;
      long long int rem = k % n;
      long long int c2 = 0;
      for (long long int i = 0; i < n; ++i) {
        long long int j = i;
        if (i < rem)
          c2 = 1;
        else {
          c2 = 0;
        }
        for (long long int cnt = 0; cnt < c2 + t; ++cnt, j = (j + 1) % n)
          a[i][j] = 1;
      }
      for (long long int i = 0; i < n; ++i) {
        for (long long int j = 0; j < n; ++j) {
          cout << a[i][j];
        }
        cout << "\n";
      }
    }
  }
  return 0;
}
