#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], h[N]; void init() { p[0] = 1; for (int i = 1; i < N; i++) p[i] = p[i - 1] * 131 % P; } char s[101][102][102], e[10001]; int x, y; ll 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] * 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++) { ll t = (h[i] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p[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, t = -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 != -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++) { ll g = (h[i] + ((s[i][j][k] ^ '#' ^ '.') - s[i][j][k]) * p[x * y - (j - 1) * y - k] % P + P) % P; if (t == g) { for (int l = 1; l < x + 1; l++) puts(s[i][l] + 1); return 0; } } } } } else { for (int i = 1; i < x * y + 1; i++) h2 = (h2 * 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 - e[i] * p[x * y - i] % P + P) % P; if (e[i] == '.') { e[i] = '#'; h2 = (h2 + e[i] * p[x * y - i]) % P; break; } else e[i] = '.'; h2 = (h2 + e[i] * p[x * y - i]) % P; } } } return 0; }