#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; } } int bit_num = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { bit_num = std::max(bit_num, __builtin_ctzll(floor_pow2(a[i][j])) + 1); } std::pair min_cost{ inf, 0 }; for (long long x = 0; x < 1LL << bit_num; ++x) { Weight cost = 0; for (int i = 0; i < n; ++i) { long long s = x ^ a[i][0] ^ a[0][0]; if (s == 0) { continue; } else if (s <= r[i]) { cost += 1; } else { bool ok = false; for (long long y = r[i]; y >= 0; --y) { if ((s ^ y) <= r[i]) { ok = true; break; } } if (ok) { cost += 2; } else { cost += inf; } } } for (int j = 0; j < m; ++j) { long long t = x ^ a[0][j]; if (t == 0) { continue; } else if (t <= c[j]) { cost += 1; } else { bool ok = false; for (long long y = c[j]; y >= 0; --y) { if ((t ^ y) <= c[j]) { ok = true; break; } } if (ok) { cost += 2; } else { cost += inf; } } } min_cost = std::min(min_cost, std::pair{ cost, x }); } 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; }