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