#include <bits/stdc++.h>
using namespace std;
const double delta = 0.0000000001;
const double pi = 3.1415926535;
void FAST() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long int powM(long long int x, long long int y, long long int m) {
  long long int ans = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}
int main() {
  FAST();
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    long long int ans;
    vector<vector<long long int> > a(n, vector<long long int>(n, 0));
    if (k % n == 0)
      ans = 0;
    else
      ans = 2;
    long long int i = 0, j = 0;
    while (k--) {
      a[i][j] = 1;
      i++;
      j = (j + 1) % n;
      if (i == n) {
        i = 0;
        j = (j + 1) % n;
      }
    }
    cout << ans << "\n";
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {
        cout << a[i][j];
      }
      cout << "\n";
    }
  }
}
