n, k = map(int, input().split()) s = [input() for _ in range(2 * n)] c = [list(map(int, input().split())) for _ in range(2 * n)] cnt = 0 xs = [] for x in range(2 * n): for y in range(2 * n): if s[x][y] == "#": cnt += 1 xs.append(c[x][y]) if cnt == 0: print(k) exit() xs.sort() ans = sum(xs[- cnt:]) if cnt % 2 != 0: print(ans) exit() xs = [] for x in range(2 * n): for y in range(n): xs.append(c[x][y] + c[x][2 * n - 1 - y]) xs.sort() ans = max(ans, sum(xs[- (cnt // 2):]) + k) print(ans)