#include #include #include #include #include #include #include long long floor_pow2(long long n) { long long x = 1; while ((x << 1) <= n) x <<= 1; return x; } void println() { std::cout << '\n'; } template void println(Head&& head, Tails &&...tails) { std::cout << std::forward(head); if constexpr (sizeof...(Tails)) { std::cout << ' '; } println(std::forward(tails)...); } using Weight = long long; constexpr int L = 60; constexpr Weight inf = std::numeric_limits::max(); void solve(int n, int m, const std::vector& r, const std::vector& c, const std::vector>& a) { for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (a[0][0] ^ a[0][j] ^ a[i][0] ^ a[i][j]) { println(-1); return; } } // (l, r, w): add w to x in [l, r) std::vector> costs; // add f(X) = W * [X ^ Y > Z] auto add_cost = [&](long long Y, long long Z, Weight W) { const long long YZ = Y ^ Z; std::vector> costs_l, costs_r; long long l = 0, r = 1LL << L; for (int bit = L - 1; bit >= 0; --bit) { long long mid = (l + r) >> 1; if ((YZ >> bit) & 1) { if (not ((Z >> bit) & 1)) { if (costs_l.size() and std::get<1>(costs_l.back()) == l) { std::get<1>(costs_l.back()) = mid; } else { costs_l.emplace_back(l, mid, W); } } l = mid; } else { if (not ((Z >> bit) & 1)) { if (costs_r.size() and std::get<1>(costs_r.back()) == r) { std::get<1>(costs_r.back()) = mid; } else { costs_r.emplace_back(mid, r, W); } } r = mid; } } std::move(costs_l.begin(), costs_l.end(), std::back_inserter(costs)); std::move(costs_r.begin(), costs_r.end(), std::back_inserter(costs)); }; for (int i = 0; i < n; ++i) { long long Y = a[0][0] ^ a[i][0]; if (r[i]) { add_cost(Y, 0, 1); add_cost(Y, r[i], 1); add_cost(Y, 2 * floor_pow2(r[i]) - 1, inf); } else { add_cost(Y, 0, inf); } } for (int j = 0; j < m; ++j) { long long Y = a[0][j]; if (c[j]) { add_cost(Y, 0, 1); add_cost(Y, c[j], 1); add_cost(Y, 2 * floor_pow2(c[j]) - 1, inf); } else { add_cost(Y, 0, inf); } } std::vector xs { 0, 1LL << L }; for (auto [l, r, w] : costs) { xs.push_back(l); xs.push_back(r); } std::sort(xs.begin(), xs.end()); xs.erase(std::unique(xs.begin(), xs.end()), xs.end()); const int k = xs.size(); std::vector imos(k); for (auto [l, r, w] : costs) { imos[std::lower_bound(xs.begin(), xs.end(), l) - xs.begin()] += w; imos[std::lower_bound(xs.begin(), xs.end(), r) - xs.begin()] -= w; } for (int i = 1; i < k; ++i) { imos[i] += imos[i - 1]; } std::pair min_cost{ inf, 0 }; for (int i = 0; i < k - 1; ++i) { min_cost = std::min(min_cost, std::make_pair(imos[i], xs[i])); } auto [cost, X] = min_cost; if (cost >= inf) { println(-1); return; } println(cost); for (int i = 0; i < n; ++i) { long long s = X ^ a[0][0] ^ a[i][0]; if (s == 0) { continue; } else if (s <= r[i]) { println('r', i + 1, s); } else { long long p = floor_pow2(r[i]); println('r', i + 1, p); println('r', i + 1, s ^ p); } } for (int j = 0; j < m; ++j) { long long t = X ^ a[0][j]; if (t == 0) { continue; } else if (t <= c[j]) { println('c', j + 1, t); } else { long long p = floor_pow2(c[j]); println('c', j + 1, p); println('c', j + 1, t ^ p); } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t-- > 0) { int n, m; std::cin >> n >> m; std::vector r(n), c(m); for (auto& e : r) std::cin >> e; for (auto& e : c) std::cin >> e; std::vector a(n, std::vector(m)); for (auto& row : a) for (auto& e : row) { std::cin >> e; } solve(n, m, r, c, a); } return 0; }