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 di[] = {-1, 1, 0, 0};
const int dj[] = {0, 0, 1, -1};
const int MAXN = 15;
int N, T;
char c[MAXN][MAXN];
int sv[MAXN][MAXN], sc[MAXN][MAXN];
int badTime[MAXN][MAXN];
bool used[MAXN][MAXN];
int si, sj;
int num[MAXN][MAXN];
queue<pair<int, int> > Q;
queue<int> qt;
struct edge {
  int a, b, f, c;
};
const int inf = 1000 * 1000 * 1000;
const int DM = 1050;
int n, m;
vector<edge> e;
int pt[DM];
vector<int> g[DM];
long long flow = 0;
queue<int> q;
int d[DM];
int lim;
int s, t;
void add_edge(int a, int b, int c) {
  edge ed;
  ed.a = a;
  ed.b = b;
  ed.f = 0;
  ed.c = c;
  g[a].push_back(e.size());
  e.push_back(ed);
  ed.a = b;
  ed.b = a;
  ed.f = c;
  ed.c = c;
  g[b].push_back(e.size());
  e.push_back(ed);
}
bool bfs() {
  for (int i = s; i <= t; i++) d[i] = inf;
  d[s] = 0;
  q.push(s);
  while (!q.empty() && d[t] == inf) {
    int cur = q.front();
    q.pop();
    for (size_t i = 0; i < g[cur].size(); i++) {
      int id = g[cur][i];
      int to = e[id].b;
      if (d[to] == inf && e[id].c - e[id].f >= lim) {
        d[to] = d[cur] + 1;
        q.push(to);
      }
    }
  }
  while (!q.empty()) q.pop();
  return d[t] != inf;
}
bool dfs(int v, int flow) {
  if (flow == 0) return false;
  if (v == t) {
    return true;
  }
  for (; pt[v] < g[v].size(); pt[v]++) {
    int id = g[v][pt[v]];
    int to = e[id].b;
    if (d[to] == d[v] + 1 && e[id].c - e[id].f >= flow) {
      int pushed = dfs(to, flow);
      if (pushed) {
        e[id].f += flow;
        e[id ^ 1].f -= flow;
        return true;
      }
    }
  }
  return false;
}
void dinic() {
  for (lim = (1 << 30); lim >= 1;) {
    if (!bfs()) {
      lim >>= 1;
      continue;
    }
    for (int i = s; i <= t; i++) pt[i] = 0;
    int pushed;
    while (pushed = dfs(s, lim)) {
      flow = flow + lim;
    }
  }
}
int main() {
  scanf("%d %d\n", &N, &T);
  s = 1;
  t = 1 + N * N + N * N + 1;
  int curNum = 0;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) {
      curNum++;
      num[i][j] = curNum;
    }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      scanf("%c", &c[i][j]);
      if (c[i][j] == 'Z') {
        si = i;
        sj = j;
        c[i][j] = 'Y';
      } else if (isdigit(c[i][j])) {
        sc[i][j] = c[i][j] - '0';
        c[i][j] = '.';
      }
    }
    scanf("\n");
  }
  scanf("\n");
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      scanf("%c", &c[i][j]);
      if (c[i][j] == 'Z') {
        si = i;
        sj = j;
        c[i][j] = 'Y';
      } else if (isdigit(c[i][j])) {
        sv[i][j] = c[i][j] - '0';
        c[i][j] = '.';
      }
    }
    scanf("\n");
  }
  Q.push(make_pair(si, sj));
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) badTime[i][j] = inf;
  badTime[si][sj] = 0;
  while (!Q.empty()) {
    int i = Q.front().first, j = Q.front().second;
    Q.pop();
    used[i][j] = true;
    for (int k = 0; k < 4; k++) {
      int ci = i + di[k], cj = j + dj[k];
      if (ci < 1 || ci > N || cj < 1 || cj > N) continue;
      if (c[ci][cj] != '.') continue;
      if (used[ci][cj]) continue;
      badTime[ci][cj] = badTime[i][j] + 1;
      used[ci][cj] = true;
      Q.push(make_pair(ci, cj));
    }
  }
  for (int ii = 1; ii <= N; ii++) {
    for (int jj = 1; jj <= N; jj++) {
      if (sc[ii][jj] == 0) continue;
      memset(used, 0, sizeof(used));
      Q.push(make_pair(ii, jj));
      qt.push(0);
      while (!Q.empty()) {
        int i = Q.front().first, j = Q.front().second;
        int tm = qt.front();
        Q.pop();
        qt.pop();
        used[i][j] = true;
        if (sv[i][j] > 0 && badTime[i][j] >= tm && tm <= T) {
          add_edge(1 + num[ii][jj], 1 + N * N + num[i][j], 1000 * 1000);
        }
        if (tm >= T) continue;
        if (badTime[i][j] <= tm) continue;
        for (int k = 0; k < 4; k++) {
          int ci = i + di[k], cj = j + dj[k];
          if (ci < 1 || ci > N || cj < 1 || cj > N) continue;
          if (c[ci][cj] != '.') continue;
          if (used[ci][cj]) continue;
          used[ci][cj] = true;
          Q.push(make_pair(ci, cj));
          qt.push(tm + 1);
        }
      }
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      add_edge(s, 1 + num[i][j], sc[i][j]);
      add_edge(1 + N * N + num[i][j], t, sv[i][j]);
    }
  }
  dinic();
  cout << flow << endl;
  return 0;
}