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 char infile[] = "input.in";
const char outfile[] = "output.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 15;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN * MAXN * 2];
const inline int min(const int &a, const int &b) {
  if (a > b) return b;
  return a;
}
const inline int max(const int &a, const int &b) {
  if (a < b) return b;
  return a;
}
const inline void Get_min(int &a, const int b) {
  if (a > b) a = b;
}
const inline void Get_max(int &a, const int b) {
  if (a < b) a = b;
}
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int N, T;
char a[MAXN][MAXN], b[MAXN][MAXN];
int getInfected[MAXN][MAXN];
int dp[MAXN][MAXN][MAXN][MAXN], Nodes;
int C[MAXN * MAXN * 2][MAXN * MAXN * 2];
int Source, Sink;
queue<pair<int, int> > Q;
Graph G;
int Father[MAXN * MAXN * 2];
bitset<MAXN * MAXN> Used;
inline bool inside(int x, int y) {
  return x >= 1 && x <= N && y >= 1 && y <= N;
}
inline int getNode(int x, int y) { return (x - 1) * N + y; }
inline void buildInfectedTime() {
  memset(getInfected, oo, sizeof(getInfected));
  getInfected[Q.front().first][Q.front().second] = 0;
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    for (int d = 0; d < 4; ++d) {
      int xnou = x + dx[d];
      int ynou = y + dy[d];
      if (inside(xnou, ynou) &&
          getInfected[xnou][ynou] == oo & a[xnou][ynou] != 'Y') {
        getInfected[xnou][ynou] = getInfected[x][y] + 1;
        Q.push(make_pair(xnou, ynou));
      }
    }
  }
}
inline void BFs(int xst, int yst) {
  Q.push(make_pair(xst, yst));
  dp[xst][yst][xst][yst] = 0;
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    for (int d = 0; d < 4; ++d) {
      int xnou = x + dx[d];
      int ynou = y + dy[d];
      if (inside(xnou, ynou) && dp[xst][yst][xnou][ynou] == oo &&
          isdigit(a[xnou][ynou]) &&
          (getInfected[xnou][ynou] > dp[xst][yst][x][y])) {
        if (getInfected[xnou][ynou] > dp[xst][yst][x][y] + 1)
          Q.push(make_pair(xnou, ynou));
        dp[xst][yst][xnou][ynou] = dp[xst][yst][x][y] + 1;
      }
    }
  }
}
inline bool BFs(Graph &G, int Source, int Sink) {
  queue<int> Q;
  Used.reset();
  Q.push(Source);
  Used[Source] = 1;
  while (!Q.empty()) {
    int Node = Q.front();
    Q.pop();
    if (Node == Sink) continue;
    for (vector<int>::iterator it = G[Node].begin(), fin = G[Node].end();
         it != fin; ++it)
      if (!Used[*it] && C[Node][*it] > 0) {
        Used[*it] = 1;
        Father[*it] = Node;
        Q.push(*it);
      }
  }
  return Used[Sink];
}
inline int getMaxFlow(Graph &G, int Source, int Sink) {
  int maxFlow = 0;
  while (BFs(G, Source, Sink))
    for (vector<int>::iterator it = G[Sink].begin(), fin = G[Sink].end();
         it != fin; ++it) {
      if (!Used[*it] || C[*it][Sink] <= 0) continue;
      int bottleNeck = oo;
      Father[Sink] = *it;
      for (int i = Sink; i != Source; i = Father[i])
        bottleNeck = min(bottleNeck, C[Father[i]][i]);
      if (!bottleNeck) continue;
      for (int i = Sink; i != Source; i = Father[i]) {
        C[Father[i]][i] -= bottleNeck;
        C[i][Father[i]] += bottleNeck;
      }
      maxFlow += bottleNeck;
    }
  return maxFlow;
}
int main() {
  cin.sync_with_stdio(false);
  cin >> N >> T;
  for (int i = 1; i <= N; ++i) cin >> (a[i] + 1);
  for (int i = 1; i <= N; ++i) cin >> (b[i] + 1);
  for (int i = 1; i <= N && Q.empty(); ++i)
    for (int j = 1; j <= N && Q.empty(); ++j)
      if (a[i][j] == 'Z') Q.push(make_pair(i, j));
  buildInfectedTime();
  Source = 0;
  Sink = N * N + getNode(N, N + 1);
  memset(dp, oo, sizeof(dp));
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= N; ++j) {
      if (isdigit(a[i][j]) && a[i][j] > '0') {
        BFs(i, j);
        for (int x = 1; x <= N; ++x)
          for (int y = 1; y <= N; ++y)
            if (dp[i][j][x][y] <= T && isdigit(b[x][y]) && b[x][y] > '0') {
              G[getNode(i, j)].push_back(N * N + getNode(x, y));
              G[N * N + getNode(x, y)].push_back(getNode(i, j));
              C[getNode(i, j)][N * N + getNode(x, y)] = oo;
            }
        G[Source].push_back(getNode(i, j));
        G[getNode(i, j)].push_back(Source);
        C[Source][getNode(i, j)] = a[i][j] - '0';
      }
      if (isdigit(b[i][j]) && b[i][j] > '0') {
        G[N * N + getNode(i, j)].push_back(Sink);
        G[Sink].push_back(N * N + getNode(i, j));
        C[N * N + getNode(i, j)][Sink] = b[i][j] - '0';
      }
    }
  int ans = getMaxFlow(G, Source, Sink);
  cout << ans << '\n';
  return 0;
}