#include int main() { using namespace std; unsigned long N, K; cin >> N >> K; vector s(4 * N * N); for(unsigned long i{}; i < 2 * N; ++i){ string S; cin >> S; transform(begin(S), end(S), begin(s) + 2 * i * N, [](auto c){return c == '#';}); } unsigned long S{reduce(begin(s), end(s))}; vector C(4 * N * N); for(auto&& c : C)cin >> c; vector sorted_c(C); sort(begin(sorted_c), end(sorted_c), greater<>{}); long ans{reduce(begin(sorted_c), begin(sorted_c) + S)}; if(~S & 1){ vector symmetric_c(2 * N * N); for(unsigned long i{}; i < 2 * N; ++i)for(unsigned long j{}; j < N; ++j)symmetric_c[i * N + j] = C[2 * i * N + j] + C[2 * i * N + 2 * N - 1 - j]; sort(begin(symmetric_c), end(symmetric_c), greater<>{}); ans = max(ans, reduce(begin(symmetric_c), begin(symmetric_c) + S / 2) + K); } cout << ans << endl; return 0; }