#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<vector<int>> a(n, vector<int>(n, 0));
    int c = 0, i = 0, j = 0, p = 1;
    while (c < k) {
      c++;
      a[i][j] = 1;
      i++;
      j++;
      if (i == n) i = 0;
      if (j == n) j = 0;
      if (c % n == 0) {
        i = 0;
        j = p++;
      }
    }
    int minR = 1e8, maxR = -1;
    int minC = 1e8, maxC = -1;
    for (int i = 0; i < n; i++) {
      int cc = 0;
      for (int j = 0; j < n; j++) cc += a[i][j];
      minR = min(minR, cc);
      maxR = max(maxR, cc);
    }
    for (int i = 0; i < n; i++) {
      int cc = 0;
      for (int j = 0; j < n; j++) cc += a[j][i];
      minC = min(minC, cc);
      maxC = max(maxC, cc);
    }
    int r = maxR - minR, co = maxC - minC;
    cout << r * r + co * co << '\n';
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) cout << a[i][j];
      cout << '\n';
    }
  }
  return 0;
}
