import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Collections; import java.util.PriorityQueue; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); long k = Long.parseLong(sa[1]); int n2 = n * 2; char[][] s = new char[n2][n2]; for (int i = 0; i < n2; i++) { s[i] = br.readLine().toCharArray(); } int[][] c = new int[n2][n2]; for (int i = 0; i < n2; i++) { sa = br.readLine().split(" "); for (int j = 0; j < n2; j++) { c[i][j] = Integer.parseInt(sa[j]); } } br.close(); int b = 0; for (int i = 0; i < n2; i++) { for (int j = 0; j < n2; j++) { if (s[i][j] == '#') { b++; } } } PriorityQueue que1 = new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue que2 = new PriorityQueue<>(Collections.reverseOrder()); for (int i = 0; i < n2; i++) { for (int j = 0; j < n2; j++) { que1.add(c[i][j]); } for (int j = 0; j < n; j++) { que2.add(c[i][j] + c[i][n2 - 1 - j]); } } long ans = 0; for (int i = 0; i < b; i++) { ans += que1.poll(); } if (b % 2 == 0) { int b2 = b / 2; long val = 0; for (int i = 0; i < b2; i++) { val += que2.poll(); } ans = Math.max(ans, val + k); } System.out.println(ans); } }