#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++; } } } vector vec1(0); for (int i = 0; i < 2 * N; i++){ for (int j = 0; j < 2 * N; j++){ vec1.push_back(C[i][j]); } } sort(vec1.begin(), vec1.end()); reverse(vec1.begin(), vec1.end()); ll ans1 = 0; for (int i = 0; i < bk_count; i++){ ans1 += vec1[i]; } if (bk_count % 2 != 0){ cout << ans1 << endl; return 0; } vector vec2(0); for (int i = 0; i < 2 * N; i++){ for (int j = 0; j < N; j++){ vec2.push_back(C[i][j] + C[i][2 * N - 1 - j]); } } sort(vec2.begin(), vec2.end()); reverse(vec2.begin(), vec2.end()); ll ans2 = 0; for (int i = 0; i < bk_count / 2; i++){ ans2 += vec2[i]; } cout << max(ans1, ans2 + K) << endl; return 0; }