#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
bool sort_pair_da(const pair<long long int, long long int>& a,
                  const pair<long long int, long long int>& b) {
  if (a.first != b.first) {
    return (a.first > b.first);
  } else
    return (a.second < b.second);
}
bool sort_pair_ad(const pair<long long int, long long int>& a,
                  const pair<long long int, long long int>& b) {
  if (a.first != b.first) {
    return (a.first < b.first);
  } else
    return (a.second > b.second);
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  if (!v.empty()) {
    copy(v.begin(), v.end(), ostream_iterator<T>(out, " "));
    out << "\n";
  }
  return out;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin()) os << " ";
  }
  os << "\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin()) os << " ";
  }
  os << "\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
  os << v.first << " " << v.second << "\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v) {
  for (auto it : v) os << it.first << " : " << it.second << "\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v) {
  for (auto it : v) os << it.first << " : " << it.second << "\n";
  return os;
}
vector<int> sieve(long long int n) {
  vector<int> prime(n + 1, 0);
  vector<int> primes(n + 1, 0);
  int index = 0;
  for (int i = 2; i <= n; i++) {
    if (prime[i] == 0) {
      prime[i] = i;
      primes[index] = i;
      index++;
    }
    for (int j = 0; j < index && primes[j] <= prime[i] && i * primes[j] <= n;
         j++) {
      prime[primes[j] * i] = primes[j];
      if (i % primes[j] == 0) break;
    }
  }
  return prime;
}
long long int power(long long int a, long long int n) {
  long long int p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long int powermodulo(long long int a, long long int n, long long int mod) {
  long long int p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
class DSU {
 public:
  long long int n;
  vector<long long int> parent;
  vector<long long int> size;
  void init(long long int x) {
    n = x;
    for (long long int i = 0; i < x + 1; i++) {
      parent.push_back(i);
      size.push_back(1);
    }
  }
  long long int root(long long int x) {
    if (parent[x] == x) return x;
    return parent[x] = root(parent[x]);
  }
  void Union(long long int x, long long int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (size[x] < size[y]) swap(x, y);
      size[x] += size[y];
      parent[y] = x;
    }
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    vector<vector<long long int> > a(n);
    long long int ones = 0;
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {
        if (i == j) {
          if (ones < k) {
            ones++;
            a[i].push_back(1);
          } else {
            a[i].push_back(0);
          }
        } else {
          a[i].push_back(0);
        }
      }
    }
    if (ones < k) {
      for (long long int i = 1; i < n; i++) {
        long long int col = i;
        for (long long int j = 0; j < n && col < n; j++) {
          if (a[j][col] == 0) {
            if (ones < k) {
              ones++;
              a[j][col] = 1;
            }
          }
          col++;
          col = col % n;
        }
      }
      for (long long int i = 1; i < n; i++) {
        long long int row = i;
        for (long long int j = 0; j < n && row < n; j++) {
          if (a[row][j] == 0) {
            if (ones < k) {
              ones++;
              a[row][j] = 1;
            }
          }
          row++;
          row = row % n;
        }
      }
    }
    long long int minr, maxr, maxc, minc;
    minr = minc = 100000000000;
    maxr = maxc = 0;
    for (long long int i = 0; i < n; i++) {
      long long int sumr = 0;
      for (long long int j = 0; j < n; j++) {
        sumr += a[i][j];
      }
      maxr = max(maxr, sumr);
      minr = min(minr, sumr);
    }
    for (long long int j = 0; j < n; j++) {
      long long int sumc = 0;
      for (long long int i = 0; i < n; i++) {
        sumc += a[i][j];
      }
      maxc = max(maxc, sumc);
      minc = min(minc, sumc);
    }
    long long int ans = 0;
    ans += ((maxc - minc) * (maxc - minc));
    ans += ((maxr - minr) * (maxr - minr));
    cout << ans << "\n";
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {
        cout << a[i][j];
      }
      cout << "\n";
    }
  }
}
