#include <bits/stdc++.h>
using namespace std;
int n, m, vis[15][15][15][15], res, sci, bx, by, qx[215], qy[215], boom;
char map1[15][15], map2[15][15];
int x, y, l[215], v[215], g[215], qd[100005], qn[100005], qc[100005], ret, S, T,
    V, t;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void flood(int sx, int sy, int par) {
  int cx, cy, tx, ty;
  memset(vis[sx][sy], -1, sizeof(vis[sx][sy]));
  vis[sx][sy][sx][sy] = 0;
  qx[0] = sx;
  qy[0] = sy;
  for (int i = 0, j = 0; j >= i; i++) {
    cx = qx[i];
    cy = qy[i];
    if (par == 1 && vis[bx][by][cx][cy] != -1 &&
        vis[sx][sy][cx][cy] >= vis[bx][by][cx][cy])
      continue;
    for (int k = 0; k < 4; k++) {
      tx = cx + dx[k];
      ty = cy + dy[k];
      if (tx < 0 || ty < 0 || tx >= n | ty >= n) continue;
      if (map1[tx][ty] == 'Y') continue;
      if (vis[sx][sy][tx][ty] == -1) {
        vis[sx][sy][tx][ty] = vis[sx][sy][cx][cy] + 1;
        j++;
        qx[j] = tx;
        qy[j] = ty;
      }
    }
  }
}
void bd(int x, int y, int w) {
  qd[++t] = y;
  qc[t] = w;
  qn[t] = l[x];
  l[x] = t;
}
int ISAP(int x, int y) {
  int mn = V - 1, F = 0, R = y, t;
  if (x == T) return y;
  for (int i = l[x]; i; i = qn[i]) {
    if (qc[i] > 0) {
      if (v[x] == v[qd[i]] + 1) {
        t = ISAP(qd[i], min(qc[i], R));
        qc[i] -= t;
        qc[i ^ 1] += t;
        R -= t;
        F += t;
        if (v[S] >= V) return F;
        if (!R) break;
      }
      if (v[qd[i]] < mn) mn = v[qd[i]];
    }
  }
  if (!F) {
    g[v[x]]--;
    if (!g[v[x]]) v[S] = V;
    v[x] = mn + 1;
    g[v[x]]++;
  }
  return F;
}
int main() {
  scanf("%d%d", &n, &boom);
  for (int i = (0); i < (int)(n); i++) scanf("%s", map1[i]);
  for (int i = (0); i < (int)(n); i++) {
    scanf("%s", map2[i]);
    for (int j = (0); j < (int)(n); j++)
      if (map2[i][j] == 'Z') bx = i, by = j;
  }
  flood(bx, by, 0);
  S = 2 * n * n;
  T = S + 1;
  V = T + 1;
  t = 1;
  for (int i = (0); i < (int)(n); i++) {
    for (int j = (0); j < (int)(n); j++) {
      sci = map1[i][j] - '0';
      if (sci >= 1 && sci <= 9) {
        flood(i, j, 1);
        x = i * n + j;
        bd(S, x, sci);
        bd(x, S, 0);
        for (int k1 = (0); k1 < (int)(n); k1++) {
          for (int k2 = (0); k2 < (int)(n); k2++) {
            res = map2[k1][k2] - '0';
            if (res >= 1 && res <= 9) {
              if (vis[i][j][k1][k2] == -1) continue;
              if (vis[i][j][k1][k2] > boom) continue;
              if (vis[i][j][k1][k2] > vis[bx][by][k1][k2] &&
                  vis[bx][by][k1][k2] != -1)
                continue;
              y = n * n + k1 * n + k2;
              bd(x, y, sci);
              bd(y, x, 0);
            }
          }
        }
      }
    }
  }
  for (int k1 = (0); k1 < (int)(n); k1++) {
    for (int k2 = (0); k2 < (int)(n); k2++) {
      res = map2[k1][k2] - '0';
      if (res >= 1 && res <= 9) {
        y = n * n + k1 * n + k2;
        bd(y, T, res);
        bd(T, y, 0);
      }
    }
  }
  memset(v, 0, sizeof(v));
  memset(g, 0, sizeof(g));
  ret = 0;
  g[0] = V;
  while (v[S] < V) ret += ISAP(S, 1 << 29);
  printf("%d\n", ret);
  return 0;
}
