#include <bits/stdc++.h>
using namespace std;
template <class T>
inline istream& operator>>(istream& str, vector<T>& a) {
  for (auto& i : a) str >> i;
  return str;
}
const int N = 330;
int ans[N][N];
int main() {
  int T, tc = 0;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        ans[i][j] = 0;
      }
    }
    int cnt = 0, i1 = n - 1, j2 = n - 1, inc = 0;
    while (cnt < n * n and k > 0) {
      for (int j1 = 0; j1 <= i1; ++j1) {
        if (k == 0) break;
        ans[i1 - j1][j1] = 1;
        k--;
        cnt++;
        if (cnt > n * n) break;
      }
      i1--;
      int inc = 0;
      for (int i2 = n - 1; i2 >= j2; --i2) {
        if (k == 0) break;
        ans[i2][j2 + inc] = 1;
        inc++;
        k--;
        cnt++;
        if (cnt > n * n) break;
      }
      if (k == 0) break;
      j2--;
    }
    int rmx = 0, rmn = 1e9, cmx = 0, cmn = 1e9;
    for (int i = 0; i < n; ++i) {
      int rsum = 0;
      int csum = 0;
      for (int j = 0; j < n; ++j) {
        csum += ans[i][j];
        rsum += ans[j][i];
      }
      rmx = max(rmx, rsum);
      rmn = min(rmn, rsum);
      cmx = max(cmx, csum);
      cmn = min(cmn, csum);
    }
    int x = (rmx - rmn) * (rmx - rmn) + (cmx - cmn) * (cmx - cmn);
    cout << x << "\n";
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cout << ans[i][j];
      }
      cout << "\n";
    }
  }
  return 0;
}
