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.*;


public class E
{
   public static void main(String[] args)
   {
      new E(new Scanner(System.in));
   }

   public E(Scanner in)
   {
      int N = in.nextInt();
      int T = in.nextInt();
      
      int[][] cnt = new int[N][N];
      int[][] grid = new int[N][N];

      // This is the gas start point
      vect start = null;
      // These are the scientists
      ArrayList<vect> vs = new ArrayList<vect>();
      for (int j=0; j<N; j++)
      {
         String s = in.next();
         for (int i=0; i<N; i++)
         {
            char c = s.charAt(i);
            if (Character.isDigit(c))
            {
               grid[i][j] = T;
               int v = (int)(c-'0');
               while (v-->0)
                  vs.add(new vect(i,j));
            }
            else if (c == 'Y')
               grid[i][j] = 0;
            else if (c == 'Z')
            {
               grid[i][j] = 0;
               start = new vect(i,j);
            }
         }
      }

      for (int j=0; j<N; j++)
      {
         String s = in.next();
         for (int i=0; i<N; i++)
         {
            char c = s.charAt(i);
            if (Character.isDigit(c))
            {
               int v = (int)(c-'0');
               cnt[i][j] = v;
            }
         }
      }

      // Flood fill bfs the grid from the start point
      ArrayDeque<Node> q = new ArrayDeque<Node>();
      q.add(new Node(start, 0));
      while (q.size() > 0)
      {
         Node cur = q.poll();
         vect v = cur.v;
         grid[v.x][v.y] = cur.d;
         
         int nd = cur.d+1;
         vect dv = new vect(1,0);
         for (int i=0; i<4; i++)
         {
            dv = dv.orthoCW();
            vect nv = v.add(dv);
            if (nv.x < 0)
               continue;
            if (nv.y < 0)
               continue;
            if (nv.x >= N)
               continue;
            if (nv.y >= N)
               continue;
            if (grid[nv.x][nv.y] <= nd)
               continue;
            q.add(new Node(nv, nd));
         }
      }

      /*for (int j=0; j<N; j++)
      {
         StringBuilder sb = new StringBuilder();
         for (int i=0; i<N; i++)
            sb.append(grid[i][j]+" ");
         System.out.println(sb);
      }*/

      // Create a maxflow graph for all reachable reactors
      MaxFlow solver = new MaxFlow(2+vs.size()+N*N, 0, 1);
      for (int i=0; i<vs.size(); i++)
         solver.add(0, 2+i, 1);
      for (int j=0; j<N; j++)
         for (int i=0; i<N; i++)
            solver.add(2+vs.size()+j*N+i, 1, cnt[i][j]);

      // BFS for all reachable reactors
      boolean[][] seen = new boolean[N][N];
      int u = 0;
      for (vect st : vs)
      {
         for (boolean[] see : seen)
            Arrays.fill(see, false);

         q.add(new Node(st, 0));
         while (q.size() > 0)
         {
            Node cur = q.poll();
            vect v = cur.v;
            if (seen[v.x][v.y])
               continue;
            if (cur.d > grid[v.x][v.y])
               continue;
            seen[v.x][v.y] = true;
            if (cur.d == grid[v.x][v.y])
               continue;

            int nd = cur.d+1;
            vect dv = new vect(1,0);
            for (int i=0; i<4; i++)
            {
               dv = dv.orthoCW();
               vect nv = v.add(dv);
               if (nv.x < 0)
                  continue;
               if (nv.y < 0)
                  continue;
               if (nv.x >= N)
                  continue;
               if (nv.y >= N)
                  continue;
               if (nd > grid[v.x][v.y])
                  continue;
               if (seen[nv.x][nv.y])
                  continue;
               q.add(new Node(nv, nd));
            }
         }

         for (int j=0; j<N; j++)
            for (int i=0; i<N; i++)
               if (seen[i][j])
                  solver.add(2+u, 2+vs.size()+j*N+i, 1);
         u++;
      }
   
      
      int res = solver.maxFlow();
      System.out.printf("%d%n", res);
   }
}


class vect
{
   int x, y;
   public vect(int i, int j)
   {
      x=i; y=j;
   }

   vect add(vect rhs)
   {
      return new vect(x+rhs.x, y+rhs.y);
   }

   vect orthoCW()
   {
      return new vect(-y, x);
   }
}


class Node
{
   vect v;
   int d;

   public Node(vect vv, int dd)
   {
      v=vv; d=dd;
   }
}


class MaxFlow
{
   int N, src, snk;
   ArrayList<Edge>[] adj;
   boolean[] seen;
   public MaxFlow(int N)
   {
      this.N = N;
      seen = new boolean[N];
      adj = new ArrayList[N];
      for (int i=0; i<N; i++)
         adj[i] = new ArrayList<Edge>();
   }

   public MaxFlow(int N, int src, int snk)
   {
      this(N);
      this.src = src;
      this.snk = snk;
   }

   void add(int u, int v, int c)
   {
      Edge e = new Edge(u, v, c);
      Edge r = new Edge(v, u, 0, c);
      r.back = true;
      e.rev = r; r.rev = e;
      adj[u].add(e);
      adj[v].add(r);
   }

   int maxFlow()
   {
      int ret = 0;
      while (true)
      {
         Arrays.fill(seen, false);
         int f = dfs(src, Integer.MAX_VALUE);
         if (f == 0) break;
         ret += f;
      }
      return ret;
   }

   int dfs(int cur, int aug)
   {
      if (cur == snk) return aug;
      if (seen[cur]) return 0;
      seen[cur] = true;

      for (Edge e : adj[cur])
         if (e.resid > 0 && !seen[e.v])
         {
            int a = dfs(e.v, Math.min(aug, e.resid));
            if (a > 0)
            {
               e.resid -= a;
               e.rev.resid += a;
               return a;
            }
         }
      return 0;
   }

   class Edge
   {
      int u, v, resid, cap;
      Edge rev;
      boolean back;
      public Edge(int uu, int vv, int cc)
      {
         u=uu; v=vv; cap = resid = cc;
      }
      public Edge(int uu, int vv, int rr, int cc)
      {
         this(uu, vv, cc); resid = rr;
      }
      int flow() { return cap-resid;}
   }
}