#include <bits/stdc++.h>
using namespace std;
class Hungary {
 public:
  static const int SIZE = 2000;
  int cnt, pos[SIZE], rev[SIZE];
  int gao(int n, vector<int> e[]) {
    cnt = 0;
    memset(pos, 255, sizeof(pos));
    memset(rev, 255, sizeof(rev));
    for (int i = 0; i < n; i++) {
      memset(v, 0, sizeof(v));
      if (match(i, e)) cnt++;
    }
    return cnt;
  }

 private:
  int v[SIZE];
  bool match(int x, vector<int> e[]) {
    for (int i = 0; i < int(e[x].size()); i++) {
      int y = e[x][i];
      if (!v[y])
        v[y] = 1;
      else
        continue;
      if (rev[y] < 0 || match(rev[y], e)) {
        pos[x] = y;
        rev[y] = x;
        return true;
      }
    }
    return false;
  }
};
using namespace std;
const int INF = 1000000000;
const int mr[] = {0, -1, 0, 1};
const int mc[] = {1, 0, -1, 0};
int a[12][12], b[12][12], f[12][12], m, t;
vector<int> e[2000];
vector<int> c[12][12];
void floodfill(int sr, int sc) {
  queue<int> qr;
  queue<int> qc;
  memset(f, 255, sizeof(f));
  qr.push(sr);
  qc.push(sc);
  f[sr][sc] = 0;
  while (!qr.empty()) {
    sr = qr.front();
    qr.pop();
    sc = qc.front();
    qc.pop();
    for (int i = 0; i < 4; i++) {
      int tr = sr + mr[i];
      int tc = sc + mc[i];
      if (a[tr][tc] >= 0 && f[tr][tc] < 0) {
        f[tr][tc] = f[sr][sc] + 1;
        qr.push(tr);
        qc.push(tc);
      }
    }
  }
}
void setmark(int sr, int sc, int cnt) {
  int v[12][12];
  queue<int> qr;
  queue<int> qc;
  memset(v, 255, sizeof(v));
  qr.push(sr);
  qc.push(sc);
  v[sr][sc] = 0;
  while (!qr.empty()) {
    sr = qr.front();
    qr.pop();
    sc = qc.front();
    qc.pop();
    if (v[sr][sc] > min(t, f[sr][sc])) continue;
    for (int j = 0; j < int(c[sr][sc].size()); j++)
      for (int i = 0; i < cnt; i++) e[m + i].push_back(c[sr][sc][j]);
    if (v[sr][sc] == min(t, f[sr][sc])) continue;
    for (int i = 0; i < 4; i++) {
      int tr = sr + mr[i];
      int tc = sc + mc[i];
      if (a[tr][tc] >= 0 && v[tr][tc] < 0) {
        v[tr][tc] = v[sr][sc] + 1;
        qr.push(tr);
        qc.push(tc);
      }
    }
  }
  m += cnt;
}
Hungary match;
int main() {
  int n, sr, sc, o = m = 0;
  scanf("%d%d", &n, &t);
  memset(a, 255, sizeof(a));
  memset(b, 255, sizeof(b));
  for (int i = 1; i <= n; i++) {
    while (getchar() != '\n')
      ;
    for (int j = 1; j <= n; j++) {
      char ch = getchar();
      if (ch >= '0' && ch <= '9') a[i][j] = ch - '0';
      if (ch == 'Z') {
        sr = i;
        sc = j;
      }
    }
  }
  floodfill(sr, sc);
  while (getchar() != '\n')
    ;
  for (int i = 1; i <= n; i++) {
    while (getchar() != '\n')
      ;
    for (int j = 1; j <= n; j++) {
      char ch = getchar();
      if (ch >= '0' && ch <= '9') b[i][j] = ch - '0';
      for (int k = 0; k < b[i][j]; k++) c[i][j].push_back(o++);
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (f[i][j] < 0) f[i][j] = INF;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j] > 0) setmark(i, j, a[i][j]);
  printf("%d\n", match.gao(m, e));
}
