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 static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		try {
			if (new File("input.txt").exists())
				System.setIn(new FileInputStream("input.txt"));
		} catch (SecurityException e) {
		}
		new Thread(null, new Runnable() {
			public void run() {
				try {
					new Main().run();
				} catch (Throwable e) {
					e.printStackTrace();
					exit(999);
				}
			}
		}, "1", 1 << 23).start();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st = new StringTokenizer("");

	int n, T;
	boolean f[][];
	int start[][];
	int end[][];
	
	Q qu;
	
	void get(int m[][]) throws IOException{
		for(int i = 0; i < n; i++){
			char s[] = nextToken().toCharArray();
			for(int j = 0; j < n; j++){
				switch(s[j]){
				case 'Z' : 
					if(qu.isEmpty())
						qu.push(i, j, 0);
					f[i][j] = false;
					break;
				case 'Y':
					f[i][j] = false;
					break;
				default:
					m[i][j] = s[j] - '0';
				}
			}
		}
	}
	int ttd[][]; //time to die
	boolean used[][];
	
	int dx[] = {-1, 0, 1 , 0};
	int dy[] = {0, 1, 0 , -1};

	int inf = -1;
	int cap[][];
	int fl[][];
	int m;
	
	void compute_ttd(){
		for(boolean i[] : used)
			fill(i, false);

		for(boolean i[] : used)
			fill(i, false);
		while(!qu.isEmpty()){
			
			P e = qu.pop();
			int x = e.x, y = e.y, t = e.t;
			ttd[x][y] = t;
			used[x][y] = true;
			for(int k = 0; k < 4; k++){
				int nx = x + dx[k];
				int ny = y + dy[k];
				if(valid(nx, ny)){
					used[nx][ny] = true;
					qu.push(nx, ny, t + 1);
				}
			}
		}
	}
	
	boolean valid(int x, int y){
		if(x < 0 || y < 0 || x >= n || y >= n || !f[x][y] || used[x][y])
			return false;
		return true;
	}
	
	List<Integer> g[];
	void setG(int vert){
		for(boolean i[] : used)
			fill(i, false);

		while(!qu.isEmpty()){
			P e = qu.pop();
			int x = e.x, y = e.y, t = e.t;
			used[x][y] = true;
			
			if(t <= ttd[x][y] && t <= T){
				cap[vert][m + 2 + x * n + y] = inf;
				g[vert].add(m + 2 + x * n + y);
				g[m + 2 + x * n + y].add(vert);
			}
			if(t >= ttd[x][y] || t >= T)
				continue;
			for(int k = 0; k < 4; k++)
				if(valid(x + dx[k], y + dy[k])){
					used[x + dx[k]][y + dy[k]] = true;
					qu.push(x + dx[k], y + dy[k], t + 1);
				}
		}
	}
	int path[];
	boolean lu[];
	QL ql;
	boolean find_path(int start, int end){
		fill(lu, false);

		ql.clear();
		ql.push(start);
		lu[start] = true;
		while(!ql.isEmpty()){
			int v = ql.pop();
			if(v == end)
				return true;
			for(int e : g[v]){
				if(lu[e])
					continue;
				if(cap[v][e] > fl[v][e]){
					lu[e] = true;
					path[e] = v;
					ql.push(e);
				}
			}
		}
		return false;
	}
	
	private void run() throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		
		n = nextInt();
		T = nextInt();
		
		inf = n * n + n + 100;
		
		
		f = new boolean[n][n];
		start = new int[n][n];
		end = new int[n][n];
		for(boolean []i : f)
			fill(i, true);
		
		qu = new Q(n * n + 1);//
		
		get(start);
		get(end);
		
		ttd = new int[n][n];
		used = new boolean[n][n];
		
		for(int i[] : ttd)
			fill(i, inf << 1);
		
		compute_ttd();

		m = n * n; 
		
		cap = new int[m + m + 2][m + m + 2];
		fl = new int[m + m + 2][m + m + 2];
		
		g = new ArrayList[m + m + 2];
		for(int i = 0; i < m + m + 2; i++)
			g[i] = new ArrayList<Integer>();
		
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++){
				qu.clear();
				qu.push(i, j, 0);
				setG(n * i + j);
				cap[n * i + j][n * i + j] = inf;
			}
		
