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.LinkedList;
import java.util.InputMismatchException;
import java.io.*;
import java.util.Arrays;
import java.util.Queue;

/**
 * Generated by Contest helper plug-in
 * Actual solution is at the bottom
 */
public class Main {
    public static void main(String[] args) {
        InputReader in = new StreamInputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        run(in, out);
    }

    public static void run(InputReader in, PrintWriter out) {
        Solver solver = new Matching();
        solver.solve(1, in, out);
        Exit.exit(in, out);
    }
}

class StreamInputReader extends InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar, numChars;

    public StreamInputReader(InputStream stream) {
        this.stream = stream;
        curChar = 0;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    @Override
    public void close() {
        try {
            stream.close();
        } catch (IOException ignored) {
        }
    }
}

abstract class InputReader {
    private boolean finished = false;

    public abstract int read();

    public int nextInt() {
        return Integer.parseInt(nextToken());
    }
    public String readString() {
        return readLine();
    }


    public String readLine() {
        StringBuffer res = new StringBuffer();
        do {
            int c = read();
            if (isEndOfLineChar(c)) {
                return res.toString();
            }
            res.appendCodePoint(c);
        }  while (true) ;
    }
    public String nextToken() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }
    private boolean isEndOfLineChar(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }

    public abstract void close();
}

interface Solver {
    public void solve(int testNumber, InputReader in, PrintWriter out);
}

class Exit {
    private Exit() {
    }

    public static void exit(InputReader in, PrintWriter out) {
        in.setFinished(true);
        in.close();
        out.close();
    }
}

class Matching implements Solver {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int size = in.nextInt();
        int bomb = in.nextInt();
        char doctors[][] = new char[size][size];
        char capsuls[][] = new char[size][size];
        for (int i = 0; i < size; ++i) {
            doctors[i] = in.nextToken().toCharArray();
        }
        for (int i = 0; i < size; ++i) {
            capsuls[i] = in.nextToken().toCharArray();
        }

        Queue<Point> Q = new LinkedList<Point>() ;
        int dist[][] = new int[size][size];
        for (int[] a : dist) Arrays.fill(a, Integer.MAX_VALUE / 10);
        for (int i = 0; i < size; ++i)
            for (int j = 0; j < size; ++j)
                if (doctors[i][j] == 'Z') {
                    dist[i][j] = 0;
                    Q.add(new Point(j, i));
                }
        int dj[] = {-1, 1, 0, 0};
        int di[] = {0, 0, -1, 1};
        while (Q.size() > 0) {
            Point p = Q.poll();
            int j = p.x;
            int i = p.y;
            for (int u = 0; u < di.length; ++u) {
                int ni = i + di[u];
                int nj = j + dj[u];
                if (ni >= 0 && nj >= 0 && ni < size && nj < size && dist[ni][nj] == Integer.MAX_VALUE / 10 && Character.isDigit(capsuls[ni][nj])) {
                    dist[ni][nj] = dist[i][j]  + 1;
                    Q.add(new Point(nj, ni));
                }
            }
        }
       /* out.print("curse dist : \n");
        for (int d[] : dist) {
            out.print(Arrays.toString(d) + "\n"); out.flush();;
        */
        int d[][][][][] = new int[size][size][bomb + 2][size][size];
        for (int a[][][][] : d)
            for (int b[][][] : a)
                for (int c[][] : b)
                    for (int e[] : c)
                        Arrays.fill(e, -1);
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x) {
                if (Character.isDigit(doctors[y][x]) && doctors[y][x] - '0' >= 0) {
                  Queue<Tuples> queue = new LinkedList<Tuples>();
                    queue.add(new Tuples(x, y, 0));
                    d[y][x][0][y][x] = 1;
                    while (queue.size() > 0) {
                        Tuples p = queue.poll();
                        int i = p.y;
                        int j = p.x;
                        int time = p.time;
                        for (int u = 0; u < di.length; ++u) {
                            int ni = i + di[u];
                            int nj = j + dj[u];


                            if (ni >= 0 && nj >= 0 && ni < size && nj < size && time + 1 <= bomb && d[y][x][time + 1][ni][nj] == -1 && dist[ni][nj] >= time + 1 && Character.isDigit(doctors[ni][nj]) ) {
                                if (dist[ni][nj] > time + 1)
                                    queue.add(new Tuples(nj, ni, time + 1));
                                d[y][x][time + 1][ni][nj] = 1;
                            }
                        }
                    }
                }
            }
        }

        int nodes = size * size * 2 + 2;
        int src = nodes - 2;
        int des= nodes - 1;
        int capa[][] = new int[nodes][nodes];
        int flow[][] = new int[nodes][nodes];
        int prev[] = new int[nodes];
        for (int y = 0; y < size; ++y) {
            for (int x= 0; x <size; ++x) {
                if (Character.isDigit(doctors[y][x]) && doctors[y][x] - '0' > 0) {
                    capa[src][y * size + x] = doctors[y][x] - '0';
                    for (int i = 0; i < size; ++i)
                        for (int j = 0; j < size; ++j) {
                            for (int t = 0; t <= bomb; ++t)
                                if (d[y][x][t][i][j] >= 0 && Character.isDigit(capsuls[i][j]) && capsuls[i][j] - '0' > 0) {
                                    capa[y * size + x][size * size + i * size + j] = Integer.MAX_VALUE / 10;
                               //     out.println("add edge" + "(" + y + ", " + x + ")" + "->" + "(" + i + ", " + j + ")" );
                                }
                        }
                }
            }
        }
        for (int y = 0; y < size; ++y) {
            for (int x = 0; x < size; ++x)
                if (Character.isDigit(capsuls[y][x]) && capsuls[y][x] - '0' > 0) {
                    capa[size * size + y * size + x][des] = capsuls[y][x] - '0';
                }
        }

        int res = 0;
        while (true) {
            int q[] = new int[nodes];
            int dd[] = new int[nodes];
            int h = 0;
            q[h ++] = src;
            dd[src] = 1;
            for (int i = 0; i < h; ++i) {
                int cur = q[i];
                for (int j = 0; j < nodes; ++j) {
                    if (capa[cur][j] - flow[cur][j] > 0 && dd[j] == 0) {
                        prev[j] = cur;
                        q[h ++] = j;
                        dd[j] = 1;
                    }
                }
            }
            if (dd[des] == 0) break;
            int add = Integer.MAX_VALUE / 10;
            for (int cur = des; cur != src; cur = prev[cur]) {
                int p = prev[cur];
                add = Math.min(add, capa[p][cur] - flow[p][cur]);
            }
            for (int cur = des; cur != src; cur = prev[cur]) {
                int p = prev[cur];
                flow[p][cur] += add;
                flow[cur][p] -= add;
            }
            res += add;
        }
        out.print(res);
    }
    class Tuples {
        int x, y, time;

        Tuples(int x, int y, int time) {
            this.x = x;
            this.y = y;
            this.time = time;
        }
    }
    class Point {
        int x, y;

        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}