Problem: They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.

The station is represented by a square n × n divided into 1 × 1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.

In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.

The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.

There are exactly t minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.

Find the maximum number of scientists who will be able to escape.

Input

The first line contains two integers n and t (2 ≤ n ≤ 10, 1 ≤ t ≤ 60). Each of the next n lines contains n characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next n more lines contains n characters. These lines describe the rescue capsules' locations.

In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory.

Output

Print a single number — the maximum number of scientists who will manage to save themselves.

Examples

Input

3 3
1YZ
1YY
100

0YZ
0YY
003


Output

2

Input

4 4
Y110
1Y1Z
1Y0Y
0100

Y001
0Y0Z
0Y0Y
0005


Output

3

Note

In the second sample the events could take place as follows: 

<image>

Solution (in JAVA):

Solution:
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
import java.util.*;

public class Solution implements Runnable{
    public static BufferedReader br;
    public static PrintWriter out;
    public static StringTokenizer stk;
    public static boolean isStream = true;

    public static void main(String[] args) throws IOException {
    	if (isStream) {
            br = new BufferedReader(new InputStreamReader(System.in));
        } else {
            br = new BufferedReader(new FileReader("in.txt"));
        }
        out = new PrintWriter(System.out);
        new Thread(new Solution()).start();
    }

