D. Grid-00100

f:id:peroon:20200807061517p:plain

// 正方行列の転置
void transpose(VV& A){
  ll N = A.size();
  rep(i,N){
    FOR(j,i+1,N){
      swap(A[i][j], A[j][i]);
    }
  }
}

// max - min
ll ma_mi(VV& A){
  ll N = A.size();
  VI B;
  rep(i,N){
    B.push_back(SUM(A[i]));
  }
  return MAX(B) - MIN(B);
}

ll f(VV A){
  ll a = ma_mi(A);
  transpose(A);
  ll b = ma_mi(A);
  return a*a + b*b;
}

// for codeforces
void solve(){
  ll N,K;
  cin>>N>>K;
  VV A(N, VI(N));

  ll n = K/N;
  rep(x,n){
    rep(y,N){
      A[y][x]=1;
    }
  }
  ll r = K%N;
  rep(y,r){
    A[y][n]=1;
  }
  rep(i,N){
    rotate(A[i].begin(), A[i].begin()+N-i, A[i].end());
  }

  // answer
  p(f(A));
  rep(i,N){
    print_vector(A[i]);
  }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N; 
    cin>>N;
    
    while(N--)solve();
    
    return 0;
}