#include using namespace std; typedef pair pii; typedef long long ll; const int N = 2000010, MOD = 1e9 + 7, INF = 0x3f3f3f3f; int n, m, w[N]; ll qmi(ll a, ll b, ll c) { ll res = 1; while (b) { if (b & 1) res = res * a % c; a = a * a % c; b >>= 1; } return res; } bool isprime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int findPrime(int n) { while (!isprime(n)) n++; return n; } typedef array Hash; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int P = findPrime(rng() % 900000000 + 100000000); ll p[N], p2[N]; Hash h[N]; void init() { p2[0] = p[0] = 1; for (int i = 1; i < N; i++) p[i] = p[i - 1] * 13331 % P, p2[i] = p2[i - 1] * 131 % P; } char s[101][102][102], e[10001]; int x, y; Hash h2; int main() { init(); scanf("%d%d%d%d", &n, &m, &x, &y); map ma; for (int i = 1; i < n + 1; i++) { scanf("%d", w + i); for (int j = 1; j < x + 1; j++) { scanf("%s", s[i][j] + 1); for (int k = 1; k < y + 1; k++) h[i] = {(h[i][0] * 13331 + s[i][j][k]) % P, (h[i][1] * 131 + s[i][j][k]) % P}; } ma[h[i]] += i <= m ? 1 : -1; if (w[i]) { for (int j = 1; j < x + 1; j++) for (int k = 1; k < y + 1; k++) { Hash t = {(h[i][0] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p[x * y - (j - 1) * y - k] % P + P) % P, \ (h[i][1] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p2[x * y - (j - 1) * y - k] % P + P) % P}; ma[t] += i <= m ? 1 : -1; } } } int res = x * y > 20 || ma.size() != qmi(2, x * y, MOD) ? 0 : -n; Hash t = {-1, -1}; for (auto [a, b] : ma) res = max(res, b); printf("%d\n", res); for (auto [a, b] : ma) if (b == res) t = a; if (t[0] != -1) { for (int i = 1; i < n + 1; i++) { if (h[i] == t) { for (int j = 1; j < x + 1; j++) puts(s[i][j] + 1); return 0; } if (w[i]) { for (int j = 1; j < x + 1; j++) for (int k = 1; k < y + 1; k++) { Hash g = {(h[i][0] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p[x * y - (j - 1) * y - k] % P + P) % P, \ (h[i][1] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p2[x * y - (j - 1) * y - k] % P + P) % P}; if (t == g) { for (int l = 1; l < x + 1; l++) { for (int o = 1; o < y + 1; o++) putchar(j == l && k == o ? s[i][l][o] ^ '.' ^ '#' : s[i][l][o]); puts(""); } return 0; } } } } } else { for (int i = 1; i < x * y + 1; i++) h2 = {(h2[0] * 13331 + (e[i] = '.')) % P, (h2[1] * 131 + (e[i] = '.')) % P}; while (1) { if (!ma.count(h2)) { for (int i = 1; i < x + 1; i++) { for (int j = 1; j < y + 1; j++) putchar(e[(i - 1) * y + j]); puts(""); } return 0; } for (int i = x * y; i; i--) { h2 = {(h2[0] - e[i] * p[x * y - i] % P + P) % P, (h2[1] - e[i] * p2[x * y - i] % P + P) % P}; if (e[i] == '.') { e[i] = '#'; h2 = {(h2[0] + e[i] * p[x * y - i] % P + P) % P, (h2[1] + e[i] * p2[x * y - i] % P + P) % P}; break; } else e[i] = '.'; h2 = {(h2[0] + e[i] * p[x * y - i] % P + P) % P, (h2[1] + e[i] * p2[x * y - i] % P + P) % P}; } } } return 0; }