#include <bits/stdc++.h>
using namespace std;
const int N = 305;
int ar[N][N];
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ar[i][j] = 0;
    }
  }
  int p = 0, q = 0;
  for (int i = 1; i <= (int)(k); ++i) {
    ar[p + 1][q + 1] = 1;
    p++;
    q = (q + 1) % n;
    if (p == n) {
      p = 0;
      q = (q + 1) % n;
    }
  }
  cout << (k % n ? 2 : 0) << '\n';
  for (int i = 1; i <= (int)(n); ++i) {
    for (int j = 1; j <= (int)(n); ++j) {
      cout << ar[i][j];
    }
    cout << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
