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;
struct Edge {
  int v, w;
} edge[21000];
int adj[21000], head[210], e;
void addedge(int u, int v, int w) {
  edge[e].v = v;
  edge[e].w = w;
  adj[e] = head[u];
  head[u] = e++;
  edge[e].v = u;
  edge[e].w = 0;
  adj[e] = head[v];
  head[v] = e++;
}
int S, T;
int dist[210], q[210 * 2], arc[210], gap[210], inq[210], pre[210];
int SAP(int s, int t) {
  memset(arc, -1, sizeof(arc));
  memset(pre, -1, sizeof(pre));
  int qf = 0, qr = 0;
  q[qr++] = t;
  inq[t] = 1;
  gap[0]++;
  dist[t] = 0;
  while (qf < qr) {
    int u = q[qf++];
    for (int i = head[u]; i != -1; i = adj[i])
      if (!inq[edge[i].v]) {
        int v = edge[i].v;
        q[qr++] = v;
        inq[v] = 1;
        dist[v] = dist[u] + 1;
        gap[dist[v]]++;
        arc[v] = head[v];
      }
  }
  int low = 10000000, u = s, ans = 0;
  pre[s] = s;
  while (dist[s] < t) {
    bool flag = false;
    for (int &i = arc[u]; i != -1; i = adj[i])
      if (edge[i].w && dist[edge[i].v] + 1 == dist[u]) {
        flag = true;
        low = min(low, edge[i].w);
        pre[edge[i].v] = u;
        u = edge[i].v;
        if (u == t) {
          while (u != s) {
            u = pre[u];
            edge[arc[u]].w -= low;
            edge[arc[u] ^ 1].w += low;
          }
          ans += low;
          low = 10000000;
        }
        break;
      }
    if (flag) continue;
    int mindis = t + 1;
    for (int i = head[u]; i != -1; i = adj[i])
      if (edge[i].w && mindis > dist[edge[i].v]) {
        mindis = dist[edge[i].v];
        arc[u] = i;
      }
    gap[dist[u]]--;
    if (gap[dist[u]] == 0) return ans;
    dist[u] = mindis + 1;
    gap[dist[u]]++;
    u = pre[u];
  }
  return ans;
}
char str[12][12], str2[12][12];
int sci[12][12], ca[12][12], stone[62][12][12];
int dp[62][12][12][12][12];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, t;
void expand() {
  for (int k = 1; k <= t; ++k)
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) {
        if (stone[k - 1][i][j] == 1)
          stone[k][i][j] = 1;
        else if (stone[k - 1][i][j] == 2)
          stone[k][i][j] = 2;
        else {
          int nx, ny;
          for (int z = 0; z < 4; ++z) {
            nx = i + dir[z][0];
            ny = j + dir[z][1];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= n) {
              if (stone[k - 1][nx][ny] == 2) stone[k][i][j] = 2;
            }
          }
        }
      }
}
int vis[12][12];
void dfs(int x, int y, int nowx, int nowy, int k) {
  vis[nowx][nowy] = 1;
  dp[k][x][y][nowx][nowy] = 1;
  if (k >= t || stone[k][nowx][nowy]) return;
  int nx, ny;
  for (int z = 0; z < 4; ++z) {
    nx = nowx + dir[z][0];
    ny = nowy + dir[z][1];
    if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && !vis[nx][ny]) {
      if (stone[k][nx][ny]) continue;
      dfs(x, y, nx, ny, k + 1);
    }
  }
}
int main() {
  scanf("%d%d", &n, &t);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", str[i] + 1);
    for (int j = 1; j <= n; ++j) {
      if (str[i][j] == 'Z')
        stone[0][i][j] = 2;
      else if (str[i][j] == 'Y')
        stone[0][i][j] = 1;
      else
        sci[i][j] = str[i][j] - '0';
    }
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%s", str2[i] + 1);
    for (int j = 1; j <= n; ++j) {
      if (str2[i][j] >= '0' && str2[i][j] <= '9') ca[i][j] = str2[i][j] - '0';
    }
  }
  expand();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (sci[i][j]) {
        memset(vis, 0, sizeof(vis));
        dfs(i, j, i, j, 0);
      }
  for (int k = 1; k <= t; ++k)
    for (int x = 1; x <= n; ++x)
      for (int y = 1; y <= n; ++y)
        for (int nx = 1; nx <= n; ++nx)
          for (int ny = 1; ny <= n; ++ny) {
            dp[k][x][y][nx][ny] += dp[k - 1][x][y][nx][ny];
          }
  S = 0;
  T = n * n * 2 + 1;
  memset(head, -1, sizeof(head));
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) addedge(S, (i - 1) * n + j, sci[i][j]);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) addedge(n * n + (i - 1) * n + j, T, ca[i][j]);
  for (int x = 1; x <= n; ++x)
    for (int y = 1; y <= n; ++y)
      for (int nx = 1; nx <= n; ++nx)
        for (int ny = 1; ny <= n; ++ny)
          if (sci[x][y] && ca[nx][ny]) {
            if (dp[t][x][y][nx][ny])
              addedge((x - 1) * n + y, n * n + (nx - 1) * n + ny, 10000000);
          }
  int ans = SAP(S, T);
  printf("%d\n", ans);
  return 0;
}