#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } #include using Modint = atcoder::static_modint<998244353>; using namespace std; void testcase(){ int N, M; cin >> N >> M; if(N % 2 == 1 || M % 2 == 1){ cout << "-1\n"; return; } if(N % 4 == 2 && M % 4 == 2){ cout << "-1\n"; return; } bool f = M % 4 == 2; if(f) swap(N, M); vector> ans(N, vector(M)); rep(i,N/2) rep(j,M/4){ int x = j * N + i * 2 + 1; ans[i*2+0][j*4+0] = x+0; ans[i*2+0][j*4+1] = x+0; ans[i*2+0][j*4+2] = x+0; ans[i*2+0][j*4+3] = x+1; ans[i*2+1][j*4+0] = x+0; ans[i*2+1][j*4+1] = x+1; ans[i*2+1][j*4+2] = x+1; ans[i*2+1][j*4+3] = x+1; } if(f) swap(N, M); cout << (N*M/4) << "\n"; if(f){ rep(i,N){ rep(j,M){ if(j) cout << " "; cout << ans[j][i]; } cout << "\n"; } } else { rep(i,N){ rep(j,M){ if(j) cout << " "; cout << ans[i][j]; } cout << "\n"; } } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; rep(t,T) testcase(); return 0; }