#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; for(int i = 0; i < U; i++){ int cnt = 0; for(int j = 0; j < N; j++){ cnt += (tb[i] ^ tb[j]).count() <= F[j] ? (j < U ? 1 : -1) : 0; } if(cnt > ans){ ans = cnt; S = tb[i]; } if(F[i] == 0) continue; for(int k = 0; k < H * W; k++){ tb[i].flip(k); cnt = 0; for(int j = 0; j < N; j++){ cnt += ((tb[i] ^ tb[j]).count() <= F[j] ? (j < U ? 1 : -1) : 0); } if(cnt > ans){ ans = cnt; 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); for(int i = 0; i < N; i++){ 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 = tb[i]; } } 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'; } }