#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
struct DSU {
vector<int> parent;
DSU() {}
DSU(int N): parent(N, -1) {}
int get(int x){
return (parent[x] < 0) ? x : (parent[x] = get(parent[x]));
}
bool merge(int a, int b){
a = get(a); b = get(b);
if (a == b) return false;
if (parent[a] > parent[b]) swap(a, b);
parent[a] += parent[b];
parent[b] = a;
return true;
}
};
bool check(int n, const vector<string> &vec){
int R = vec.size(), C = vec[0].size();
DSU dsu((R + C) * 2);
for (int i = 0; i < R; i++){
for (int j = 0; j < C; j++){
if (vec[i][j] == 'X'){
dsu.merge(R, C + n);
dsu.merge(R + 1, C + n + 1);
}
else{
dsu.merge(R, C + n + 1);
dsu.merge(R + 1, C + n);
}
}
}
for (int i = 0; i < (R + C) * 2; i += 2){
if (dsu.get(i) == dsu.get(i+1)) return false;
}
return true;
}
bool reversal(int n, int m, vector<string> v){
bool valid = true;
for (int a = 0; a < m; a++){
for (int b = 0; b < m; b++){
vector<string> vec;
for (int row = a; row < n; row += m){
vec.emplace_back();
for (int col = b; col < n; col += m){
vec.back().push_back(v[row][col]);
}
}
valid &= check(n, vec);
}
}
return valid;
}