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.util.*;
import java.io.*;

public class Main{

    BufferedReader in;
    StringTokenizer str = null;
    PrintWriter out;

    private String next() throws Exception{
    	while (str == null || !str.hasMoreElements())
    	    str = new StringTokenizer(in.readLine());
    	return str.nextToken();
    }
    
    private int nextInt() throws Exception{
		return Integer.parseInt(next());
    }
    
    char [][]sc, map;
    int n, t;

    int [][]dr;
    Queue<Integer> q;

    int di[] = new int[]{-1, 1, 0, 0};
    int dj[] = new int[]{0, 0, -1, 1};

    MaxFlowMinCost mfmc;

    final static int BASE = 10;

    public void run() throws Exception{
    	in = new BufferedReader(new InputStreamReader(System.in));
    	out = new PrintWriter(System.out);

    	n = nextInt();
    	t = nextInt();
    	sc = new char[n][];
    	for (int i = 0; i < n ; ++i) {
    		sc[i] = next().toCharArray();
    	}

    	map = new char[n][];
    	for (int i = 0; i < n; ++i) {
    		map[i] = next().toCharArray();
    	}

    	dr = new int[n][n];
    	for(int []i : dr) Arrays.fill(i, oo);
    	q = new ArrayDeque<Integer>();

    	for(int i = 0; i < n; ++i) {
    		for(int j = 0; j < n; ++j) {
    			if (map[i][j] == 'Z') {
    				dr[i][j] = 0;
    				q.add(i * BASE + j);
    			}
    		}
    	}

    	while(q.size() > 0) {
    		int cur = q.poll();
    		int x = cur / BASE, y = cur % BASE;

    		for(int i = 0; i < 4; ++i) {
    			int nx = x + di[i], ny = y + dj[i];
    			add(nx, ny, x, y, dr);
    		}
    	}

    	int source = 2 * n * n, sink = 2 * n * n + 1;
    	mfmc = new MaxFlowMinCost(2 * n * n + 2, source, sink);

    	for(int i = 0; i < n; ++i) {
    		for(int j = 0; j < n; ++j) {
    			if (Character.isDigit(sc[i][j])) {
    				int flow = sc[i][j] - '0', v = i * n + j;
    				if (flow > 0) {
    					mfmc.addEdge(source, v, flow);
    				}
    			}

    			if (Character.isDigit(map[i][j])) {
    				int flow = map[i][j] - '0', v = n * n + i * n + j;
    				if (flow > 0) {
    					mfmc.addEdge(v, sink, flow);
    				}	
    			}

    			if (Character.isDigit(sc[i][j])) {
    				int count = sc[i][j] - '0';
    				if (count == 0) continue;
    				int [][]d = bfs(i, j, dr);
    				// System.out.println(i + " " + j);
    				// print(d);
    				for(int x = 0; x < n; ++x) {
    					for(int y = 0; y < n; ++y) {
    						if (Character.isDigit(map[x][y])) {
    							count = map[x][y] - '0';
    							if (count == 0) continue;
    							if (d[x][y] <= t) {
    								int v = i * n + j, u = n * n + x * n + y;
    								mfmc.addEdge(v, u, oo);	
    							}
    						}
    					}
    				}
    			}
    		}
    	}

    	// mfmc.print();

    	mfmc.execute();

    	out.println(mfmc.flow);
        out.close();
    }

    private void print(int [][]d) {
    	for(int i = 0; i < n; ++i) {
    		System.out.println(Arrays.toString(d[i]));
    	}
    	System.out.println();
    }

    private int[][] bfs(int sx, int sy, int [][]dr) {
    	int [][]d = new int[n][n];
    	for(int []i : d) Arrays.fill(i, oo);

    	d[sx][sy] = 0;
    	q.add(sx * BASE + sy);

    	while(q.size() > 0) {
    		int cur = q.poll();
    		int x = cur / BASE, y = cur % BASE;

    		for(int i = 0; i < 4; ++i) {
    			int nx = x + di[i], ny = y + dj[i];
    			add(nx, ny, x, y, d, dr);
    		}
    	}

    	return d;
    }

