N,K = map(int,input().split()) S = [input().strip() for _ in range(2*N)] C = [list(map(int,input().split())) for _ in range(2*N)] num = 0 A = [] for i in range(2*N): for j in range(2*N): if S[i][j]=="#": num += 1 A.append(C[i][j]) A = sorted(A,reverse=True) ans = sum(A[:num]) if num%2==0: B = [] for i in range(2*N): for j in range(N): B.append(C[i][j]+C[i][2*N-1-j]) B = sorted(B,reverse=True) ans = max(ans,sum(B[:num//2])+K) print(ans)