#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } using bst = bitset<410>; mt19937 mt(random_device{}()); void solve() { int h, w; cin >> h >> w; vector a(h, vector(w, 0)); rep(i, 0, h) rep(j, 0, w) cin >> a[i][j]; // rep(i, 0, h) rep(j, 0, w) a[i][j] = mt(); int rflg = 0; if (h > w) { vector ra(w, vector(h)); rep(i, 0, h) rep(j, 0, w) ra[j][i] = a[i][j]; swap(h, w); swap(a, ra); rflg = 1; } vector> base; bst eq0; int f0 = 0; rep(i, 0, h) rep(j, 0, w) { bst nb; nb.set(i * w + j); ll nw = a[i][j]; for (auto& [x, b] : base) { if (chmin(nw, nw ^ x)) nb ^= b; } if (nw > 0) base.push_back({nw, nb}); else if (!f0) eq0 = nb, f0 = 1; } int q; cin >> q; while (q--) { ll nw; cin >> nw; // nw = mt(); bst nb; if (nw == 0) { if (!f0) { cout << "-1\n"; continue; } nb = eq0; } else { for (auto& [x, b] : base) { if (chmin(nw, nw ^ x)) nb ^= b; } if (nw != 0) { cout << "-1\n"; continue; } } vector> vp; rep(i, 0, h) rep(j, 0, w) if (nb[i * w + j]) { if (vp.empty()) vp.push_back({i, j}); else { auto [x, y] = vp.back(); if (x == i && y == j) { if (y > 0) { vp.push_back({i, 0}); vp.push_back({i, j}); vp.push_back({i, 0}); } else assert(0); } else if (x == i || y == j) vp.push_back({i, j}); else { vp.push_back({i, y}); vp.push_back({i, j}); vp.push_back({i, y}); } } } assert((int)vp.size() <= 2 * h * w); cout << vp.size() - 1 << '\n'; for (auto [x, y] : vp) { if (rflg) swap(x, y); cout << x + 1 << ' ' << y + 1 << '\n'; } rep(i, 0, vp.size() - 1) { int d = vp[i].first == vp[i + 1].first; int b = vp[i].second == vp[i + 1].second; if (!(b ^ d)) cout << endl; assert(b ^ d); } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int t = 1; // cin >> t; while (t--) solve(); }