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;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\n";
  err(++it, args...);
}
const double PI = acos(-1);
const double eps = (1e-9);
const long long int INF = 2e18;
const int M = (1e9) + 7;
const int N = 11;
struct dinic {
  struct flow_edge {
    int from, to;
    long long int rem;
  };
  long long int lim, flow_inf = (1e18);
  int m = 0, source, sink;
  bool scaling = true;
  vector<flow_edge> edges;
  vector<vector<int> > ad;
  vector<int> lev, ptr;
  dinic(int n) : ad(n), lev(n), ptr(n) {}
  void add_edge(int a, int b, long long int c, long long int c_rev) {
    edges.emplace_back(flow_edge{a, b, c});
    edges.emplace_back(flow_edge{b, a, c_rev});
    ad[a].emplace_back(m++), ad[b].emplace_back(m++);
  }
  bool bfs() {
    fill(lev.begin(), lev.end(), -1);
    queue<int> pq;
    lev[source] = 0, pq.push(source);
    while (!pq.empty() && lev[sink] == -1) {
      int s = pq.front();
      pq.pop();
      for (int id : ad[s])
        if (lev[edges[id].to] == -1 && edges[id].rem >= lim)
          lev[edges[id].to] = lev[s] + 1, pq.push(edges[id].to);
    }
    return (lev[sink] != -1);
  }
  long long int dfs(int s, long long int pushed) {
    if (s == sink || pushed == 0) return pushed;
    while (ptr[s] < (int)ad[s].size()) {
      int id = ad[s][ptr[s]];
      if (lev[edges[id].to] == lev[s] + 1 && edges[id].rem > 0) {
        long long int f = dfs(edges[id].to, min(pushed, edges[id].rem));
        if (f > 0) {
          edges[id].rem -= f, edges[id ^ 1].rem += f;
          return f;
        }
      }
      ptr[s]++;
    }
    return 0;
  }
  long long int max_flow(int s, int t) {
    source = s, sink = t;
    long long int total = 0;
    for (lim = (scaling ? (1 << 10) : 1); lim > 0; lim = lim / 2) {
      while (bfs()) {
        fill(ptr.begin(), ptr.end(), 0);
        while (long long int cur = dfs(source, flow_inf)) total += cur;
      }
    }
    return total;
  }
};
bool vis[N][N], save[N][N], bomb[N][N], org[N][N];
int lev[N][N];
int I[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void compute(int x, int y, int p, int q, int explode, int n) {
  int i, j, a, b, f, k;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j)
      vis[i][j] = 0, save[i][j] = 0, bomb[i][j] = org[i][j], lev[i][j] = 0;
  }
  queue<tuple<int, int, int> > pq;
  pq.push(make_tuple(0, x, y)), vis[x][y] = 1, save[x][y] = 1;
  pq.push(make_tuple(1, p, q));
  while (!pq.empty()) {
    tie(f, a, b) = pq.front();
    pq.pop();
    if (lev[a][b] == explode) return;
    if (f == 0 && bomb[a][b] == 0) {
      for (k = 0; k < 4; ++k) {
        i = a + I[k][0], j = b + I[k][1];
        if ((i >= 0 && i <= n - 1) && (j >= 0 && j <= n - 1) &&
            vis[i][j] == 0 && bomb[i][j] == 0)
          vis[i][j] = 1, save[i][j] = 1, lev[i][j] = lev[a][b] + 1,
          pq.push(make_tuple(0, i, j));
      }
    }
    if (f) {
      for (k = 0; k < 4; ++k) {
        i = a + I[k][0], j = b + I[k][1];
        if ((i >= 0 && i <= n - 1) && (j >= 0 && j <= n - 1) && bomb[i][j] == 0)
          bomb[i][j] = 1, pq.push(make_tuple(1, i, j));
      }
    }
  }
  return;
}
void solve() {
  int n, t, i, j, a, b;
  scanf("%d%d", &n, &t);
  vector<string> sup(n);
  vector<string> dem(n);
  for (i = 0; i < n; ++i) cin >> sup[i];
  for (i = 0; i < n; ++i) cin >> dem[i];
  int p, q;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j)
      if (sup[i][j] == 'Z') p = i, q = j;
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j)
      if (sup[i][j] == 'Y' || sup[i][j] == 'Z') org[i][j] = 1;
  }
  int node = 2 * n * n + 2;
  int src = node - 2, sink = node - 1;
  dinic space(node);
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      if (org[i][j]) continue;
      compute(i, j, p, q, t, n);
      for (a = 0; a < n; ++a) {
        for (b = 0; b < n; ++b)
          if (save[a][b]) space.add_edge(n * i + j, n * a + b + n * n, 1000, 0);
      }
      a = sup[i][j] - '0';
      if (a > 0) space.add_edge(src, n * i + j, a, 0);
      a = dem[i][j] - '0';
      if (a > 0) space.add_edge(n * i + j + n * n, sink, a, 0);
    }
  }
  cout << space.max_flow(src, sink) << "\n";
}
void starting() {}
int main() {
  int t = 1;
  starting();
  while (t--) solve();
}