import sys input = sys.stdin.readline n, k = map(int, input().split()) S = [list(input()) for _ in range(2 * n)] C = [list(map(int, input().split())) for _ in range(2 * n)] num = 0 L = [] for i in range(2 * n): for j in range(2 * n): L.append(C[i][j]) if S[i][j] == '#': num += 1 L.sort(reverse=True) ans = sum(L[:num]) if num > 0 and num % 2 == 0: LL = [] for i in range(2 * n): for j in range(n): LL.append(C[i][j] + C[i][2 * n - j - 1]) LL.sort(reverse=True) ans = max(ans, sum(LL[:num // 2]) + k) print(ans)