#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3F3F3F3F;
int K, T;
int dist[20][20][20][20];
int cont[20][20];
char SCI[20][20];
char CAP[20][20];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};
const int MAXV = 2000;
int M, N;
int PAR[MAXV];
int MATCH;
int X[MAXV], Y[MAXV];
vector<int> adj[MAXV];
int disc[MAXV];
int augment(int u, int c) {
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (disc[v] != c) {
      disc[v] = c;
      if (PAR[v] == -1 || augment(PAR[v], c))
        return (PAR[v] = u, PAR[u] = v, 1);
    }
  }
  return 0;
}
int bpm() {
  MATCH = 0;
  for (int i = 0; i < M + N; i++) disc[i] = PAR[i] = -1;
  for (int i = 0; i < M; i++)
    if (adj[i].size() && PAR[i] == -1) MATCH += augment(i, i);
  return MATCH;
}
int main() {
  scanf(" %d %d ", &K, &T);
  for (int i = 0; i < K; i++) {
    scanf(" %s ", SCI[i]);
  }
  for (int i = 0; i < K; i++) {
    scanf(" %s ", CAP[i]);
  }
  {
    int y, x;
    for (int i = 0; i < K; i++)
      for (int j = 0; j < K; j++)
        if (SCI[i][j] == 'Z') {
          y = i;
          x = j;
          break;
        }
    for (int i = 0; i < K; i++)
      for (int j = 0; j < K; j++) cont[i][j] = INF;
    queue<pair<int, int> > q;
    q.push(pair<int, int>(y, x));
    cont[y][x] = 0;
    while (!q.empty()) {
      pair<int, int> u = q.front();
      q.pop();
      for (int i = 0; i < 4; i++) {
        int ny = u.first + dy[i];
        int nx = u.second + dx[i];
        if (ny < 0 || nx < 0 || ny >= K || nx >= K) continue;
        if (SCI[ny][nx] == 'Z' || SCI[ny][nx] == 'Y' || cont[ny][nx] != INF)
          continue;
        cont[ny][nx] = cont[u.first][u.second] + 1;
        q.push(pair<int, int>(ny, nx));
      }
    }
  }
  for (int y = 0; y < K; y++)
    for (int x = 0; x < K; x++) {
      for (int i = 0; i < K; i++)
        for (int j = 0; j < K; j++) dist[y][x][i][j] = INF;
      queue<pair<int, int> > q;
      q.push(pair<int, int>(y, x));
      dist[y][x][y][x] = 0;
      while (!q.empty()) {
        pair<int, int> u = q.front();
        q.pop();
        if (dist[y][x][u.first][u.second] >= min(T, cont[u.first][u.second]))
          continue;
        for (int i = 0; i < 4; i++) {
          int ny = u.first + dy[i];
          int nx = u.second + dx[i];
          if (ny < 0 || nx < 0 || ny >= K || nx >= K) continue;
          if (SCI[ny][nx] == 'Z' || SCI[ny][nx] == 'Y' ||
              dist[y][x][ny][nx] != INF)
            continue;
          if (dist[y][x][u.first][u.second] + 1 > min(T, cont[ny][nx]))
            continue;
          dist[y][x][ny][nx] = dist[y][x][u.first][u.second] + 1;
          q.push(pair<int, int>(ny, nx));
        }
      }
    }
  M = N = 0;
  for (int i = 0; i < K; i++)
    for (int j = 0; j < K; j++)
      if (SCI[i][j] > '0' && SCI[i][j] <= '9') {
        int c = SCI[i][j] - '0';
        for (int k = 0; k < c; k++) {
          X[M] = i;
          Y[M] = j;
          M++;
        }
      }
  for (int i = 0; i < K; i++)
    for (int j = 0; j < K; j++)
      if (CAP[i][j] > '0' && CAP[i][j] <= '9') {
        int c = CAP[i][j] - '0';
        for (int k = 0; k < c; k++) {
          X[M + N] = i;
          Y[M + N] = j;
          N++;
        }
      }
  for (int i = 0; i < M; i++)
    for (int j = M; j < M + N; j++)
      if (dist[X[i]][Y[i]][X[j]][Y[j]] != INF) {
        adj[i].push_back(j);
        adj[j].push_back(i);
      }
  printf("%d\n", bpm());
  return 0;
}
