#include #include #include #include #include #include #include signed main() { int n; std::cin >> n; std::vector pos(n, std::valarray(n)); std::valarray row(n), col(n); for(const auto i : std::views::iota(0, n)) { for(const auto j : std::views::iota(0, n)) { char c; std::cin >> c; if((pos[i][j] = (c == '#'))) row[i] ^= 1, col[j] ^= 1; } } auto check_parity = [&](const bool even) { if(even && row[0] != 0) return false; if(row[0] != col[0]) return false; for(const auto i : std::views::iota(1, n)) { if(row[0] != row[i] or col[0] != col[i]) { return false; } } return true; }; if(!check_parity(true)) { std::cout << "-1\n"; return 0; } if(--n == 0) { std::cout << "0\n"; return 0; } for(const auto i : std::views::iota(1, n)) { for(const auto j : std::views::iota(0, n)) { pos[i][j] ^= pos[i - 1][j]; } } for(const auto i : std::views::iota(0, n)) { for(const auto j : std::views::iota(1, n)) { pos[i][j] ^= pos[i][j - 1]; } } std::ranges::fill(row, 0); std::ranges::fill(col, 0); for(const auto i : std::views::iota(0, n)) { for(const auto j : std::views::iota(0, n)) { if(pos[i][j]) row[i] ^= 1, col[j] ^= 1; } } if(n % 2 == 1 && !check_parity(false)) { std::cout << "-1\n"; return 0; } std::cout << 0 << "\n"; } __attribute__((constructor)) inline void fast_io() { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); }