#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n,k;
  cin>>n>>k;
  vector<string> S(2*n);
  rep(i,2*n) cin>>S[i];
  vector<vector<ll>> C(2*n,vector<ll>(2*n));
  vector<ll> A;
  int cnt=0;
  rep(i,2*n) rep(j,2*n){
    cin>>C[i][j];
    A.push_back(C[i][j]);
    if(S[i][j]=='#') cnt++;
  }
  sort(ALL(A));
  reverse(ALL(A));
  
  ll ans=0;
  rep(i,cnt) ans+=A[i];
  
  if(cnt%2==0){
    vector<ll> B;
    rep(i,2*n) rep(j,n) B.push_back(C[i][j]+C[i][2*n-1-j]);
    sort(ALL(B));
    reverse(ALL(B));
    ll tmp=k;
    rep(i,cnt/2) tmp+=B[i];
    ans=max(ans,tmp);
  }
  cout<<ans<<endl;
  
  return 0;
}