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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedOutputStream;
import java.util.List;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author An Almost Retired Guy
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt(), t = in.nextInt();
            char[][] scientists = in.nextCharArray2(n, n), capsules = in.nextCharArray2(n, n);
            boolean[][] table = new boolean[n][n];
            int x0 = -1, y0 = -1;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++) {
                    table[i][j] = Character.isDigit(scientists[i][j]);
                    if (scientists[i][j] == 'Z') {
                        x0 = i;
                        y0 = j;
                    }
                }
            int n2 = n * n, N = 2 * n2 + 2, sink = N - 1, source = sink - 1;
            ArrayList<MaxFlowDinic.Edge>[] graph = new ArrayList[N];
            for (int i = 0; i < N; i++) graph[i] = new ArrayList<>();
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    MaxFlowDinic.addEdge(graph, source, i * n + j, table[i][j] ? scientists[i][j] - '0' : 0);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    MaxFlowDinic.addEdge(graph, n2 + i * n + j, sink, table[i][j] ? capsules[i][j] - '0' : 0);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        for (int l = 0; l < n; l++)
                            if (f(table, i, j, k, l, x0, y0, t)) {
                                if (table[i][j] && table[k][l] && scientists[i][j] != '0' && capsules[k][l] != '0')
                                    System.err.println(i + " " + j + " " + k + " " + l);
                                MaxFlowDinic.addEdge(graph, i * n + j, n2 + k * n + l, Integer.MAX_VALUE);
                            }
            int ans = MaxFlowDinic.maxFlow(graph, source, sink);
            out.println(ans);
        }

        boolean f(boolean[][] table, int xs, int ys, int xe, int ye, int x0, int y0, int t) {
            int n = table.length;
            int[][] last = new int[n][n], now = new int[n][n];
            final int[] dx = new int[]{-1, 0, 0, 1}, dy = new int[]{0, -1, 1, 0};
            now[xs][ys] = 1;
            now[x0][y0] = -1;
            while (t-- > 0) {
                boolean can1 = false;
                for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) last[i][j] = now[i][j];
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        if (last[i][j] == 1) for (int k = 0; k < 4; k++) {
                            int i2 = i + dx[k], j2 = j + dy[k];
                            if (i2 >= 0 && i2 < n && j2 >= 0 && j2 < n && table[i2][j2] && now[i2][j2] == 0) {
                                now[i2][j2] = 1;
                                can1 = true;
                            }
                        }
                boolean can2 = false;
                for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) last[i][j] = now[i][j];
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        if (last[i][j] == -1) for (int k = 0; k < 4; k++) {
                            int i2 = i + dx[k], j2 = j + dy[k];
                            if (i2 >= 0 && i2 < n && j2 >= 0 && j2 < n && table[i2][j2] && (now[i2][j2] == 0 || (now[i2][j2] == 1 && (i2 != xe || j2 != ye)))) {
                                now[i2][j2] = -1;
                                can2 = true;
                            }
                        }
                if (!can1 && !can2) break;
            }
            return now[xe][ye] == 1;
        }

    }

    static class MaxFlowDinic {
        public static void addEdge(List<MaxFlowDinic.Edge>[] graph, int s, int t, int cap) {
            graph[s].add(new MaxFlowDinic.Edge(t, graph[t].size(), cap));
            graph[t].add(new MaxFlowDinic.Edge(s, graph[s].size() - 1, 0));
        }

        static boolean dinicBfs(List<MaxFlowDinic.Edge>[] graph, int src, int dest, int[] dist) {
            Arrays.fill(dist, -1);
            dist[src] = 0;
            int[] Q = new int[graph.length];
            int sizeQ = 0;
            Q[sizeQ++] = src;
            for (int i = 0; i < sizeQ; i++) {
                int u = Q[i];
                for (MaxFlowDinic.Edge e : graph[u]) {
                    if (dist[e.t] < 0 && e.f < e.cap) {
                        dist[e.t] = dist[u] + 1;
                        Q[sizeQ++] = e.t;
                    }
                }
            }
            return dist[dest] >= 0;
        }

        static int dinicDfs(List<MaxFlowDinic.Edge>[] graph, int[] ptr, int[] dist, int dest, int u, int f) {
            if (u == dest)
                return f;
            for (; ptr[u] < graph[u].size(); ++ptr[u]) {
                MaxFlowDinic.Edge e = graph[u].get(ptr[u]);
                if (dist[e.t] == dist[u] + 1 && e.f < e.cap) {
                    int df = dinicDfs(graph, ptr, dist, dest, e.t, Math.min(f, e.cap - e.f));
                    if (df > 0) {
                        e.f += df;
                        graph[e.t].get(e.rev).f -= df;
                        return df;
                    }
                }
            }
            return 0;
        }

        public static int maxFlow(List<MaxFlowDinic.Edge>[] graph, int src, int dest) {
            int flow = 0;
            int[] dist = new int[graph.length];
            while (dinicBfs(graph, src, dest, dist)) {
                int[] ptr = new int[graph.length];
                while (true) {
                    int df = dinicDfs(graph, ptr, dist, dest, src, Integer.MAX_VALUE);
                    if (df == 0)
                        break;
                    flow += df;
                }
            }
            return flow;
        }

        public static class Edge {
            public int t;
            public int rev;
            public int cap;
            public int f;

            public Edge(int t, int rev, int cap) {
                this.t = t;
                this.rev = rev;
                this.cap = cap;
            }

        }

    }

    static class InputReader {
        BufferedReader br;
        StringTokenizer st;

        public InputReader(InputStream inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String nextString() {
            return next();
        }

        public char[] nextCharArray(int n) {
            char[] a = nextString().toCharArray();
            assert (a.length == n);
            return a;
        }

        public char[][] nextCharArray2(int n, int m) {
            char[][] a = new char[n][];
            for (int i = 0; i < n; i++) a[i] = nextCharArray(m);
            return a;
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(new BufferedOutputStream(outputStream));
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public void close() {
            super.close();
        }

    }
}