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;
const int c[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int S, T, tot = 1, n, tt, inf, b[1000], q[1000000], a[1000000][3], dep[20][20],
          dis[20][20], p[20][20], s[20][20];
char ma[20][20], mb[20][20];
void add(int x, int y, int c) {
  a[++tot][0] = y, a[tot][1] = c, a[tot][2] = b[x], b[x] = tot;
  a[++tot][0] = x, a[tot][1] = 0, a[tot][2] = b[y], b[y] = tot;
}
void bfs(int x0, int y0, bool flag) {
  int l, r, id = p[x0][y0];
  q[l = r = 1] = x0 * 1000 + y0;
  memset(dis, 63, sizeof(dis));
  inf = dis[0][0];
  dis[x0][y0] = 0;
  while (l <= r) {
    int x0 = q[l] / 1000, y0 = q[l++] % 1000;
    if (flag && s[x0][y0] != 0 && dis[x0][y0] <= tt &&
        dis[x0][y0] <= dep[x0][y0])
      add(id, s[x0][y0], inf);
    if (dis[x0][y0] > tt || dis[x0][y0] >= dep[x0][y0]) continue;
    if (id == 3 && s[x0][y0] == 9) printf("%d\n", dis[x0][y0]);
    for (int i = 0; i < 4; i++) {
      int x = x0 + c[i][0], y = y0 + c[i][1];
      if (x > 0 && x <= n && y > 0 && y <= n && isdigit(ma[x][y]) &&
          dis[x][y] == inf) {
        dis[x][y] = dis[x0][y0] + 1;
        q[++r] = x * 1000 + y;
      }
    }
  }
}
int d[1000];
bool build() {
  memset(d, 0, sizeof(d));
  int l, r;
  q[l = r = 1] = S;
  d[S] = 1;
  while (l <= r) {
    int x = q[l++];
    for (int i = b[x]; i; i = a[i][2]) {
      int y = a[i][0];
      if (d[y] || !a[i][1]) continue;
      d[y] = d[x] + 1;
      q[++r] = y;
      if (y == T) return (true);
    }
  }
  return (false);
}
int dinic(int x, int flow) {
  if (x == T) return (flow);
  int k = flow;
  for (int i = b[x]; i; i = a[i][2]) {
    int y = a[i][0];
    if (d[y] != d[x] + 1 || !a[i][1]) continue;
    int t = dinic(y, min(k, a[i][1]));
    k -= t;
    a[i][1] -= t;
    a[i ^ 1][1] += t;
    if (k == 0) return (flow);
  }
  if (k == flow) d[x] = -1;
  return (flow - k);
}
int main() {
  scanf("%d%d", &n, &tt);
  for (int i = 1; i <= n; i++) scanf("%s", ma[i] + 1);
  for (int i = 1; i <= n; i++) scanf("%s", mb[i] + 1);
  memset(dep, 63, sizeof(dep));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (ma[i][j] == 'Z') bfs(i, j, false);
  memcpy(dep, dis, sizeof(dis));
  int t = 1;
  S = 0, T = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (ma[i][j] > '0' && ma[i][j] <= '9') {
        p[i][j] = ++t;
        add(S, t, ma[i][j] - '0');
      }
      if (mb[i][j] > '0' && mb[i][j] <= '9') {
        s[i][j] = ++t;
        add(t, T, mb[i][j] - '0');
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (p[i][j]) bfs(i, j, true);
  int ans = 0;
  while (build()) ans += dinic(S, inf);
  printf("%d\n", ans);
  return (0);
}