    public void loadLine() {
        try {
            stk = new StringTokenizer(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String nextLine() {
        try {
            return br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public String nextWord() {
        while (stk==null||!stk.hasMoreTokens()) loadLine();
        return stk.nextToken();
    }

    public Integer nextInt() {
        while (stk==null||!stk.hasMoreTokens()) loadLine();
        return Integer.valueOf(stk.nextToken());
    }

    public Long nextLong() {
        while (stk==null||!stk.hasMoreTokens()) loadLine();
        return Long.valueOf(stk.nextToken());
    }

    public Double nextDouble() {
        while (stk==null||!stk.hasMoreTokens()) loadLine();
        return Double.valueOf(stk.nextToken());
    }
    
    public Float nextFloat() {
        while (stk==null||!stk.hasMoreTokens()) loadLine();
        return Float.valueOf(stk.nextToken());
    }
    
    int n;
    int t;
    int[][][] isBad;
    int[][][][][] can;
    
    int[] dx = new int[] {-1,1,0,0};
    int[] dy = new int[] {0,0,1,-1};
    
    int[][] map, flow;
    
    int INF = 1000000;
    
    int findFlow() {
    	int m = n*n*2+2;
    	int[] from = new int[m];
    	while (true) {
    		Arrays.fill(from, -1);
    		from[m-2] = -2;
	    	LinkedList<Integer> list = new LinkedList<Integer>();
	    	list.addFirst(m-2);
	    	while (list.size() != 0) {
	    		int cur = list.removeLast();
	    		for (int next = 0; next < m; next++) {
	    			if (map[cur][next] != -1 && from[next] == -1 && map[cur][next]-flow[cur][next] > 0) {
	    				from[next] = cur;
	    				list.addFirst(next);
	    			}
	    		}
	    	}
	    	if (from[m-1] != -1) {
	    		int min = Integer.MAX_VALUE;
	    		int pos = m-1;
	    		while (from[pos] != -2) {
	    			min = Math.min(min, map[from[pos]][pos]-flow[from[pos]][pos]);
	    			pos = from[pos];
	    		}
	    		pos = m-1;
	    		while (from[pos] != -2) {
	    			flow[from[pos]][pos] += min;
	    			flow[pos][from[pos]] -= min;
	    			pos = from[pos];
	    		}
	    	} else {
	    		break;
	    	}
    	}
    	int ans = 0;
    	for (int i = 0; i < m; i++) {
    		if (map[i][m-1] != -1) {
    			ans += flow[i][m-1];
    		}
    	}
    	return ans;
    }
    
    public void run() {
    	n = nextInt();
    	t = nextInt();
    	String[] ss1 = new String[n];
    	for (int i = 0; i < n; i++) {
    		ss1[i] = nextWord();
    	}
    	String[] ss2 = new String[n];
    	for (int i = 0; i < n; i++) {
    		ss2[i] = nextWord();
    	}
    	
    	isBad = new int[t+1][n][n];
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
        		if (ss1[i].charAt(j) == 'Y') {
        			isBad[0][i][j] = 1;
        		}
        		if (ss1[i].charAt(j) == 'Z') {
        			isBad[0][i][j] = 2;
        		}
        	}	
    	}
    	for (int tt = 1; tt <= t; tt++) {
    		for (int i = 0; i < n; i++) {
        		for (int j = 0; j < n; j++) {
        			isBad[tt][i][j] = isBad[tt-1][i][j];
        			if (isBad[tt][i][j] == 0) {
	            		for (int d = 0; d < 4; ++d) {
	            			if (i+dx[d] >= 0 && i+dx[d] < n && j+dy[d] >= 0 && j+dy[d] < n) {
		            			if (isBad[tt-1][i+dx[d]][j+dy[d]] == 2) {
		            				isBad[tt][i][j] = 2;
		            			}
	            			}
	            		}
        			}
            	}	
        	}
    	}
    	
    	can = new int[n][n][t+1][n][n];
    	for (int si = 0; si < n; si++) {
    		for (int sj = 0; sj < n; sj++) {
        		if (ss1[si].charAt(sj) >= '1' && ss1[si].charAt(sj) <= '9') {
        			can[si][sj][0][si][sj] = 1;
        			for (int tt = 1; tt <= t; tt++) {
        	    		for (int i = 0; i < n; i++) {
        	        		for (int j = 0; j < n; j++) {
        	        			if (isBad[tt-1][i][j] == 0) {
        		            		for (int d = 0; d < 4; ++d) {
        		            			if (i+dx[d] >= 0 && i+dx[d] < n && j+dy[d] >= 0 && j+dy[d] < n) {
	        		            			if (can[si][sj][tt-1][i+dx[d]][j+dy[d]] == 1 && isBad[tt-1][i+dx[d]][j+dy[d]] == 0) {
	        		            				can[si][sj][tt][i][j] = 1;
	        		            			}
        		            			}
        		            		}
        	        			}
        	            	}	
        	        	}
        	    	}
        		}
        	}
    	}
    	
    	map = new int[2*n*n+2][2*n*n+2];
    	flow = new int[2*n*n+2][2*n*n+2];
    	for (int i = 0; i < map.length; i++) {
    		Arrays.fill(map[i], -1);
    	}
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
        		if (ss1[i].charAt(j) >= '1' && ss1[i].charAt(j) <= '9') {
        			for (int ri = 0; ri < n; ri++) {
        	    		for (int rj = 0; rj < n; rj++) {
        	    			if (ss2[ri].charAt(rj) >= '1' && ss2[ri].charAt(rj) <= '9') {
	        	    			boolean f = false;
	        	    			for (int tt = 0; tt <= t; tt++) {
	        	    				if (can[i][j][tt][ri][rj] == 1) {
	        	    					f = true;
	        	    				}
	        	    			}
	        	        		if (f) {
	        	        			map[i*n+j][n*n+ri*n+rj] = INF;
	        	        			map[n*n+ri*n+rj][i*n+j] = 0;
	        	        		}
        	    			}
        	        	}	
        	    	}
        		}
        	}	
    	}
    	
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
        		if (ss1[i].charAt(j) >= '1' && ss1[i].charAt(j) <= '9') {
        			map[n*n*2][i*n+j] = ss1[i].charAt(j) - '0';
        			map[i*n+j][n*n*2] = 0;
        		}
    		}
    	}
    	
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
        		if (ss2[i].charAt(j) >= '1' && ss2[i].charAt(j) <= '9') {
        			map[n*n+i*n+j][n*n*2+1] = ss2[i].charAt(j) - '0';
        			map[n*n*2+1][n*n+i*n+j] = 0;
        		}
    		}
    	}
    	
    	out.println(findFlow());
    	
    	out.flush();
    }
}