n, k = map(int, input().split()) x = 0; a = []; b = [] for _ in range(2*n): for v in input(): if v == "#": x += 1 for _ in range(2*n): c = list(map(int, input().split())) for v in c: a.append(v) for i in range(n): b.append(c[i]+c[-1-i]) ans = 0 if x%2 else sum(sorted(b)[-x//2:])+k print(max(ans, sum(sorted(a)[-x:])))