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:
//package round70;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Scanner;

public class E {
	Scanner in;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		int t = ni();
		char[][] map = new char[n][n]; // initial map
		char[][][] dmap = new char[t+1][n][n]; // dead map
		char[][] hmap = new char[n][n]; // hazard map
		int ct = 0, cct = 0;
		for(int i = 0;i < n;i++){
			map[i] = in.next().toCharArray();
			dmap[0][i] = Arrays.copyOf(map[i], n);
			for(int j = 0;j < n;j++){
				if(map[i][j] >= '1' && map[i][j] <= '9'){
					ct += map[i][j] - '0';
				}
			}
		}
		for(int i = 0;i < n;i++){
			hmap[i] = in.next().toCharArray();
			for(int j = 0;j < n;j++){
				if(hmap[i][j] >= '1' && hmap[i][j] <= '9'){
					cct += hmap[i][j] - '0';
				}
			}
		}
		
		int[] dr = {1,0,-1,0,0};
		int[] dc = {0,1,0,-1,0};
		for(int i = 1;i <= t;i++){
			for(int j = 0;j < n;j++){
				for(int k = 0;k < n;k++){
					if(dmap[i-1][j][k] == 'Z'){
						for(int l = 0;l < 5;l++){
							int nr = j + dr[l];
							int nc = k + dc[l];
							if(nr >= 0 && nr < n && nc >= 0 && nc < n && map[nr][nc] != 'Y'){
								dmap[i][nr][nc] = 'Z';
							}
						}
					}
				}
			}
//			tr(dmap[i]);
		}
		
		boolean[][] g = new boolean[n*n][n*n];
		BitSet[][][] alive = new BitSet[2][n*n][n*n];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				alive[0][i][j] = new BitSet();
				alive[1][i][j] = new BitSet();
				if(map[i][j] >= '1' && map[i][j] <= '9'){
					alive[0][i][j].set(i*n+j);
				}
			}
		}
		
//		for(int i = 0;i < n;i++){
//			for(int j = 0;j < n;j++){
//				if(hmap[i][j] >= '1' && hmap[i][j] <= '9'){
//					for(int l = alive[0][i][j].nextSetBit(0);l != -1;l = alive[0][i][j]
//							.nextSetBit(l + 1)){
//						g[l][i*n+j] = true;
//					}
//				}
//			}
//		}
		for(int k = 0, w = 0;k < t;k++, w^=1){
			for(int i = 0;i < n;i++){
				for(int j = 0;j < n;j++){
					alive[w^1][i][j].clear();
				}
			}
			for(int i = 0;i < n;i++){
				for(int j = 0;j < n;j++){
					if(k == 0 || dmap[k][i][j] != 'Z'){
						for(int l = 0;l < 5;l++){
							int nr = i + dr[l];
							int nc = j + dc[l];
							if(nr >= 0 && nr < n && nc >= 0 && nc < n && map[nr][nc] != 'Y' && dmap[k][nr][nc] != 'Z'){
								alive[w^1][nr][nc].or(alive[w][i][j]);
//								if(alive[w^1][nr][nc].get(8)){
//									tr(k, nr, nc);
//								}
							}
						}
					}
				}
			}
			for(int i = 0;i < n;i++){
				for(int j = 0;j < n;j++){
					if(hmap[i][j] >= '1' && hmap[i][j] <= '9'){
						for(int l = alive[w^1][i][j].nextSetBit(0);l != -1;l = alive[w^1][i][j]
								.nextSetBit(l + 1)){
							g[l][i*n+j] = true;
						}
					}
				}
			}
		}
//		for(int i = 0;i < n*n;i++){
//			tr(g[i]);
//		}
		
		int[] indc = new int[n*n+1];
		int[] inds = new int[n*n+1];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				indc[i*n+j+1] = indc[i*n+j] + (hmap[i][j] >= '1' && hmap[i][j] <= '9' ? hmap[i][j]-'0' : 0);
				inds[i*n+j+1] = inds[i*n+j] + (map[i][j] >= '1' && map[i][j] <= '9' ? map[i][j]-'0' : 0);
			}
		}
		
		boolean[][] lg = new boolean[ct][cct];
		for(int i = 0;i < n*n;i++){
			for(int j = 0;j < n*n;j++){
				if(g[i][j]){
					for(int k = inds[i];k < inds[i+1];k++){
						for(int l = indc[j];l < indc[j+1];l++){
							lg[k][l] = true;
						}
					}
				}
			}
		}
//		tr(lg);
		
		out.println(doBipartiteMatching(lg));
	}
	
	public static int doBipartiteMatching(boolean[][] g)
	{
		if(g.length == 0)return 0;
		if(g[0].length == 0)return 0;
		
		int n = g.length;
		int m = g[0].length;
		int[] im = new int[m];
		Arrays.fill(im, -1);
		for(int i = 0;i < n;i++){
			BitSet visited = new BitSet();
			visit(g, i, visited, im);
		}

		int ct = 0;
		for(int i = 0;i < m;i++){
			if(im[i] != -1)ct++;
		}
		return ct;
	}

	public static boolean visit(boolean[][] g, int cur, BitSet visited, int[] im)
	{
		if(cur == -1)return true;

		for(int i = visited.nextClearBit(0);i != g[cur].length;i = visited.nextClearBit(i+1)){
			if(g[cur][i]){
				visited.set(i);
				if(visit(g, im[i], visited, im)){
					im[i] = cur;
					return true;
				}
			}
		}
		return false;
	}
	
	void run() throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);

		solve();
		out.flush();
	}
	
	
	public static void main(String[] args) throws Exception
	{
		new E().run();
	}
	
	int ni() { return Integer.parseInt(in.next()); }
	double nd() { return Double.parseDouble(in.next()); }
	void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}