#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; std::mt19937 RNG(std::chrono::system_clock::now().time_since_epoch().count()); unsigned long long RULL() { return std::uniform_int_distribution()(RNG); } using ull = unsigned long long; ull hs[100][100]; } int main() { ios::sync_with_stdio(false); cin.tie(0); rep(i, 100) rep(j, 100) hs[i][j] = RULL(); int n, u, h, w; cin >> n >> u >> h >> w; using A = array, 100>; static A s[100]{}; static int f[100]{}; struct S { signed char z, i, j, val; }; unordered_map d; rep(z, n) { cin >> f[z]; rep(i, h) rep(j, w) cin >> s[z][i][j]; signed char add = z < u ? 1 : -1; ull now = 0; rep(i, h) rep(j, w) now ^= (s[z][i][j] == '#') * hs[i][j]; auto [it, inserted] = d.emplace(now, S{z, -1, -1, add}); if (!inserted) it->second.val += add; if (f[z] == 1) { rep(i, h) rep(j, w) { auto [it, inserted] = d.emplace(now ^ hs[i][j], S{z, i, j, add}); if (!inserted) it->second.val += add; } } } int mx = -10000; A ans{}; auto dfs = [&](auto&& self, int i, int j, ull now) -> bool { if (j == w) j = 0, i++; if (i == h) { if (d.contains(now)) return false; mx = 0; return true; } ans[i][j] = '.'; if (self(self, i, j + 1, now)) return true; ans[i][j] = '#'; return self(self, i, j + 1, now ^ hs[i][j]); }; dfs(dfs, 0, 0, 0); S smx{-1, -1, -1, -1}; for (auto [_, v] : d) { if (chmax(mx, v.val)) smx = v; } if (smx.z != -1) { auto [z, i, j, val] = smx; ans = s[z]; if (i != -1) ans[i][j] ^= '.' ^ '#'; } cout << mx << '\n'; rep(i, h) { rep(j, w) cout << ans[i][j]; cout << '\n'; } }