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(3)
using namespace std;
struct Maxflow {
  struct edge {
    int from, to;
    long long flow, capacity;
  };
  int s, t;
  vector<edge> edg;
  vector<int> g[7100];
  int dist[7100], visited[7100];
  int cur[7100];
  void init() {
    edg.clear();
    int i;
    for (i = 0; i < 7100; i++) {
      g[i].clear();
    }
  }
  void addedge(int from, int to, long long capacity) {
    edge e1 = edge{from, to, 0ll, capacity};
    edge e2 = edge{to, from, 0ll, 0ll};
    edg.push_back(e1), edg.push_back(e2);
    g[from].push_back((int)edg.size() - 2);
    g[to].push_back((int)edg.size() - 1);
  }
  bool bfs() {
    memset(visited, 0, sizeof(visited));
    memset(dist, 0, sizeof(dist));
    queue<int> q;
    q.push(s);
    visited[s] = 1;
    dist[s] = 0;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int i = 0; i < (int)g[v].size(); i++) {
        edge &e = edg[g[v][i]];
        int nxt = e.to;
        if (!visited[nxt] && e.capacity > e.flow) {
          dist[nxt] = dist[v] + 1;
          q.push(nxt);
          visited[nxt] = 1;
        }
      }
    }
    return visited[t];
  }
  long long dfs(int x, long long cp) {
    if (x == t || cp == 0) {
      return cp;
    }
    long long flow = 0, newflow;
    for (int &y = cur[x]; y < (int)g[x].size(); y++) {
      edge &e = edg[g[x][y]];
      if (dist[x] + 1 == dist[e.to]) {
        long long minn = min(cp, e.capacity - e.flow);
        newflow = dfs(e.to, minn);
        if (newflow > 0) {
          e.flow += newflow;
          edg[g[x][y] ^ 1].flow -= newflow;
          flow += newflow;
          cp -= newflow;
          if (cp == 0) {
            break;
          }
        }
      }
    }
    return flow;
  }
  long long Dinic() {
    long long flow = 0;
    while (bfs()) {
      memset(cur, 0, sizeof(cur));
      flow += dfs(s, 1000000000);
    }
    return flow;
  }
};
Maxflow mf;
char g1[14][14];
char g2[14][14];
bool visited[14][14];
int dist[14][14];
int n;
int tgt[103];
void BFS(int x, int y) {
  queue<pair<pair<int, int>, int> > q;
  int i, j;
  for (i = 0; i < 14; i++) {
    for (j = 0; j < 14; j++) {
      dist[i][j] = 111;
    }
  }
  dist[x][y] = 0;
  q.push(make_pair(make_pair(x, y), 0));
  int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (visited[p.first.first][p.first.second]) continue;
    visited[p.first.first][p.first.second] = true;
    dist[p.first.first][p.first.second] = p.second;
    for (i = 0; i < 4; i++) {
      int tx = dx[i] + p.first.first, ty = dy[i] + p.first.second;
      if (tx <= 0 || tx > n || ty <= 0 || ty > n || visited[tx][ty] ||
          g1[tx][ty] == 'Y')
        continue;
      q.push(make_pair(make_pair(tx, ty), p.second + 1));
    }
  }
}
int index(int level, int x, int y) { return level * n * n + (x - 1) * n + y; }
int main() {
  int i, t, j, k;
  scanf("%d%d", &n, &t);
  for (i = 1; i <= n; i++) scanf("%s", g1[i] + 1);
  for (i = 1; i <= n; i++) scanf("%s", g2[i] + 1);
  mf.s = 0, mf.t = 7100 - 2;
  int x = 0, y = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (g1[i][j] == 'Z') {
        x = i, y = j;
        break;
      }
    }
  }
  for (i = 1; i <= n * n; i++) tgt[i] = 7100 - 3 - (n * n - i);
  BFS(x, y);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (g1[i][j] >= '0' && g1[i][j] <= '9')
        mf.addedge(mf.s, index(0, i, j), g1[i][j] - '0');
      if (g2[i][j] >= '0' && g2[i][j] <= '9')
        mf.addedge(tgt[index(0, i, j)], mf.t, g2[i][j] - '0');
      mf.addedge(index(0, i, j), tgt[index(0, i, j)], 1000000000);
    }
  }
  int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
  for (i = 1; i <= t; i++) {
    for (j = 1; j <= n; j++) {
      for (k = 1; k <= n; k++) {
        if (g1[j][k] == 'Y' || g1[j][k] == 'Z') continue;
        for (int l = 0; l < 4; l++) {
          int tx = dx[l] + j, ty = dy[l] + k;
          if (tx >= 1 && tx <= n && ty >= 1 && ty <= n && g1[tx][ty] != 'Y' &&
              g1[tx][ty] != 'Z') {
            if (dist[j][k] >= i && dist[tx][ty] >= i && g1[j][k] != 'Y' &&
                g1[j][k] != 'Z') {
              mf.addedge(index(i - 1, tx, ty), index(i, j, k), 1000000000);
            }
          }
        }
        mf.addedge(index(i, j, k), tgt[index(0, j, k)], 1000000000);
      }
    }
  }
  printf("%lld\n", mf.Dinic());
  return 0;
}