    private void add(int x, int y, int fx, int fy, int [][]d, int [][]dr) {
    	if (x < 0 || y < 0) return;
    	if (x >= n || y >= n) return;
    	if (map[x][y] == 'Y' || map[x][y] == 'Z') return;
    	if (d[x][y] > d[fx][fy] + 1) {
    		if (dr[x][y] >= d[fx][fy] + 1) {
    			d[x][y] = d[fx][fy] + 1;
    			if (dr[x][y] > d[fx][fy] + 1) {
    				q.add(x * BASE + y);		
    			}
    		}
    	} 
    }

    private void add(int x, int y, int fx, int fy, int [][]d) {
    	if (x < 0 || y < 0) return;
    	if (x >= n || y >= n) return;
    	if (map[x][y] == 'Y') return;
    	if (d[x][y] > d[fx][fy] + 1) {
    		d[x][y] = d[fx][fy] + 1;
    		q.add(x * BASE + y);
    	}
    }

    public static void main(String args[]) throws Exception{
		new Main().run();
    }

    final static int oo = Integer.MAX_VALUE/2;

    class MaxFlowMinCost {

    	class Edge {
    		int from, to, cap, flow;

    		public Edge(int from, int to, int cap, int flow) {
    			this.from = from;
    			this.to = to;
    			this.cap = cap;
    			this.flow = flow;
    		}

    		public String toString() {
    			return "from=" + from + ", to=" + to + ", cap=" + cap + ", flow=" + flow;
    		}
    	}

    	int n, source, sink;
    	int flow = 0;

    	List<Integer> []g;
    	List<Edge> e;

    	int []q, from, d;
    	boolean []inqueue;

    	public MaxFlowMinCost(int n, int source, int sink) {
    		this.n = n;
    		this.source = source;
    		this.sink = sink;

    		g = new LinkedList[n];
    		for(int i = 0; i < n; ++i) g[i] = new LinkedList<Integer>();
    		e = new ArrayList<Edge>();

    		q = new int[n];
    		d = new int[n];
    		from = new int[n];
    		inqueue = new boolean[n];
    	}

    	private boolean bfs() {
    		Arrays.fill(d, oo);
    		Arrays.fill(from, -1);
    		Arrays.fill(inqueue, false);
    		int head = 0, tail = 0;

    		d[source] = 0;
    		q[tail++] = source;
    		inqueue[source] = true;

    		while(head != tail && d[sink] == oo) {
    			int x = q[head++];
    			for(int id : g[x]) {
    				Edge edge = e.get(id);
    				if (edge.cap <= edge.flow) continue;
    				if (d[edge.to] > d[x] + 1) {
    					d[edge.to] = d[x] + 1;
    					from[edge.to] = id;
    					if (inqueue[edge.to]) continue;
    					inqueue[edge.to] = true;
    					q[tail++] = edge.to;
    					if (tail == n) tail = 0;
    				}
    			}
    			if (head == n) head = 0;
    			inqueue[x] = false;
    		}

    		return d[sink] != oo;
    	}

    	private void increase() {
    		// System.out.println("INCREASING FLOW...");
    		int c = sink, delta = oo;
    		while(from[c] != -1) {
    			int id = from[c];
    			Edge edge = e.get(id);
    			// System.out.println(edge);
    			delta = Math.min(delta, edge.cap - edge.flow);
    			c = edge.from;
    		}
    		// System.out.println("delta = " + delta);
    		flow+=delta;
    		c = sink;
    		while(from[c] != -1) {
    			int id = from[c];
    			e.get(id).flow+=delta;
    			e.get(id ^ 1).flow-=delta;

    			c = e.get(id).from;
    		}
    	}

    	public void execute() {
    		while(bfs()) {
    			increase();
    		}
    	}

    	public void addEdge(int from, int to, int cap) {
    		g[from].add(e.size());
    		e.add(new Edge(from, to, cap, 0));
    		g[to].add(e.size());
    		e.add(new Edge(to, from, cap, cap));
    	}

    	public void print() {
    		for(Edge edge : e) {
    			System.out.println(edge);
    		}
    	}

    }
}