#include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    ll K;
    cin >> K;
    int N2 = N * 2;
    V<string> S(N2);
    rep(i, N2) cin >> S[i];
    V<V<ll>> C(N2, V<ll>(N2));
    rep(i, N2) rep(j, N2) cin >> C[i][j];
    int cnt = 0;
    rep(i, N2) rep(j, N2) cnt += S[i][j] == '#';
    V<ll> B;
    rep(i, N2) rep(j, N2) B.push_back(C[i][j]);
    sort(B.rbegin(), B.rend());
    ll ans = accumulate(B.begin(), B.begin() + cnt, 0LL);
    show(ans);
    if (cnt % 2 != 0) {
        cout << ans << '\n';
        return 0;
    }
    // symmetry
    V<ll> D;
    rep(i, N2) rep(j, N) D.push_back(C[i][j] + C[i][N2 - 1 - j]);
    sort(D.rbegin(), D.rend());
    ans = max(ans, K + accumulate(D.begin(), D.begin() + cnt / 2, 0LL));
    cout << ans << '\n';
    return 0;
}