#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
int a[1005][1005];
int main() {
int T = 1, kase = 0;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
if (n <= 3 && m <= 3) {
printf("%d\n", n * m);
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= m; j++)
printf("1");
}
else if (n == 3) {
int ans=m/4*9+m%4*3-(m%4>1);
printf("%d\n",ans);
for(int i=1;i<=n;i++,puts(""))
for(int j=1;j<=m;j++){
if(j%4==2&&i==3||j%4==0&&i!=3) printf("0");
else printf("1");
}
}
else if (m == 3) {
int ans=n/4*9+n%4*3-(n%4>1);
printf("%d\n",ans);
for(int i=1;i<=n;i++,puts(""))
for(int j=1;j<=m;j++){
if(i%4==2&&j==3||i%4==0&&j!=3) printf("0");
else printf("1");
}
}
else {
int ans = 0;
for (int i = 1; i <= n; i++) {
if (i % 4 == 1) ans += m / 4 * 3 + m % 4;
else if (i % 4 == 2) ans += m / 4 * 3 + m % 4 - (m % 4 > 1);
else if (i % 4 == 3) ans += m / 4 * 3 + m % 4 - (m % 4 > 2);
else ans += m / 4 * 3 + m % 4 - (m%4>0);
}
printf("%d\n", ans);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (i % 4 == 1 && j % 4 == 0 || i % 4 == 2 && j % 4 == 2 || i % 4 == 3 && j % 4 == 3 ||
i % 4 == 0 && j % 4 == 1)
a[i][j]=0;
else a[i][j]=1;
}
if(n%4==1&&m%4==1){
for(int i=2;i<=n;i+=4)
for(int j=1;j<=m;j++){
int ov=a[i][j];
a[i][j]=a[i+1][j],a[i+1][j]=a[i+2][j],a[i+2][j]=o;
}
}
for(int i=1;i<=n;i++,puts(""))
for(int j=1;j<=m;j++)
printf("%d",a[i][j]);
}
}
return 0;
}
/*
11101
11011
01110
10111
11101
*/