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.flatten() a=o.sort() os=sum(a[:b]) if b%2==1: print(os) else: p=m[:,:n]+m[:,:n:2*n][::-1] q=p.flatten() r=q.sort() qs=sum(r[:b])+k print(max(qs,os))