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 M = 10;
const int inf = 1000;
const int D = 9;
const int V = (M * M - 1) * D * 2 + 2;
int n, t;
int dist[M][M];
int fireTime[M][M];
bool visited[M][M];
char scholar[M][M];
char capsule[M][M];
int minTime[M][M][M][M];
bool canReach[M][M][M][M];
int source;
int sink;
int parent[V];
bool mark[V];
vector<int> N[V];
int C[V][V];
int capacity[V][V];
vector<pair<int, int> > spos;
vector<pair<int, int> > cpos;
void read();
void setFireTime();
void setVisited();
void initializeFireTime();
void initializeDistance();
pair<int, int> getBadReactor();
bool bfs(pair<int, int>, pair<int, int>);
void add(queue<pair<int, int> > &, pair<int, int>);
bool valid(int, int);
bool block(int, int);
void printFireTime();
void printTableLine();
void initialize();
void makeGraph();
int numberOfScholars();
int numberOfCapsules();
void addEdge(int, int, int);
void printGraph();
int calculateMaximumFlow();
void setMark();
int pathCap(int, int);
void updateGraph(int);
int main() {
  read();
  setFireTime();
  initialize();
  makeGraph();
  printf("%d\n", calculateMaximumFlow());
  return 0;
}
void read() {
  cin >> n >> t;
  for (int r = 0; r < n; r++)
    for (int c = 0; c < n; c++) cin >> scholar[r][c];
  for (int r = 0; r < n; r++)
    for (int c = 0; c < n; c++) cin >> capsule[r][c];
}
void setFireTime() {
  setVisited();
  initializeFireTime();
  initializeDistance();
  pair<int, int> start = getBadReactor();
  pair<int, int> goal = pair<int, int>(-1, -1);
  bfs(start, goal);
  for (int r = 0; r < n; r++)
    for (int c = 0; c < n; c++) fireTime[r][c] = dist[r][c];
}
void setVisited() {
  for (int r = 0; r < n; r++)
    for (int c = 0; c < n; c++) visited[r][c] = 0;
}
void initializeFireTime() {
  for (int r = 0; r < n; r++)
    for (int c = 0; c < n; c++) fireTime[r][c] = inf;
}
void initializeDistance() {
  for (int r = 0; r < n; r++)
    for (int c = 0; c < n; c++) dist[r][c] = inf;
}
pair<int, int> getBadReactor() {
  pair<int, int> res;
  for (int r = 0; r < n; r++)
    for (int c = 0; c < n; c++)
      if (capsule[r][c] == 'Z') res = pair<int, int>(r, c);
  return res;
}
bool bfs(pair<int, int> start, pair<int, int> goal) {
  queue<pair<int, int> > Q;
  add(Q, start);
  int level = 0;
  bool result = false;
  while (!Q.empty()) {
    int size = Q.size();
    for (int i = 0; i < size; i++) {
      pair<int, int> v = Q.front();
      int time = fireTime[v.first][v.second];
      if (v == goal && level <= time) result = true;
      dist[v.first][v.second] = level;
      if (level < time) {
        for (int dr = -1; dr < 2; dr++) {
          for (int dc = -1; dc < 2; dc++) {
            int s = dr + dc;
            int p = dr * dc;
            if (s != 0 && p == 0) {
              int r = v.first + dr;
              int c = v.second + dc;
              if (valid(r, c) && !block(r, c) && !visited[r][c]) {
                pair<int, int> elem(r, c);
                add(Q, elem);
              }
            }
          }
        }
      }
      Q.pop();
    }
    level++;
  }
  return result;
}
void add(queue<pair<int, int> > &Q, pair<int, int> elem) {
  visited[elem.first][elem.second] = true;
  Q.push(elem);
}
bool valid(int r, int c) { return (r >= 0 && r < n && c < n && c >= 0); }
bool block(int r, int c) {
  return (capsule[r][c] == 'Y' || capsule[r][c] == 'Z');
}
void printFireTime() {
  printTableLine();
  for (int r = 0; r < n; r++) {
    for (int c = 0; c < n; c++) {
      if (c > 0) printf(" ");
      printf("| ");
      if (dist[r][c] == inf)
        printf("&");
      else
        printf("%c", char(fireTime[r][c] + int('0')));
    }
    printf(" |\n");
    printTableLine();
  }
}
void printTableLine() {
  int size = 4 * n + 1;
  for (int i = 0; i < size; i++) printf("-");
  printf("\n");
}
void initialize() {
  for (int row = 0; row < n; row++) {
    for (int col = 0; col < n; col++) {
      for (int r = 0; r < n; r++) {
        for (int c = 0; c < n; c++) {
          pair<int, int> start(row, col);
          pair<int, int> goal(r, c);
          setVisited();
          initializeDistance();
          bool reach = bfs(start, goal);
          canReach[row][col][r][c] = reach;
          if (reach) minTime[row][col][r][c] = dist[r][c];
        }
      }
    }
  }
}
void makeGraph() {
  source = 0;
  int p = numberOfScholars();
  int q = numberOfCapsules();
  sink = p + q + 1;
  for (int i = 1; i <= p; i++) addEdge(source, i, 1);
  for (int i = 1; i <= q; i++) addEdge(p + i, sink, 1);
  for (int i = 1; i <= p; i++) {
    for (int j = 1; j <= q; j++) {
      pair<int, int> start = spos[i];
      pair<int, int> end = cpos[j];
      int a = start.first;
      int b = start.second;
      int c = end.first;
      int d = end.second;
      if (canReach[a][b][c][d] && minTime[a][b][c][d] <= t)
        addEdge(i, p + j, 1);
    }
  }
}
int numberOfScholars() {
  int res = 0;
  pair<int, int> p(-1, -1);
  spos.push_back(p);
  for (int r = 0; r < n; r++) {
    for (int c = 0; c < n; c++) {
      char ch = scholar[r][c];
      if (ch >= '0' && ch <= '9') {
        int num = int(ch) - int('0');
        p = pair<int, int>(r, c);
        for (int i = 0; i < num; i++) spos.push_back(p);
        res += num;
      }
    }
  }
  return res;
}
int numberOfCapsules() {
  int res = 0;
  pair<int, int> p(-1, -1);
  cpos.push_back(p);
  for (int r = 0; r < n; r++) {
    for (int c = 0; c < n; c++) {
      char ch = capsule[r][c];
      if (ch >= '0' && ch <= '9') {
        int num = int(ch) - int('0');
        p = pair<int, int>(r, c);
        for (int i = 0; i < num; i++) cpos.push_back(p);
        res += num;
      }
    }
  }
  return res;
}
void addEdge(int u, int v, int cap) {
  N[u].push_back(v);
  N[v].push_back(u);
  C[u][v] = cap;
  capacity[u][v] = cap;
}
void printGraph() {
  printf("Source = %d  &&  Sink = %d\n", source, sink);
  for (int v = 0; v <= sink; v++) {
    printf("N[%d] = {", v);
    for (int i = 0; i < N[v].size(); i++) {
      if (i > 0) printf(", ");
      printf("%d", N[v][i]);
    }
    printf("}\n");
  }
  for (int u = 0; u <= sink; u++) {
    for (int v = 0; v <= sink; v++) {
      if (v > 0) printf(" ");
      printf("%d", capacity[u][v]);
    }
    printf("\n");
  }
}
int calculateMaximumFlow() {
  int flow = 0;
  while (true) {
    setMark();
    int cap = pathCap(source, -1);
    if (cap > 0) {
      flow += cap;
      updateGraph(cap);
    } else
      break;
  }
  return flow;
}
void setMark() {
  for (int v = 0; v < V; v++) mark[v] = false;
}
int pathCap(int v, int p) {
  mark[v] = true;
  parent[v] = p;
  if (v == sink) return 1;
  for (int i = 0; i < N[v].size(); i++) {
    int u = N[v][i];
    if (!mark[u] && C[v][u] > 0) {
      int cap = pathCap(u, v);
      if (cap > 0) return min(cap, C[v][u]);
    }
  }
  return 0;
}
void updateGraph(int cap) {
  int u, v;
  v = sink;
  while (true) {
    u = parent[v];
    if (u < 0) break;
    C[u][v] -= cap;
    C[v][u] += cap;
    v = u;
  }
}