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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
struct node {
  int r, c, t;
  node() {}
  node(int _r, int _c, int _t) {
    r = _r;
    c = _c;
    t = _t;
  }
} cur;
struct flow_graph {
  int MAX_V, E, s, t, head, tail;
  int *cap, *to, *next, *last, *dist, *q, *now;
  flow_graph() {}
  flow_graph(int V, int MAX_E) {
    MAX_V = V;
    E = 0;
    cap = new int[2 * MAX_E], to = new int[2 * MAX_E],
    next = new int[2 * MAX_E];
    last = new int[MAX_V], q = new int[MAX_V];
    dist = new int[MAX_V], now = new int[MAX_V];
    fill(last, last + MAX_V, -1);
  }
  void add_edge(int u, int v, int uv) {
    if (uv > 0) {
      to[E] = v, cap[E] = uv, next[E] = last[u];
      last[u] = E++;
      to[E] = u, cap[E] = 0, next[E] = last[v];
      last[v] = E++;
    }
  }
  bool bfs() {
    fill(dist, dist + MAX_V, -1);
    head = tail = 0;
    q[tail] = t;
    ++tail;
    dist[t] = 0;
    while (head < tail) {
      int v = q[head];
      ++head;
      for (int e = last[v]; e != -1; e = next[e]) {
        if (cap[e ^ 1] > 0 && dist[to[e]] == -1) {
          q[tail] = to[e];
          ++tail;
          dist[to[e]] = dist[v] + 1;
        }
      }
    }
    return dist[s] != -1;
  }
  int dfs(int v, int f) {
    if (v == t) return f;
    for (int &e = now[v]; e != -1; e = next[e]) {
      if (cap[e] > 0 && dist[to[e]] == dist[v] - 1) {
        int ret = dfs(to[e], min(f, cap[e]));
        if (ret > 0) {
          cap[e] -= ret;
          cap[e ^ 1] += ret;
          return ret;
        }
      }
    }
    return 0;
  }
  int max_flow(int source, int sink) {
    s = source;
    t = sink;
    int f = 0, df;
    while (bfs()) {
      for (int i = 0; i < MAX_V; ++i) now[i] = last[i];
      while (true) {
        df = dfs(s, INT_MAX);
        if (df == 0) break;
        f += df;
      }
    }
    return f;
  }
} G(2 + 10 * 10 * 62, 10 * 10 * (4 * 60 + 6));
int main() {
  int n, t;
  scanf("%d %d", &n, &t);
  char M1[n][n], M2[n][n];
  for (int i = 0; i < n; ++i) scanf("%s", M1[i]);
  for (int i = 0; i < n; ++i) scanf("%s", M2[i]);
  int sr = 0, sc = 0;
  int dist[n][n];
  memset(dist, -1, sizeof(dist));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (M1[i][j] == 'Z') sr = i, sc = j;
  int dr[] = {-1, 1, 0, 0};
  int dc[] = {0, 0, -1, 1};
  int r2, c2;
  queue<node> Q;
  Q.push(node(sr, sc, 0));
  dist[sr][sc] = 0;
  while (!Q.empty()) {
    cur = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      r2 = cur.r + dr[i];
      c2 = cur.c + dc[i];
      if (r2 >= 0 && r2 < n && c2 >= 0 && c2 < n && dist[r2][c2] == -1 &&
          M1[r2][c2] != 'Y' && M1[r2][c2] != 'Z') {
        dist[r2][c2] = dist[cur.r][cur.c] + 1;
        Q.push(node(r2, c2, cur.t + 1));
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (M1[i][j] >= '0' && M1[i][j] <= '9') {
        G.add_edge(0, 1 + i * n + j, M1[i][j] - '0');
        G.add_edge(1 + (t + 1) * n * n + i * n + j, 1 + (t + 2) * n * n,
                   M2[i][j] - '0');
      }
    }
  }
  for (int i = 0; i <= t; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        if ((dist[j][k] == -1 || dist[j][k] >= i) && M1[j][k] >= '0' &&
            M1[j][k] <= '9') {
          G.add_edge(1 + i * n * n + j * n + k, 1 + (t + 1) * n * n + j * n + k,
                     M2[j][k] - '0');
          if (i < t && (dist[j][k] == -1 || dist[j][k] > i)) {
            for (int d = 0; d < 4; ++d) {
              r2 = j + dr[d];
              c2 = k + dc[d];
              if (r2 >= 0 && r2 < n && c2 >= 0 && c2 < n && M1[r2][c2] >= '0' &&
                  M1[r2][c2] <= '9' &&
                  (dist[r2][c2] == -1 || dist[r2][c2] >= i + 1))
                G.add_edge(1 + i * n * n + j * n + k,
                           1 + (i + 1) * n * n + r2 * n + c2, 900);
            }
          }
        }
      }
    }
  }
  printf("%d\n", G.max_flow(0, 1 + n * n * (t + 2)));
  return 0;
}