#include using namespace std; typedef long long int ll; typedef pair P; typedef vector VI; typedef vector VVI; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() template bool chmax(T &x, const T &y) {return (x bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;}; constexpr ll MOD=998244353; constexpr ll INF=2e18; int main(){ ll n, k; cin >> n >> k; ll m=n*2; vector s(m); REP(i,m) cin >> s[i]; VVI c(m,VI(m)); REP(i,m)REP(j,m) cin >> c[i][j]; ll cnt=0; REP(i,m)REP(j,m){ if(s[i][j]=='#') cnt++; } ll ans=-INF; if(cnt%2==0){ VI a; REP(i,m)REP(j,n){ a.push_back(c[i][j]+c[i][m-1-j]); } sort(ALL(a),greater()); ll sum=k; REP(i,cnt/2) sum+=a[i]; chmax(ans,sum); } VI b; REP(i,m)REP(j,m) b.push_back(c[i][j]); sort(ALL(b),greater()); ll sum=0; REP(i,cnt) sum+=b[i]; chmax(ans,sum); cout << ans << endl; return 0; }