#include <bits/stdc++.h>
using namespace std;
int grid[305][305];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int cases;
  cin >> cases;
  while (cases--) {
    long long n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) grid[i][j] = 0;
    for (int i = 0; i < k; i++) {
      int r = i % n;
      int c = (i / n + i % n) % n;
      grid[r][c] = 1;
    }
    cout << ((k % n) != 0) * 2 << "\n";
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) cout << grid[i][j];
      cout << "\n";
    }
  }
  return 0;
}
