import sys input=lambda:sys.stdin.readline().rstrip() N,K=map(int,input().split()) black=0 A=[0 for i in range(4*N**2)] B=[0 for i in range(2*N**2)] for i in range(2*N): black+=input().count('#') for i in range(2*N): temp=list(map(int,input().split())) for j in range(2*N): A[i*2*N+j]=temp[j] for j in range(N): B[i*N+j]=temp[j]+temp[2*N-1-j] A=sorted(A,key=lambda x:-x) B=sorted(B,key=lambda x:-x) ans=sum(A[:black]) if black%2==0: ans=max(ans,sum(B[:black//2])+K) print(ans)