#include using namespace std; using ll = long long; using T = bitset<10000>; int main() { ios::sync_with_stdio(false); cin.tie(0); string str; int N, U, H, W, v; cin >> N >> U >> H >> W; vector F(N); vector tb(N); for(int i = 0; i < N; i++){ cin >> F[i]; for(int y = 0; y < H; y++){ cin >> str; for(int x = 0; x < W; x++){ if(str[x] == '#') tb[i][y * W + x] = 1; } } } int ans = -(N + 1); T S; vector change(H * W); for(int i = 0; i < N; i++){ int cnt = 0; fill(change.begin(), change.end(), 0); for(int j = 0; j < N; j++){ auto df = tb[i] ^ tb[j]; int cnt2 = df.count(); cnt += cnt2 <= F[j] ? (j < U ? 1 : -1) : 0; for(int k = 0; k < H * W; k++){ int cnt3 = cnt2 + (df[k] ? -1 : 1); change[k] += cnt3 <= F[j] ? (j < U ? 1 : -1) : 0; } } if(cnt > ans){ ans = cnt; S = tb[i]; } for(int k = 0; k < H * W; k++){ tb[i].flip(k); if(change[k] > ans){ ans = change[k]; S = tb[i]; } tb[i].flip(k); } } if(ans < 0){ int cur = 0, th = 1 << 20; if(H * W < 20) th = 1 << H * W; while(cur < th){ T S2(cur); int cnt = 0; for(int j = 0; j < N; j++){ cnt += (S2 ^ tb[j]).count() <= F[j] ? (j < U ? 1 : -1) : 0; } if(cnt > ans){ ans = cnt; S = S2; } if(ans >= 0) break; cur++; } } cout << ans << '\n'; for(int y = 0; y < H; y++){ for(int x = 0; x < W; x++){ cout << (S[y * W + x] ? '#' : '.'); } cout << '\n'; } }