#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>; 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]; 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; bst nb; if (nw == 0) { if (!f0) { cout << "-1\n"; return; } nb = eq0; } else { for (auto& [x, b] : base) { if (chmin(nw, nw ^ x)) nb ^= b; } if (nw != 0) { cout << "-1\n"; return; } } 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) vp.push_back({i, j}); else { vp.push_back({i, y}); vp.push_back({i, j}); vp.push_back({i, y}); } } } if (rflg) for (auto& [x, y] : vp) swap(x, y); cout << vp.size() - 1 << '\n'; for (auto [x, y] : vp) cout << x + 1 << ' ' << y + 1 << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int t = 1; // cin >> t; while (t--) solve(); }