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 double eps = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const long long LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const int MOD = 1000000007;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const double PI = 3.141592653589793;
inline void fastin(int &input_number) {
  input_number = 0;
  int ch = getchar_unlocked();
  int sign = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') sign = -1;
    ch = getchar_unlocked();
  }
  while (ch >= '0' && ch <= '9') {
    input_number = (input_number << 3) + (input_number << 1) + ch - '0';
    ch = getchar_unlocked();
  }
  input_number *= sign;
}
inline void open(string a) {
  freopen((a + ".in").c_str(), "r", stdin);
  freopen((a + ".out").c_str(), "w", stdout);
}
inline void fastios() { ios_base::sync_with_stdio(0); }
inline void fasterios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
struct dtmeteor {
  int mx, my, mstep;
};
struct dtbebek {
  int bx, by, bstep, bcnt;
};
struct dtmaxflow {
  int flowpos, flowmini;
};
int n, m;
char isi[15][15];
char shelter[15][15];
bool shelt[15][15];
int meteorx, meteory;
int jarakmeteor[15][15];
bool vismeteor[15][15];
dtmeteor tmpmeteor;
queue<dtmeteor> qmeteor;
bool visbebek[15][15];
dtbebek tmpbebek;
queue<dtbebek> qbebek;
vector<dtbebek> bebek;
int bebeksekarang;
bool vismaxflow[305][305];
dtmaxflow tmpmaxflow;
queue<dtmaxflow> qmaxflow;
int sink, source;
vector<int> edge[305];
int cap[305][305];
int par[305];
int ans;
bool adaflow;
void resparent() {
  memset(par, -1, sizeof(par));
  par[source] = 0;
  adaflow = 0;
}
void backtrackmaxflow(dtmaxflow now) {
  while (now.flowpos != source) {
    cap[now.flowpos][par[now.flowpos]] += now.flowmini;
    cap[par[now.flowpos]][now.flowpos] -= now.flowmini;
    now.flowpos = par[now.flowpos];
  }
}
void bfsmaxflow(dtmaxflow now) {
  if (now.flowpos == sink) {
    while (!qmaxflow.empty()) qmaxflow.pop();
    ans += now.flowmini;
    backtrackmaxflow(now);
    adaflow = 1;
    return;
  }
  for (int i = 0; i < edge[now.flowpos].size(); i++) {
    if (par[edge[now.flowpos][i]] != -1) continue;
    if (cap[now.flowpos][edge[now.flowpos][i]] <= 0) continue;
    par[edge[now.flowpos][i]] = now.flowpos;
    qmaxflow.push({edge[now.flowpos][i],
                   min(now.flowmini, cap[now.flowpos][edge[now.flowpos][i]])});
  }
}
int indexshelter(int nowx, int nowy) {
  return ((nowx - 1) * 10) + nowy + bebek.size() + 1;
}
void debugm() {
  cout << '\n';
  cout << '\n';
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cout << jarakmeteor[i][j];
    }
    cout << '\n';
  }
}
void bfsmeteor(dtmeteor now) {
  if (now.mstep == m) {
    while (!qmeteor.empty()) qmeteor.pop();
    return;
  }
  jarakmeteor[now.mx][now.my] = now.mstep;
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = now.mx + dx[i];
    ny = now.my + dy[i];
    if (nx <= 0 || ny <= 0 || nx > n || ny > n) continue;
    if (vismeteor[nx][ny]) continue;
    if (isi[nx][ny] == 'Y') continue;
    vismeteor[nx][ny] = 1;
    qmeteor.push({nx, ny, now.mstep + 1});
  }
}
void bfsbebek(dtbebek now) {
  if (shelt[now.bx][now.by]) {
    cap[bebeksekarang][indexshelter(now.bx, now.by)] = now.bcnt;
    edge[bebeksekarang].push_back(indexshelter(now.bx, now.by));
    edge[indexshelter(now.bx, now.by)].push_back(bebeksekarang);
  }
  if (now.bstep == jarakmeteor[now.bx][now.by]) return;
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = now.bx + dx[i];
    ny = now.by + dy[i];
    if (nx <= 0 || ny <= 0 || nx > n || ny > n) continue;
    if (visbebek[nx][ny]) continue;
    if (isi[nx][ny] == 'Y') continue;
    if (!shelt[nx][ny] && now.bstep + 1 == jarakmeteor[nx][ny]) continue;
    if (now.bstep + 1 > jarakmeteor[nx][ny]) continue;
    visbebek[nx][ny] = 1;
    qbebek.push({nx, ny, now.bstep + 1, now.bcnt});
  }
}
int main() {
  source = 0;
  sink = 250;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      jarakmeteor[i][j] = m;
      cin >> isi[i][j];
      if (isi[i][j] >= '1' && isi[i][j] <= '9')
        bebek.push_back({i, j, 0, isi[i][j] - '1' + 1});
      if (isi[i][j] == 'Z') {
        meteorx = i;
        meteory = j;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> shelter[i][j];
      if (shelter[i][j] >= '1' && shelter[i][j] <= '9') {
        edge[indexshelter(i, j)].push_back(sink);
        edge[sink].push_back(indexshelter(i, j));
        cap[indexshelter(i, j)][sink] = shelter[i][j] - '1' + 1;
        shelt[i][j] = 1;
      }
    }
  }
  vismeteor[meteorx][meteory] = 1;
  qmeteor.push({meteorx, meteory, 0});
  while (!qmeteor.empty()) {
    tmpmeteor = qmeteor.front();
    qmeteor.pop();
    bfsmeteor(tmpmeteor);
  }
  for (int i = 0; i < bebek.size(); i++) {
    bebeksekarang = i + 1;
    edge[bebeksekarang].push_back(source);
    edge[source].push_back(bebeksekarang);
    cap[source][bebeksekarang] = bebek[i].bcnt;
    memset(visbebek, 0, sizeof(visbebek));
    visbebek[bebek[i].bx][bebek[i].by] = 1;
    qbebek.push(bebek[i]);
    while (!qbebek.empty()) {
      tmpbebek = qbebek.front();
      qbebek.pop();
      bfsbebek(tmpbebek);
    }
  }
  do {
    resparent();
    qmaxflow.push({source, INF});
    while (!qmaxflow.empty()) {
      tmpmaxflow = qmaxflow.front();
      qmaxflow.pop();
      bfsmaxflow(tmpmaxflow);
    }
  } while (adaflow);
  cout << ans << '\n';
  return 0;
}