#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long q = 1, i, j;
  cin >> q;
  while (q--) {
    int n, k, x = 0, c = 0;
    cin >> n >> k;
    vector<vector<int> > v(n, vector<int>(n, 0));
    if (k % n == 0)
      cout << 0 << '\n';
    else
      cout << 2 << '\n';
    while (k > 0) {
      for (i = 0; i < (n); i++) {
        if (k == 0) break;
        x = i + c;
        if (x > n - 1) x = x - n;
        if (v[x][i] == 0) v[x][i] = 1, k--;
      }
      if (k == 0) break;
      for (i = 0; i < (n); i++) {
        if (k == 0) break;
        x = i + c;
        if (x > n - 1) x = x - n;
        if (v[i][x] == 0) v[i][x] = 1, k--;
      }
      c++;
    }
    for (i = 0; i < (n); i++) {
      for (j = 0; j < (n); j++) cout << v[i][j];
      cout << '\n';
    }
  }
}
