#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int res[305][305] = {0};
    int n, k;
    cin >> n >> k;
    if (k % n == 0)
      cout << "0\n";
    else
      cout << "2\n";
    int p = 0, q = 0;
    while (k > 0) {
      k--;
      res[p][q] = 1;
      p++;
      q++;
      q %= n;
      if (p == n) {
        p = 0;
        q++;
        q %= n;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cout << res[i][j];
      }
      cout << "\n";
    }
  }
  return 0;
}
