#include #include #include using namespace std; typedef long long ll; int main(){ int N; ll K; cin >> N >> K; vector S(2 * N); for (int i = 0; i < 2 * N; i++) cin >> S[i]; vector> C(2 * N, vector(2 * N)); for (int i = 0; i < 2 * N; i++){ for (int j = 0; j < 2 * N; j++){ cin >> C[i][j]; } } int bk_count = 0; for (int i = 0; i < 2 * N; i++){ for (int j = 0; j < 2 * N; j++){ if (S[i][j] == '#'){ bk_count++; } } } ll INF = 100000000000000000; ll ans = -INF; for (int i = 0; i < (1 << 4 * N * N); i++){ int pp = 0; for (int j = 0; j < 4 * N * N; j++){ if ((i >> j) & 1){ pp++; } } if (pp != bk_count){ continue; } else{ ll score = 0; bool symmetry = true; for (int j = 0; j < 4 * N * N; j++){ int x = j / (2 * N); int y = j % (2 * N); int p = (x * 2 * N + (2 * N - 1 - y)); if ((i >> j) & 1){ if (!((i >> p) & 1)){ symmetry = false; } } } for (int j = 0; j < 4 * N * N; j++){ int x = j / (2 * N); int y = j % (2 * N); if ((i >> j) & 1){ score += C[x][y]; } } if (symmetry) score += K; ans = max(ans, score); } } cout << ans << endl; }