#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} int solve(){ int h, w; cin >> h >> w; int n = h * w; if(n % 4 != 0){ cout << "-1\n"; return 0; } int k = n / 4; vector s(n, 0); bool success = false; auto f = [&](int y, int x){ if(y < 0 || y >= h || x < 0 || x >= w) return -1; return y * w + x; }; auto out = [&](){ cout << k << "\n"; rep(y, h){ rep(x, w) cout << s[f(y, x)] << ' '; cout << "\n"; } }; vector> dy = {{0, 0, 0, 1}, {0, 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 1, 2}, {0, 1, 2, 2}, {0, 0, 1, 2}, {0, 1, 2, 2}, {0, 1, 1, 1}}; vector> dx = {{0, 1, 2, 2}, {0, 0, 1, 2}, {0, 1, 2, 0}, {0, 1, 1, 1}, {0, 0, 0, 1}, {0, 1, 2, 3}, {0, 0, 0, -1}, {0, 0, -1, -2}}; auto next = [&](int pos){ int y = pos / w; int x = pos % w; vector> cand; rep(t, 8){ bool ok = true; vector v; rep(i, 4){ int pos_new = f(y + dy[t][i], x + dx[t][i]); if(pos_new == -1){ok = false; break;} if(s[pos_new] != 0){ok = false; break;} v.push_back(pos_new); } if(ok) cand.push_back(v); } return cand; }; auto dfs = [&](auto dfs, int pos, int num) -> void{ if(success) return; if(pos == n){ out(); success = true; return; } if(s[pos] == 0){ vector> cand = next(pos); for(auto& v : cand){ for(auto pos_new : v) s[pos_new] = num; dfs(dfs, pos + 1, num + 1); for(auto pos_new : v) s[pos_new] = 0; } }else dfs(dfs, pos + 1, num); }; dfs(dfs, 0, 1); if(!success) cout << "-1\n"; return 0; } int main(){ int t; cin >> t; rep(_, t) solve(); }