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 long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
const int N = 212;
const int M = 11234 * 2;
const int eps = 0;
struct dinic {
  int hd[N], seen[N], qu[N], lv[N], ei[N], to[M], nx[M];
  int fl[M], cp[M];
  int en = 2;
  int tempo = 0;
  void reset(int n = N) {
    en = 2;
    memset(hd, 0, sizeof(int) * n);
  }
  void reset_flow() { memset(fl, 0, sizeof(int) * en); }
  void add_edge(int a, int b, int c, int rc = 0) {
    to[en] = b;
    nx[en] = hd[a];
    fl[en] = 0;
    cp[en] = c;
    hd[a] = en++;
    to[en] = a;
    nx[en] = hd[b];
    fl[en] = 0;
    cp[en] = rc;
    hd[b] = en++;
  }
  bool bfs(int s, int t) {
    seen[t] = ++tempo;
    lv[t] = 0;
    int ql = 0, qr = 0;
    qu[qr++] = t;
    while (ql != qr) {
      t = qu[ql++];
      ei[t] = hd[t];
      if (s == t) return true;
      for (int e = hd[t]; e; e = nx[e])
        if (seen[to[e]] != tempo && cp[e ^ 1] - fl[e ^ 1] > eps) {
          seen[to[e]] = tempo;
          lv[to[e]] = lv[t] + 1;
          qu[qr++] = to[e];
        }
    }
    return false;
  }
  int dfs(int s, int t, int f) {
    if (s == t) return f;
    for (int &e = ei[s]; e; e = nx[e])
      if (ei[to[e]] && seen[to[e]] == tempo && cp[e] - fl[e] > eps &&
          lv[to[e]] == lv[s] - 1)
        if (int rf = dfs(to[e], t, min(f, cp[e] - fl[e]))) {
          fl[e] += rf;
          fl[e ^ 1] -= rf;
          return rf;
        }
    return 0;
  }
  int max_flow(int s, int t) {
    int fl = 0;
    while (bfs(s, t))
      while (int f = dfs(s, t, numeric_limits<int>::max())) fl += f;
    return fl;
  }
};
dinic d;
char S[112][112], C[112][112];
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
int n;
pair<int, int> o;
int ds[11][11][11][11];
void go(int si, int sj) {
  queue<pair<int, int> > q;
  q.push(pair<int, int>(si, sj));
  ds[si][sj][si][sj] = 0;
  while (!q.empty()) {
    pair<int, int> x = q.front();
    q.pop();
    int i = x.first, j = x.second;
    if ((o.first != si || o.second != sj) &&
        ds[o.first][o.second][i][j] != -1 &&
        ds[si][sj][i][j] >= ds[o.first][o.second][i][j])
      continue;
    for (int d = 0; d < 4; d++) {
      int ni = i + di[d], nj = j + dj[d];
      if (ni < 0 || nj < 0 || ni >= n || nj >= n || C[ni][nj] == 'Y' ||
          ds[si][sj][ni][nj] != -1)
        continue;
      ds[si][sj][ni][nj] = ds[si][sj][i][j] + 1;
      q.push(pair<int, int>(ni, nj));
    }
  }
}
int main() {
  memset(ds, -1, sizeof ds);
  int i, j, T;
  scanf("%d %d", &n, &T);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) scanf(" %c", &S[i][j]);
  vector<pair<int, int> > st;
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      scanf(" %c", &C[i][j]);
      if (isdigit(C[i][j])) {
        st.push_back(pair<int, int>(i, j));
      }
      if (C[i][j] == 'Z') o = pair<int, int>(i, j);
    }
  go(o.first, o.second);
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (C[i][j] != 'Y') go(i, j);
  int s = 2 * n * n, t = 2 * n * n + 1;
  for (i = 0; i < st.size(); i++) {
    int si = st[i].first, sj = st[i].second;
    d.add_edge(s, i, S[si][sj] - '0');
    d.add_edge(i + n * n, t, C[si][sj] - '0');
    for (j = 0; j < st.size(); j++) {
      int d = ds[si][sj][st[j].first][st[j].second];
      int d2 = ds[o.first][o.second][st[j].first][st[j].second];
      if (d != -1 && d <= T && (d2 == -1 || d <= d2))
        ::d.add_edge(i, j + n * n, 10000);
    }
  }
  printf("%d\n", d.max_flow(s, t));
}