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;
class Dinic {
  struct edge {
    int u, v, f, c, nx;
    edge() {}
    edge(int u, int v, int f, int c, int nx) : u(u), v(v), f(f), c(c), nx(nx) {}
  } e[256 * 256];
  int n, m, src, dst, head[256], work[256], d[256];
  bool _bfs() {
    static int q[256], le, ri;
    le = ri = 0;
    memset(d, -1, sizeof(int) * n);
    q[ri++] = src;
    d[src] = 0;
    while (le < ri) {
      for (int k = q[le++], i = head[k]; i != -1; i = e[i].nx) {
        if (e[i].f < e[i].c && d[e[i].v] == -1) {
          d[e[i].v] = d[k] + 1;
          q[ri++] = e[i].v;
        }
      }
    }
    return (d[dst] != -1);
  }
  int _dfs(int u, int f) {
    if (u == dst) return f;
    int minf;
    for (int& i = work[u]; i != -1; i = e[i].nx) {
      if (e[i].f < e[i].c && d[e[i].v] == d[u] + 1) {
        minf = _dfs(e[i].v, min(f, e[i].c - e[i].f));
        if (minf > 0) {
          e[i].f += minf;
          e[i ^ 1].f -= minf;
          return minf;
        }
      }
    }
    return 0;
  }

 public:
  void init(int n, int src, int dst) {
    this->n = n;
    this->src = src;
    this->dst = dst;
    m = 0;
    memset(head, -1, sizeof(int) * n);
  }
  void addEdge(int u, int v, int c) {
    e[m] = edge(u, v, 0, c, head[u]);
    head[u] = m++;
    e[m] = edge(v, u, 0, 0, head[v]);
    head[v] = m++;
  }
  int maxFlow() {
    int ret = 0, delta;
    while (_bfs()) {
      memcpy(work, head, sizeof(int) * n);
      while (true) {
        delta = _dfs(src, 1000000000);
        if (delta == 0) break;
        ret += delta;
      }
    }
    return ret;
  }
} dinic;
int n, t, zx, zy;
char smp[12][12], cmp[12][12], work[12][12];
int vis[64][12][12];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
inline int X(int i, int j) { return i * n + j; }
inline int Y(int i, int j) { return n * n + i * n + j; }
inline int S() { return n * n * 2; }
inline int T() { return S() + 1; }
inline int N() { return T() + 1; }
struct qNode {
  int x, y, t, type;
  qNode() {}
  qNode(int x, int y, int t, int type) : x(x), y(y), t(t), type(type) {}
};
void bfs(int sx, int sy) {
  queue<qNode> q;
  q.push(qNode(sx, sy, 0, 0));
  vis[0][sx][sy] = 1;
  q.push(qNode(zx, zy, 0, 1));
  while (!q.empty()) {
    qNode u = q.front();
    int nx, ny;
    q.pop();
    if (u.t == t) break;
    if (u.type == 0 && isalpha(work[u.x][u.y])) continue;
    for (int i = 0; i < 4; ++i) {
      nx = u.x + dx[i];
      ny = u.y + dy[i];
      if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
        if (u.type == 0 && !vis[u.t + 1][nx][ny] && !isalpha(work[nx][ny])) {
          vis[u.t + 1][nx][ny] = 1;
          q.push(qNode(nx, ny, u.t + 1, 0));
        } else if (u.type == 1 && !isalpha(work[nx][ny])) {
          work[nx][ny] = 'Z';
          q.push(qNode(nx, ny, u.t + 1, 1));
        }
      }
    }
  }
}
int main() {
  while (scanf("%d%d", &n, &t) != EOF) {
    dinic.init(N(), S(), T());
    for (int i = 0; i < n; ++i) {
      scanf("%s", smp[i]);
      for (int j = 0; j < n; ++j)
        if (isdigit(smp[i][j]) && smp[i][j] > '0')
          dinic.addEdge(S(), X(i, j), smp[i][j] - '0');
        else if (smp[i][j] == 'Z') {
          zx = i;
          zy = j;
        }
    }
    for (int i = 0; i < n; ++i) {
      scanf("%s", cmp[i]);
      for (int j = 0; j < n; ++j)
        if (isdigit(cmp[i][j]) && cmp[i][j] > '0')
          dinic.addEdge(Y(i, j), T(), cmp[i][j] - '0');
    }
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        if (!isdigit(smp[i][j]) || smp[i][j] <= '0') continue;
        memset(vis, 0, sizeof(vis));
        memcpy(work, smp, sizeof(smp));
        bfs(i, j);
        for (int xi = 0; xi < n; ++xi)
          for (int yi = 0; yi < n; ++yi) {
            if (!isdigit(cmp[xi][yi]) || cmp[xi][yi] <= '0') continue;
            for (int ti = 0; ti <= t; ++ti)
              if (vis[ti][xi][yi]) {
                dinic.addEdge(X(i, j), Y(xi, yi), 1000000000);
                break;
              }
          }
      }
    printf("%d\n", dinic.maxFlow());
  }
  return 0;
}