import numpy as np n,k=list(map(int,input().split())) s=[] c=[] for i in range(n*2): s.extend(input().split(".")) for j in range(n*2): c.append(list(map(int,input().split()))) b=s.count("#") m=np.array(c) o=m.ravel() o.sort() os=sum(o[:b]) if b%2==1: print(os) else: p=m[:,:n]+m[:,:n:2*n][::-1] q=p.ravel() q.sort() qs=sum(q[:b//2])+k print(max(qs,os))