#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test;
  cin >> test;
  while (test--) {
    int n, k;
    cin >> n >> k;
    if (k % n == 0) {
      cout << 0;
      cout << "\n";
    } else {
      cout << 2;
      cout << "\n";
    }
    int p = 0;
    int q = 0;
    vector<vector<int>> ans(n, vector<int>(n, 0));
    while (k > 0) {
      k--;
      ans[p][q] = 1;
      p++;
      q++;
      q = q % n;
      if (p == n) {
        p = 0;
        q++;
        q = q % n;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) cout << ans[i][j];
      cout << "\n";
    }
  }
  return 0;
}
