#include #include using namespace std; typedef long long ll; int main() { int n; ll k; cin >> n >> k; n *= 2; string s[505]; for (int i = 0; i < n; i++) { cin >> s[i]; } int c[505][505]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> c[i][j]; } } int x = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { x += (s[i][j] == '#'); } } int d[250005], e[130000]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i * n + j] = c[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n / 2; j++) { e[i * n / 2 + j] = c[i][j] + c[i][n - 1 - j]; } } sort(d, d + n * n, greater()); sort(e, e + n * n / 2, greater()); ll ans = 0; for (int i = 0; i < x; i++) { ans += d[i]; } if (x % 2 == 0) { ll s = k; for (int i = 0; i < x / 2; i++) { s += e[i]; } ans = max(ans, s); } cout << ans << endl; }