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 MAXN = 1e6 + 5000;
const long double eps = (1e-12);
const int logn = 21;
const double Pi = acos(-1.0);
const long long pp = 1000000000 + 7;
int lowbit(int x) { return x & (-x); }
const int INF = 1e9;
int n, t;
int idx(int x, int y) { return (x - 1) * n + y; }
pair<int, int> ixd(int x) {
  int yy = x % n;
  if (yy == 0) yy = n;
  return make_pair((x - 1) / n + 1, yy);
}
int g1[100][100];
int val1[105];
int g2[100][100];
int val2[105];
char s[MAXN];
int dx[5] = {-1, 1, 0, 0};
int dy[5] = {0, 0, 1, -1};
struct Edge {
  int from, to;
  long long cap, flow;
};
struct Dinic {
  int n, tmp, s, t;
  vector<Edge> edges;
  vector<int> G[MAXN];
  bool vis[MAXN];
  int d[MAXN];
  int cur[MAXN];
  void init(int n, int s, int t) {
    this->n = n, this->s = s, this->t = t;
    edges.clear();
    for (int i = 1; i <= n; i++) G[i].clear();
  }
  void AddEdge(int from, int to, long long cap) {
    edges.push_back({from, to, cap, 0});
    edges.push_back({to, from, 0, 0});
    tmp = edges.size();
    G[from].push_back(tmp - 2);
    G[to].push_back(tmp - 1);
  }
  bool BFS() {
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    q.push(s);
    d[s] = 0, vis[s] = 1;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (int i = 0; i < G[x].size(); i++) {
        Edge& e = edges[G[x][i]];
        if (!vis[e.to] && e.cap > e.flow) {
          vis[e.to] = 1;
          d[e.to] = d[x] + 1;
          q.push(e.to);
        }
      }
    }
    return vis[t];
  }
  long long DFS(int x, long long a) {
    if (x == t || a == 0) return a;
    long long flow = 0, f;
    for (int& i = cur[x]; i < G[x].size(); i++) {
      Edge& e = edges[G[x][i]];
      if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
        e.flow += f;
        edges[G[x][i] ^ 1].flow -= f;
        flow += f;
        a -= f;
        if (a == 0) break;
      }
    }
    return flow;
  }
  long long Maxflow() {
    long long flow = 0;
    while (BFS()) {
      memset(cur, 0, sizeof(cur));
      flow += DFS(s, INF);
    }
    return flow;
  }
} dc;
int bad = 0;
int g[105];
bool vis[105][105];
void bfs() {
  queue<pair<pair<int, int>, int> > q;
  q.push(make_pair(make_pair(bad, bad), 1));
  for (int i = 1; i <= n * n; i++) {
    if (val1[i] == -1) continue;
    q.push(make_pair(make_pair(i, i), 1));
  }
  while (!q.empty()) {
    pair<int, int> tmp = q.front().first;
    int tim = q.front().second;
    q.pop();
    if (tim > t + 1) continue;
    if (vis[tmp.first][tmp.second]) continue;
    vis[tmp.first][tmp.second] = tim;
    if (tmp.first == bad) {
      g[tmp.second] = tim;
      pair<int, int> now = ixd(tmp.second);
      for (int i = 0; i < 4; i++) {
        int xx = now.first + dx[i];
        int yy = now.second + dy[i];
        if (xx < 1 || xx > n || yy < 1 || yy > n) continue;
        if (g1[xx][yy] == -1) continue;
        q.push(make_pair(make_pair(tmp.first, idx(xx, yy)), tim + 1));
      }
    }
    if (g[tmp.second] < tim && g[tmp.second] > 0) continue;
    dc.AddEdge(tmp.first, tmp.second + n * n, INF);
    if (g[tmp.second] > 0)
      continue;
    else {
      pair<int, int> now = ixd(tmp.second);
      for (int i = 0; i < 4; i++) {
        int xx = now.first + dx[i];
        int yy = now.second + dy[i];
        if (xx < 1 || xx > n || yy < 1 || yy > n) continue;
        if (g1[xx][yy] == -1) continue;
        q.push(make_pair(make_pair(tmp.first, idx(xx, yy)), tim + 1));
      }
    }
  }
}
signed main() {
  scanf("%d%d", &n, &t);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= n; j++) {
      if (s[j] == 'Z') {
        bad = idx(i, j);
        g1[i][j] = -1;
        val1[idx(i, j)] = -1;
      } else if (s[j] == 'Y') {
        g1[i][j] = -1;
        val1[idx(i, j)] = -1;
      } else {
        g1[i][j] = s[j] - '0';
        val1[idx(i, j)] = g1[i][j];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= n; j++) {
      if (s[j] == 'Z') {
        bad = idx(i, j);
        g2[i][j] = -1;
        val2[idx(i, j)] = -1;
      } else if (s[j] == 'Y') {
        g2[i][j] = -1;
        val2[idx(i, j)] = -1;
      } else {
        g2[i][j] = s[j] - '0';
        val2[idx(i, j)] = g2[i][j];
      }
    }
  }
  int ss = n * n * 2 + 1;
  int tt = ss + 1;
  dc.init(tt, ss, tt);
  for (int i = 1; i <= n * n; i++) {
    if (val1[i] != -1) dc.AddEdge(ss, i, val1[i]);
    if (val2[i] != -1) dc.AddEdge(i + n * n, tt, val2[i]);
  }
  bfs();
  int ans = dc.Maxflow();
  printf("%d\n", ans);
  return 0;
}