#include using namespace std; using ll = long long; int h, w, q; ll a[25][25]; inline bool is_adj(int x1, int y1, int x2, int y2) { if (x1 == x2 && y1 == y2) return false; if (x1 == x2 || y1 == y2) return true; if (abs(x1 - x2) == abs(y1 - y2)) return true; return false; } void solve() { cin >> h >> w; int n = h * w; vector> cells; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { cin >> a[i][j]; cells.push_back({i, j}); } } vector basis(60, 0); vector> pivot_comb(60); vector pivot_col; for (int i = 0; i < n; i++) { ll val = a[cells[i].first][cells[i].second]; vector cur_comb(n, 0); cur_comb[i] = 1; for (int b = 59; b >= 0; b--) { if ((val >> b) & 1) { if (!basis[b]) { basis[b] = val; pivot_comb[b] = cur_comb; pivot_col.push_back(b); break; } val ^= basis[b]; for (int j = 0; j < n; j++) { cur_comb[j] ^= pivot_comb[b][j]; } } } } vector> nullspace; for (int i = 0; i < n; i++) { ll val = a[cells[i].first][cells[i].second]; vector cur_comb(n, 0); cur_comb[i] = 1; for (int b = 59; b >= 0; b--) { if ((val >> b) & 1) { if (basis[b]) { val ^= basis[b]; for (int j = 0; j < n; j++) { cur_comb[j] ^= pivot_comb[b][j]; } } } } if (val == 0) { int cnt = 0; for (int x : cur_comb) cnt += x; if (cnt > 0) nullspace.push_back(cur_comb); } } cin >> q; while (q--) { ll x; cin >> x; if (x == 0) { cout << 3 << "\n"; cout << 1 << " " << 1 << "\n"; cout << 1 << " " << 2 << "\n"; cout << 1 << " " << 1 << "\n"; cout << 1 << " " << 2 << "\n"; continue; } ll rem = x; vector sol(n, 0); for (int b = 59; b >= 0; b--) { if ((rem >> b) & 1) { if (!basis[b]) { rem = -1; break; } rem ^= basis[b]; for (int j = 0; j < n; j++) { sol[j] ^= pivot_comb[b][j]; } } } if (rem != 0) { cout << -1 << "\n"; continue; } int sol_cnt = 0; for (int v : sol) sol_cnt += v; if (sol_cnt % 2 == 0) { for (auto &null_v : nullspace) { int null_cnt = 0; for (int v : null_v) null_cnt += v; if (null_cnt % 2 != 0) { for (int j = 0; j < n; j++) { sol[j] ^= null_v[j]; } sol_cnt = 0; for (int v : sol) sol_cnt += v; break; } } } vector c_indices; for (int i = 0; i < n; i++) { if (sol[i]) c_indices.push_back(i); } vector> path; if (sol_cnt % 2 != 0) { int r_idx = c_indices[0]; auto r = cells[r_idx]; path.push_back(r); for (size_t i = 1; i < c_indices.size(); i++) { auto v = cells[c_indices[i]]; if (is_adj(r.first, r.second, v.first, v.second)) { path.push_back(v); path.push_back(r); } else { auto u = make_pair(r.first, v.second); path.push_back(u); path.push_back(v); path.push_back(u); path.push_back(r); } } } else { int r_idx = c_indices[0]; int w_idx = c_indices[1]; auto r = cells[r_idx]; auto w = cells[w_idx]; path.push_back(r); for (size_t i = 2; i < c_indices.size(); i++) { auto v = cells[c_indices[i]]; if (is_adj(r.first, r.second, v.first, v.second)) { path.push_back(v); path.push_back(r); } else { auto u = make_pair(r.first, v.second); path.push_back(u); path.push_back(v); path.push_back(u); path.push_back(r); } } if (is_adj(r.first, r.second, w.first, w.second)) { path.push_back(w); } else { auto u = make_pair(r.first, w.second); path.push_back(u); path.push_back(r); path.push_back(u); path.push_back(w); } } cout << path.size() - 1 << "\n"; for (auto &p : path) { cout << p.first << " " << p.second << "\n"; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }