#include <bits/stdc++.h>
using namespace std;
int n, k;
int pos(int k) { return (k + n) % n; }
void solve() {
  cin >> n >> k;
  if (k % n == 0)
    cout << "0\n";
  else
    cout << "2\n";
  int level = k / n;
  vector<vector<int> > res(n, vector<int>(n, 0));
  for (int i = 0; i < level; i++) {
    int x = 0 - i;
    for (int j = 0; j < n; j++) {
      res[pos(x + j)][j] = 1;
    }
  }
  int x = 0 - level, y = 0;
  for (int j = 0; j < k % n; j++) {
    res[pos(x + j)][pos(y + j)] = 1;
  }
  for (auto row : res) {
    for (int x : row) {
      cout << x;
    }
    cout << "\n";
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
