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>
#pragma GCC optimize("Ofast")
#pragma comment(linker, "/STACK:210000000")
using namespace std;
const int INF = 2e9 + 9;
const long long INF1 = 8e15 + 9;
const long long MAXN = 2e5 + 7;
const long long MAXN1 = 1 << 8;
const long long MAXN2 = 2e7;
const long long MOD = 1e9 + 7;
const long long MOD1 = 1e9 + 9;
const long long ALPH = 50;
const long long PW1 = 239;
const long long PW2 = 199;
const long long PW3 = 193;
const long long PW4 = 117;
const long double EPS = 1e-13;
const long long BLOCK = 3684;
const long long BLOCK1 = 1 << 9;
void solve();
signed main() {
  srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  if (0) cin >> q;
  while (q--) solve();
}
struct Edge {
  int fl, cap, to;
  Edge() {}
  Edge(int a, int b) : to(a), fl(0), cap(b) {}
};
bool used[1 << 10], used1[MAXN1], used2[MAXN1];
int dp[MAXN1][MAXN1][MAXN1];
vector<int> g[1 << 10];
vector<Edge> edges;
int dist[MAXN1];
bool bfs(int start, int finish, int B) {
  fill(dist, dist + MAXN1, INF);
  queue<int> q;
  dist[start] = 0;
  q.emplace(start);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto &i : g[x]) {
      if (edges[i].fl + B <= edges[i].cap && dist[edges[i].to] > dist[x] + 1) {
        dist[edges[i].to] = dist[x] + 1;
        q.emplace(edges[i].to);
      }
    }
  }
  return dist[finish] != INF;
}
int dfs(int vert, int finish, int cmin, int B) {
  if (vert == finish) return cmin;
  for (auto &i : g[vert]) {
    if (edges[i].fl + B <= edges[i].cap &&
        dist[edges[i].to] == dist[vert] + 1) {
      int p =
          dfs(edges[i].to, finish, min(cmin, edges[i].cap - edges[i].fl), B);
      if (p) {
        edges[i].fl += p;
        edges[i ^ 1].fl -= p;
        return p;
      }
    }
  }
  return 0;
}
void solve() {
  int n, t, start;
  cin >> n >> t;
  int ind = 0;
  vector<pair<int, int> > people, capsule;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      char ch;
      cin >> ch;
      if (ch == 'Y' || ch == 'Z') {
        used[i * n + j] = 1;
      } else if (ch > '0')
        people.emplace_back(i * n + j, ch - '0');
      if (ch == 'Z') start = i * n + j;
    }
  }
  used1[start] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      char ch;
      cin >> ch;
      if (ch > '0' && ch <= '9')
        capsule.emplace_back(i * n + j + n * n, ch - '0');
    }
  }
  for (int i = 0; i < MAXN1; ++i) {
    if (!used[i]) dp[0][i][i] = 1;
  }
  for (int i = 1; i <= t; ++i) {
    for (int q1 = 0; q1 < n * n; ++q1) {
      for (int q2 = 0; q2 < n * n; ++q2) {
        if (dp[i - 1][q1][q2] && !used[q2]) {
          dp[i][q1][q2] = 1;
          if (q2 >= n && !used[q2 - n]) dp[i][q1][q2 - n] = 1;
          if (q2 + n < MAXN1 && !used[q2 + n]) dp[i][q1][q2 + n] = 1;
          if (q2 % n != 0 && !used[q2 - 1]) dp[i][q1][q2 - 1] = 1;
          if (q2 % n != n - 1 && !used[q2 + 1]) dp[i][q1][q2 + 1] = 1;
        }
      }
    }
    for (int j = 0; j < n * n; ++j) {
      if (used1[j]) {
        if (j >= n && !used[j - n]) used2[j - n] = used[j - n] = 1;
        if (j + n < MAXN1 && !used[j + n]) used2[j + n] = used[j + n] = 1;
        if (j % n != 0 && !used[j - 1]) used2[j - 1] = used[j - 1] = 1;
        if (j % n != n - 1 && !used[j + 1]) used2[j + 1] = used[j + 1] = 1;
      }
    }
    for (int j = 0; j < n * n; ++j) used1[j] |= used2[j];
  }
  for (int i = 0; i <= t; ++i) {
    for (int q1 = 0; q1 < n * n; ++q1) {
      for (int q2 = 0; q2 < n * n; ++q2) {
        dp[t][q1][q2] |= dp[i][q1][q2];
      }
    }
  }
  for (auto &i : people) {
    g[MAXN1 - 1].emplace_back(edges.size());
    edges.emplace_back(i.first, i.second);
    g[i.first].emplace_back(edges.size());
    edges.emplace_back(MAXN1 - 1, 0);
  }
  for (auto &i : capsule) {
    g[i.first].emplace_back(edges.size());
    edges.emplace_back(MAXN1 - 2, i.second);
    g[MAXN1 - 2].emplace_back(edges.size());
    edges.emplace_back(i.first, 0);
  }
  for (auto &i : people) {
    for (auto &j : capsule) {
      if (dp[t][i.first][j.first - n * n]) {
        g[i.first].emplace_back(edges.size());
        edges.emplace_back(j.first, j.second);
        g[j.first].emplace_back(edges.size());
        edges.emplace_back(i.first, 0);
      }
    }
  }
  for (int i = 16; i > 0; i >>= 1) {
    while (bfs(MAXN1 - 1, MAXN1 - 2, i)) {
      while (dfs(MAXN1 - 1, MAXN1 - 2, INF, i))
        ;
    }
  }
  int ans = 0;
  for (auto &i : g[MAXN1 - 1]) ans += edges[i].fl;
  cout << ans;
}