//		for(int i = 0; i < n; i++)
//			System.err.println(Arrays.toString(ttd[i]));
//		for(int i = 0; i < m; i++)
//			System.err.println(Arrays.toString(cap[i]));
//
//		for(int ii = 0; ii < n; ii++)
//			for(int jj = 0; jj < n; jj++)
//		for(int i = 0; i < n; i++)
//			for(int j = 0; j < n; j++)
//						if(cap[ii * n + jj][i * n + j] != 0)
//							System.err.println(ii + " " + jj + " | " + i + " " + j);

		// m - from   m+1 - to
		for(int i = 0; i < n; i++)
			for(int j = 0; j < n; j++){
				cap[m][i * n + j] = start[i][j];
				g[m].add(i * n + j);
				cap[m + 2 + i * n + j][m+1] = end[i][j];
				g[m + 2 + i * n + j].add(m + 1);
			}
				
		path = new int[m + m + 2];
		lu = new boolean[m + m + 2];
		ql = new QL(m + m + 2);
		int ans = 0;
		
		while(true){
			if(!find_path(m , m + 1)){
				break;
			}
			
			int mf = inf;
			{
				int i = m + 1;
				do{
					int j = path[i];
					mf = min(mf, cap[j][i] - fl[j][i]);
					i = j;
				}while(i != m);
			}
			ans += mf;
			{
				int i = m + 1;
				do{
					int j = path[i];
					fl[j][i] += mf;
					fl[i][j] -= mf;
					i = j;
				}while(i != m);
			}
		}
		
		out.println(ans);
		in.close();
		out.close();
	}

	class Q{
		P m[];
		int h, t;
		Q(int size){
			m = new P[size];
			for(int i = 0; i < size; i++)
				m[i] = new P(0,0,0);
			h = t = 0;
		}
		void clear(){
			h = t = 0;
		}
		void push(int x, int y, int tt){
//			System.err.println(x + " " + y);
			m[t++].set(x, y, tt);
		}
		void push(P e){
			m[t++].set(e);
		}
		P pop(){
			return m[h++];
		}
		boolean isEmpty(){
			return h == t;
		}
	}
	class QL{
		int m[];
		int h, t;
		QL(int size){
			m = new int[size];
			h = t = 0;
		}
		void push(int x){
//			System.err.println(x);
			m[t++] = x;
		}
		int pop(){
			return m[h++];
		}
		boolean isEmpty(){
			return h == t;
		}
		void clear(){
			h = t = 0;
		}
	}
	class P{
		int x, y, t;
		P(int xx, int yy, int tt){
			x = xx;
			y = yy;
			t = tt;
		}
		void set(int xx, int yy, int tt){
			x = xx;
			y = yy;
			t = tt;
		}
		void set(P e){
			x = e.x;
			y = e.y;
			t = e.t;
		}
		public String toString(){
			return x + " " + y  + " " + t + " |";
		}
	}
	void chk(boolean b) {
		if (b)
			return;
		System.out.println(new Error().getStackTrace()[1]);
		exit(999);
	}
	void deb(String fmt, Object... args) {
		System.out.printf(Locale.US, fmt + "%n", args);
	}
	String nextToken() throws IOException {
		while (!st.hasMoreTokens())
			st = new StringTokenizer(in.readLine());
		return st.nextToken();
	}
	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}
	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}
	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
	String nextLine() throws IOException {
		st = new StringTokenizer("");
		return in.readLine();
	}
	boolean EOF() throws IOException {
		while (!st.hasMoreTokens()) {
			String s = in.readLine();
			if (s == null)
				return true;
			st = new StringTokenizer(s);
		}
		return false;
	}
}