N,K = map(int,input().split()) S = [input() for _ in range(N + N)] C = [list(map(int,input().split())) for _ in range(N + N)] black = 0 for s in S: for u in s: if u == "#": black += 1 l = [] for c in C: for u in c: l.append(u) l.sort(reverse = True) ans = sum(l[:black]) ans2 = K ll = [] for i in range(N + N): for j in range(N): ll.append(C[i][j] + C[i][2 * N - 1 - j]) ll.sort(reverse = True) if black % 2 == 0: ans2 += sum(ll[:black // 2]) if ans2 > ans: ans = ans2 print(ans)