#include using namespace std; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } int main() { fast_io(); int n; long long k; cin >> n >> k; vector s(2 * n); int cnt = 0; for (int i = 0; i < 2 * n; i++) { cin >> s[i]; for (char c : s[i]) { cnt += c == '#'; } } 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]; } } vector vals, vals_sym; for (int i = 0; i < 2 * n; i++) { for (int j = 0; j < 2 * n; j++) { vals.push_back(c[i][j]); } } for (int i = 0; i < 2 * n; i++) { for (int j = 0; j < n; j++) { vals_sym.push_back(c[i][j] + c[i][2 * n - j - 1]); } } sort(vals.rbegin(), vals.rend()); sort(vals_sym.rbegin(), vals_sym.rend()); long long ans = 0; for (int i = 0; i < cnt; i++) { ans += vals[i]; } if (cnt % 2 == 0) { long long tmp = k; for (int i = 0; i < cnt / 2; i++) { tmp += vals_sym[i]; } ans = max(ans, tmp); } cout << ans << endl; }