n,k = map(int,input().split()) S = [input() for i in range(2*n)] C = [list(map(int,input().split())) for i in range(2*n)] num = 0 for s in S: num += s.count("#") ans = -10**20 sC = [] for c in C: for i in c: sC.append(i) sC.sort() ans = max(ans,sum(sC[-num:])) if num%2 == 0: sC = [] for c in C: for i in range(n): cal = c[i]+c[-i-1] sC.append(cal) sC.sort() ans = max(ans,sum(sC[-num//2:])+k) print(ans)