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 codeforces.br70;

import javax.management.Query;
import java.io.*;
import java.math.*;
import java.util.*;

public class ProblemE {


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

    private class Point {
        public int x,y;
        public Point(int xx, int yy) {
            x = xx;
            y = yy;
        }

        public Point(Point p, int ind) {
            x = p.x + dir[ind][0];
            y = p.y + dir[ind][1];
        }

        public boolean valid() {
            return x >= 0 && x < n && y >= 0 && y < n;
        }
    }

    private int[] pair1;
    private int[] pair2;
    private boolean[] used;
    List<ArrayList<Integer>> g;
    private int n;

     public void solve() {
        boolean oj = true;

        try {
            Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("E.in");
            Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("E.out");
            BufferedReader br = new BufferedReader(reader);
            StreamTokenizer st = new StreamTokenizer(reader);
            PrintWriter out = new PrintWriter(writer);

            MyTokenizer tok = new MyTokenizer(br.readLine());

            n = tok.getInteger();
            int t = tok.getInteger();

            Point z = null;

            char[][] sci = new char[n][n];
            char[][] caps = new char[n][n];

            List<Point> scis = new ArrayList<Point>();
            List<ArrayList<Integer>> v1 = new ArrayList<ArrayList<Integer>>();

            int cc = 0;
            for(int i=0;i<n;i++) {
                String s = br.readLine();
                for(int j=0;j<n;j++) {
                    sci[i][j] = s.charAt(j);
                    if (sci[i][j] == 'Z') {
                        z = new Point(i, j);
                    }
                    if (sci[i][j] > '0' && sci[i][j] <= '9') {
                        Point pp = new Point(i, j);
                        scis.add(pp);

                        ArrayList<Integer> list = new ArrayList<Integer>();
                        v1.add(list);
                        int num = sci[i][j] - '0';
                        for(int k=0;k<num;k++) {
                            list.add(cc);
                            cc++;
                        }
                    }
                }
            }

            int numsci = cc;

            br.readLine();

            List<Point> allcaps = new ArrayList<Point>();
            List<ArrayList<Integer>> v2 = new ArrayList<ArrayList<Integer>>();
            int[][] p = new int[n][n];

            cc = 0;

            for(int i=0;i<n;i++) {
                String s = br.readLine();
                for(int j=0;j<n;j++) {
                    caps[i][j] = s.charAt(j);
                    p[i][j] = -1;
                    if (caps[i][j] > '0' && caps[i][j] <= '9') {
                        Point pp = new Point(i, j);
                        allcaps.add(pp);
                        p[i][j] = allcaps.size() - 1;

                        ArrayList<Integer> list = new ArrayList<Integer>();
                        v2.add(list);
                        int num = caps[i][j] - '0';
                        for(int k=0;k<num;k++) {
                            list.add(cc);
                            cc++;
                        }
                    }
                }
            }

            int numcaps = cc;

            int[][] zdist = new int[n][n];
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    zdist[i][j] = -1;

            Queue<Point> q = new LinkedList<Point>();
            q.add(z);

            zdist[z.x][z.y] = 0;

            while (!q.isEmpty()) {
                Point cur = q.remove();
                for(int i=0;i<4;i++) {
                    Point next = new Point(cur, i);
                    if (next.valid() && zdist[next.x][next.y] == -1 && sci[next.x][next.y] != 'Y') {
                        zdist[next.x][next.y] = zdist[cur.x][cur.y] + 1;
                        q.add(next);
                    }
                }
            }



            g = new ArrayList<ArrayList<Integer>>();
            for(int i=0;i<numsci;i++)
                g.add(new ArrayList<Integer>());

            for (int i1 = 0, scisSize = scis.size(); i1 < scisSize; i1++) {
                Point sc = scis.get(i1);

                int numsc = sci[sc.x][sc.y] - '0';

                int[][] scdist = new int[n][n];
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                        scdist[i][j] = -1;

                q.clear();
                scdist[sc.x][sc.y] = 0;
                q.add(sc);

                if (p[sc.x][sc.y] >= 0) {
                    int capnum = p[sc.x][sc.y];
                    for(int i=0;i<v1.get(i1).size();i++) {
                        for(int j=0;j<v2.get(capnum).size();j++) {
                            int scn = v1.get(i1).get(i);
                            int cpn = v2.get(capnum).get(j);
                            g.get(scn).add(cpn);
                        }
                    }
                }

                while (!q.isEmpty()) {
                    Point cur = q.remove();
                    for (int k = 0; k < 4; k++) {
                        Point next = new Point(cur, k);
                        int num = scdist[cur.x][cur.y] + 1;
                        if (next.valid() && scdist[next.x][next.y] == -1 && sci[next.x][next.y] != 'Y' && sci[next.x][next.y] != 'Z' && (num <= zdist[next.x][next.y] || zdist[next.x][next.y] == -1) && num <= t) {
                            if (zdist[next.x][next.y] != num) {
                                scdist[next.x][next.y] = num;
                                q.add(next);
                            }
                            if (p[next.x][next.y] >= 0) {
                                int capnum = p[next.x][next.y];
                                for(int i=0;i<v1.get(i1).size();i++) {
                                    for(int j=0;j<v2.get(capnum).size();j++) {
                                        int scn = v1.get(i1).get(i);
                                        int cpn = v2.get(capnum).get(j);
                                        g.get(scn).add(cpn);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            pair1 = new int[numsci];
            pair2 = new int[numcaps];
            for(int i=0;i<numsci;i++) {
                pair1[i] = -1;
            }
            for(int i=0;i<numcaps;i++) {
                pair2[i] = -1;
            }
            for(int i=0;i<numsci;i++) {
                used = new boolean[numsci];
                find(i);
            }

            int count = 0;
            for(int i=0;i<numsci;i++) {
                if (pair1[i] != -1)
                    count++;
            }
            out.printf("%d", count);

            br.close();
            out.close();
            reader.close();
            writer.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
        }
    }

    private boolean find(int v) {
        used[v] = true;
        boolean result = false;
        ArrayList<Integer> list = g.get(v);
        for(int i : list) {
            if (pair2[i] == -1 || (!used[pair2[i]] && find(pair2[i]))) {
                result = true;
                pair1[v] = i;
                pair2[i] = v;
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        ProblemE f = new ProblemE();
        f.solve();
    }

    private class MyTokenizer {
        private String s;
        private int cur;

        public MyTokenizer(String s) {
            this.s = s;
            cur = 0;
        }

        public void skip() {
            while (cur < s.length() && (s.charAt(cur) == ' ' || s.charAt(cur) == '\t' || s.charAt(cur) == '\n')) {
                cur++;
            }
        }

        public double getDouble() {
            skip();
            String snum = "";
            while (cur < s.length() && (s.charAt(cur) >= '0' && s.charAt(cur) <= '9' || s.charAt(cur) == '.' || s.charAt(cur) == '-')) {
                snum += s.charAt(cur);
                cur++;
            }
            if (snum.startsWith("."))
                snum = "0" + snum;
            return Double.valueOf(snum);
        }

        public int getInteger() {
            skip();
            String snum = "";
            while (cur < s.length() && (s.charAt(cur) >= '0' && s.charAt(cur) <= '9' || s.charAt(cur) == '.' || s.charAt(cur) == '-')) {
                snum += s.charAt(cur);
                cur++;
            }
            return Integer.valueOf(snum);
        }

        public String getString() {
            skip();
            String s2 = "";
            while (cur < s.length() && (((s.charAt(cur) >= 'a' && s.charAt(cur) <= 'z')) ||
                    ((s.charAt(cur) >= 'A' && s.charAt(cur) <= 'Z')) ||
                    ((s.charAt(cur) >= '0' && s.charAt(cur) <= '9')) ||
                    s.charAt(cur) == '_'
            )) {
                s2 += s.charAt(cur);
                cur++;
            }
            return s2;
        }

        public char getCurrentChar() throws Exception {
            if (cur < s.length())
                return s.charAt(cur);
            else
                throw new Exception("Current character out of string length");
        }

        public void moveNextChar() {
            if (cur < s.length())
                cur++;
        }

        public boolean isFinished() {
            return cur >= s.length();
        }
    }
}