import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        long k = sc.nextLong();
        int count = 0;
        for (int i = 0; i < 2 * n; i++) {
            for (char c : sc.next().toCharArray()) {
                if (c == '#') {
                    count++;
                }
            }
        }
        PriorityQueue<Integer> first = new PriorityQueue<>();
        int[][] values = new int[2 * n][2 * n];
        for (int i = 0; i < 2 * n; i++) {
            for (int j = 0; j < 2 * n; j++) {
                values[i][j] = sc.nextInt();
                first.add(-values[i][j]);
            }
        }
        long sum1 = 0;
        for (int i = 0; i < count; i++) {
            sum1 -= first.poll();
        }
        if (count % 2 == 1) {
            System.out.println(sum1);
            return;
        }
        PriorityQueue<Integer> second = new PriorityQueue<>();
        for (int i = 0; i < 2 * n; i++) {
            for (int j = 0; j < n; j++) {
                second.add(-(values[i][j] + values[i][2 * n - j - 1]));
            }
        }
        long sum2 = 0;
        for (int i = 0; i < count / 2; i++) {
            sum2 -= second.poll();
        }
        System.out.println(Math.max(sum1, sum2 + k));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}