#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const long long int mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
void cp() {
  int n, k;
  cin >> n >> k;
  int arr[n][n];
  memset(arr, 0, sizeof(arr));
  vector<int> ptr(n, 0);
  iota(ptr.begin(), ptr.end(), 0);
  int p = 0;
  int pos = 0;
  for (int i = 0; i < k; i++) {
    arr[pos][ptr[p]] = 1;
    ptr[p] = (ptr[p] + 1) % n;
    p = (p + 1) % n;
    pos = (pos + 1) % n;
  }
  vector<int> row(n, 0), col(n, 0);
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      cnt += arr[i][j];
    }
    row[i] = cnt;
  }
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      cnt += arr[j][i];
    }
    col[i] = cnt;
  }
  int row_sum = *max_element(row.begin(), row.end()) -
                *min_element(row.begin(), row.end());
  row_sum *= row_sum;
  int col_sum = *max_element(col.begin(), col.end()) -
                *min_element(col.begin(), col.end());
  col_sum *= col_sum;
  cout << row_sum + col_sum << '\n';
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cout << arr[i][j];
    }
    cout << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  t = 1;
  cin >> t;
  while (t--) {
    cp();
  }
  return 0;
}
