#include <bits/stdc++.h>
using namespace std;
char s[11][11];
char c[11][11];
bool infect[100][11][11];
bool mark[100][11][11];
int G[300][300];
int d[300];
int num[300];
int pre[300];
int flow[300];
int n;
int SAP() {
  int ans, i, j;
  ans = 0;
  memset(num, 0, sizeof(num));
  memset(d, 0, sizeof(d));
  i = 0;
  pre[0] = 0;
  num[0] = n + 2;
  while (d[0] <= n + 1) {
    flow[0] = 1000000000;
    for (j = 0; j <= n + 1; j++)
      if (d[i] == d[j] + 1 && G[i][j] != 0) break;
    if (j <= n + 1) {
      flow[j] = min(flow[i], G[i][j]);
      pre[j] = i;
      i = j;
      if (i == n + 1) {
        ans += flow[n + 1];
        while (pre[i] != i) {
          G[pre[i]][i] -= flow[n + 1];
          G[i][pre[i]] += flow[n + 1];
          i = pre[i];
        }
      }
    } else {
      num[d[i]]--;
      if (num[d[i]] == 0) return ans;
      d[i] = n + 2;
      for (j = 0; j <= n + 1; j++)
        if (G[i][j]) d[i] = min(d[i], d[j] + 1);
      num[d[i]]++;
      if (i) i = pre[i];
    }
  }
  return ans;
}
int main() {
  int m, t;
  int i, j, k, p, x, y;
  int d[5][2] = {-1, 0, 1, 0, 0, -1, 0, 1, 0, 0};
  while (cin >> m >> t) {
    n = m * m * 2;
    for (i = 1; i <= m; i++)
      for (j = 1; j <= m; j++) {
        cin >> s[i][j];
        if (s[i][j] == 'Z') {
          x = i;
          y = j;
        }
      }
    for (i = 1; i <= m; i++)
      for (j = 1; j <= m; j++) cin >> c[i][j];
    memset(infect, 0, sizeof(infect));
    infect[0][x][y] = true;
    for (k = 1; k <= t; k++) {
      for (i = 1; i <= m; i++)
        for (j = 1; j <= m; j++) {
          if (infect[k - 1][i][j]) {
            for (p = 0; p < 5; p++) {
              x = i + d[p][0];
              y = j + d[p][1];
              if (x < 0 || x > m || y < 0 || y > m) continue;
              if (s[x][y] == 'Y') continue;
              infect[k][x][y] = true;
            }
          }
        }
    }
    memset(G, 0, sizeof(G));
    for (i = 1; i <= m; i++)
      for (j = 1; j <= m; j++) {
        if (c[i][j] >= '1' && c[i][j] <= '9')
          G[(i - 1) * m + j + m * m][n + 1] = c[i][j] - '0';
        if (s[i][j] >= '1' && s[i][j] <= '9') {
          G[0][(i - 1) * m + j] += s[i][j] - '0';
          memset(mark, 0, sizeof(mark));
          mark[0][i][j] = true;
          for (k = 1; k <= t; k++) {
            for (x = 1; x <= m; x++)
              for (y = 1; y <= m; y++) {
                if (infect[k - 1][x][y]) continue;
                if (mark[k - 1][x][y]) {
                  for (p = 0; p < 5; p++) {
                    int tx = x + d[p][0];
                    int ty = y + d[p][1];
                    if (tx < 1 || ty > m || ty < 1 || ty > m) continue;
                    if (s[tx][ty] == 'Y') continue;
                    if (!infect[k - 1][tx][ty]) {
                      mark[k][tx][ty] = true;
                      if (c[tx][ty] >= '1' && c[tx][ty] <= '9')
                        G[(i - 1) * m + j][(tx - 1) * m + ty + m * m] =
                            s[i][j] - '0';
                    }
                  }
                }
              }
          }
        }
      }
    cout << SAP() << endl;
  }
}
