1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include <bits/stdc++.h>
using i64 = long long;
struct DSU { std::vector<int> p, siz; DSU(int n) : p(n + 1), siz(n + 1, 1) { std::iota(p.begin(), p.end(), 0); }
int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
bool same(int x, int y) { return find(x) == find(y); }
bool merge(int x, int y) { x = find(x), y = find(y); if (x == y) return false; siz[x] += siz[y]; p[y] = x; return true; } int size(int x) { return siz[find(x)]; } };
template <typename T> T qpower(T a, i64 b, T init = T(), int mod = 0) { T res = init; if (!mod) { while (b > 0) { if (b & 1) { res = res * a; } a = a * a; b >>= 1; } } else { while (b > 0) { if (b & 1) { res = res * a % mod; } a = a * a % mod; b >>= 1; } } return res; }
void solve() { int n, m; std::cin >> n >> m; std::vector<std::string> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } DSU dsu(2 * n); std::vector<std::vector<int>> b(m); for (int j = 0; j < (m + 1) / 2; j++) { for (int i = 0; i < n; i++) { if (a[i][j] == '1') { b[j].push_back(i); } if (a[i][m - 1 - j] == '1') { b[j].push_back(i); } } if (b[j].size() > 2) { std::cout << "0\n"; return; } if (b[j].size() == 2) { if (b[j][0] == b[j][1]) { continue; } int x = b[j][0] + 1, y = b[j][1] + 1; if (a[b[j][0]][j] == a[b[j][1]][j]) { dsu.merge(x, y + n); dsu.merge(x + n, y); } else { dsu.merge(x, y); dsu.merge(x + n, y + n); } } } for (int i = 1; i <= n; i++) { if (dsu.same(i, i + n)) { std::cout << 0 << "\n"; return; } }
std::set<int> s; for (int i = 1; i <= 2 * n; i++) { s.insert(dsu.find(i)); }
int ans = qpower<i64>(2, s.size() / 2, 1, 1E9 + 7); std::cout << ans << "\n"; }
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr);
int t = 1; std::cin >> t; while (t--) solve();
return 0; }
|