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 N = 11;
const int INF = 1e9;
const int SIZE = 2 * N * N;
struct edge {
  int to, c, first, rev;
  edge() {}
  edge(int too, int cc, int ff, int revv) {
    to = too;
    c = cc;
    first = ff;
    rev = revv;
  }
};
vector<edge> g[SIZE];
int d[SIZE], ptr[SIZE], second, t, n;
queue<int> q;
void addEdge(int v, int u, int cap) {
  g[v].push_back(edge(u, cap, 0, 0));
  g[u].push_back(edge(v, 0, 0, 0));
  g[v].back().rev = g[u].size() - 1;
  g[u].back().rev = g[v].size() - 1;
}
bool buildLevNet() {
  for (int i = 0; i <= t; ++i) d[i] = INF;
  d[second] = 0;
  q.push(second);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); ++i) {
      int to = g[v][i].to;
      int cf = g[v][i].c - g[v][i].first;
      if (d[to] == INF && cf > 0) {
        d[to] = d[v] + 1;
        q.push(to);
      }
    }
  }
  return d[t] != INF;
}
int dfs(int v, int curFlow) {
  if (v == t) return curFlow;
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i].to;
    int cf = g[v][i].c - g[v][i].first, addFlow;
    if (d[to] == d[v] + 1 && cf > 0 && (addFlow = dfs(to, min(curFlow, cf)))) {
      g[v][i].first += addFlow;
      g[to][g[v][i].rev].first -= addFlow;
      return addFlow;
    }
  }
  return 0;
}
int dinica() {
  int flow = 0;
  while (buildLevNet()) {
    memset(ptr, 0, sizeof(ptr));
    int add;
    while ((add = dfs(second, INF))) flow += add;
  }
  return flow;
}
queue<pair<int, int> > go;
const int X[] = {-1, 1, 0, 0};
const int Y[] = {0, 0, -1, 1};
int dst[N][N][N][N], tbad;
char a[N][N], b[N][N];
pair<int, int> bad;
bool check(pair<int, int> v) {
  return v.first >= 0 && v.first < n && v.second >= 0 && v.second < n &&
         a[v.first][v.second] != 'Y' && a[v.first][v.second] != 'Z';
}
void bfs(int xs, int ys) {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) dst[xs][ys][i][j] = INF;
  dst[xs][ys][xs][ys] = 0;
  go.push(make_pair(xs, ys));
  while (!go.empty()) {
    pair<int, int> v = go.front();
    go.pop();
    if (dst[xs][ys][v.first][v.second] ==
            dst[bad.first][bad.second][v.first][v.second] &&
        (bad.first != xs || bad.second != ys))
      continue;
    for (int i = 0; i < 4; ++i) {
      pair<int, int> nv = make_pair(v.first + X[i], v.second + Y[i]);
      if (check(nv) && dst[xs][ys][nv.first][nv.second] == INF &&
          dst[xs][ys][v.first][v.second] + 1 <=
              dst[bad.first][bad.second][nv.first][nv.second]) {
        dst[xs][ys][nv.first][nv.second] = dst[xs][ys][v.first][v.second] + 1;
        go.push(nv);
      }
    }
  }
}
int main() {
  cin >> n >> tbad;
  int px, py;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'Z') px = i, py = j;
    }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) cin >> b[i][j];
  bad = make_pair(px, py);
  bfs(px, py);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      dst[bad.first][bad.second][i][j] =
          min(dst[bad.first][bad.second][i][j], tbad);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (check(make_pair(i, j))) bfs(i, j);
  second = 0;
  t = 2 * n * n + 1;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      if (a[i][j] != '0') addEdge(second, i * n + j + 1, a[i][j] - '0');
      if (b[i][j] != '0') addEdge(i * n + j + 1 + n * n, t, b[i][j] - '0');
    }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      for (int i1 = 0; i1 < n; ++i1)
        for (int j1 = 0; j1 < n; ++j1)
          if (check(make_pair(i, j)) && check(make_pair(i1, j1)) &&
              dst[i][j][i1][j1] <= dst[px][py][i1][j1])
            addEdge(i * n + j + 1, i1 * n + j1 + 1 + n * n, INF);
  cout << dinica();
  return 0;
}