#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)...); } template class Object { static inline std::vector pool{}; static constexpr int null_ptr = -1; struct Pointer { constexpr Pointer(): Pointer(null_ptr) {} constexpr Pointer(int ptr) : ptr(ptr) {} Derived& operator*() const { return pool[ptr]; } Derived* operator->() const { return &pool[ptr]; } constexpr operator bool() const { return ptr != null_ptr; } explicit constexpr operator int() const { return ptr; } private: int ptr; }; public: using pointer = Pointer; static constexpr pointer null = Pointer(null_ptr); template static pointer create(Args &&...args) { pointer ptr = pool.size(); pool.emplace_back(std::forward(args)...); return ptr; } }; using Weight = long long; struct Node: public Object { std::array child{ null, null }; Weight weight = 0; static pointer get_or_create(pointer ptr, bool child) { if (not ptr->child[child]) { ptr->child[child] = create(); } return ptr->child[child]; } }; 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; } } Node::pointer root = Node::create(); // add f(X) = W * [X ^ Y > Z] auto add_cost = [&](long long Y, long long Z, Weight W) { const long long YZ = Y ^ Z; Node::pointer cur = root; for (int bit = L - 1; bit >= 0; --bit) { Node::pointer nxt = Node::get_or_create(cur, (YZ >> bit) & 1); if (not ((Z >> bit) & 1)) { cur->weight += W; nxt->weight -= W; } cur = nxt; } }; for (int i = 0; i < n; ++i) { long long Y = a[0][0] ^ a[i][0]; add_cost(Y, 0, 1); } for (int j = 0; j < m; ++j) { long long Y = a[0][j]; add_cost(Y, 0, 1); } std::pair min_cost{ inf, 0 }; std::vector> st{ { root, 0, 1LL << L } }; while (st.size()) { auto [node, l, r] = st.back(); st.pop_back(); if (r - l == 1) { min_cost = std::min(min_cost, { node->weight, l }); continue; } const long long m = (l + r) >> 1; if (Node::pointer lch = node->child[0]) { lch->weight += node->weight; st.emplace_back(lch, l, m); } else { min_cost = std::min(min_cost, { node->weight, l }); } if (Node::pointer rch = node->child[1]) { rch->weight += node->weight; st.emplace_back(rch, m, r); } else { min_cost = std::min(min_cost, { node->weight, m }); } } 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 { println('r', i + 1, s); } } for (int j = 0; j < m; ++j) { long long t = X ^ a[0][j]; if (t == 0) { continue; } else { println('c', j + 1, t); } } } 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; }