#include<bits/stdc++.h>
#define int long long
using namespace std;
using LL = long long;
#define rd dist(rnd)
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
mt19937_64 rnd(random_device{}());
uniform_int_distribution<LL> dist(0, LLONG_MAX);
int T;
cin >> T;
while(T--){
int n, m;
cin >> n >> m;
vector<vector<LL> > a(n, vector<LL>(m));
while (1) {
set<int> se;
for (int i = 0;i < n;i++)
a[i][0] = rd;
for (int i = 0;i < m;i++)
a[0][i] = rd;
int tar = rd;
for (int i = 1;i < n;i++)
for (int j = 1;j < m;j++)
a[i][j] = tar ^ a[i - 1][j - 1] ^ a[i][j - 1] ^ a[i - 1][j];
for (int i = 0;i < n;i++)
for (int j = 0;j < m;j++)
se.insert(a[i][j]);
if (se.size() == n * m){
cout << n * m << '\n';
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cout << a[i][j] << " \n"[j == m - 1];
}
}
break;
}
}
}
}
1363A - Odd Selection | 131B - Opposites Attract |
490C - Hacking Cypher | 158B - Taxi |
41C - Email address | 1373D - Maximum Sum on Even Positions |
1574C - Slay the Dragon | 621A - Wet Shark and Odd and Even |
1395A - Boboniu Likes to Color Balls | 1637C - Andrew and Stones |
1334B - Middle Class | 260C - Balls and Boxes |
1554A - Cherry | 11B - Jumping Jack |
716A - Crazy Computer | 644A - Parliament of Berland |
1657C - Bracket Sequence Deletion | 1657B - XY Sequence |
1009A - Game Shopping | 1657A - Integer Moves |
230B - T-primes | 630A - Again Twenty Five |
1234D - Distinct Characters Queries | 1183A - Nearest Interesting Number |
1009E - Intercity Travelling | 1637B - MEX and Array |
224A - Parallelepiped | 964A - Splits |
1615A - Closing The Gap | 4C - Registration System |