#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long double PI = acosl(-1);
const long long infl = 3e18 + 100;
const int inf = 2e9 + 100;
const int nmax = 2e5 + 5;
const int MAXLG = log2(nmax) + 1;
vector<string> s1, s2;
int g[10][10][10][10];
int n, t;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool thikase(int i, int j) {
  if (i < n and i >= 0 and j < n and j >= 0 and s1[i][j] != 'Y' and
      s1[i][j] != 'Z')
    return true;
  return false;
}
struct FlowEdge {
  int v, u;
  long long cap, flow = 0;
  FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};
struct Dinic {
  const long long flow_inf = 1e18;
  vector<FlowEdge> edges;
  vector<vector<int>> adj;
  int n, m = 0;
  int s, t;
  vector<int> level, ptr;
  queue<int> q;
  Dinic(int n, int s, int t) : n(n), s(s), t(t) {
    adj.resize(n);
    level.resize(n);
    ptr.resize(n);
  }
  void add_edge(int v, int u, long long cap) {
    edges.emplace_back(v, u, cap);
    edges.emplace_back(u, v, 0);
    adj[v].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int id : adj[v]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[v] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  long long dfs(int v, long long pushed) {
    if (pushed == 0) return 0;
    if (v == t) return pushed;
    for (int &cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
      int id = adj[v][cid];
      int u = edges[id].u;
      if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
        continue;
      long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
      if (tr == 0) continue;
      edges[id].flow += tr;
      edges[id ^ 1].flow -= tr;
      return tr;
    }
    return 0;
  }
  long long flow() {
    long long f = 0;
    while (true) {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (long long pushed = dfs(s, flow_inf)) {
        f += pushed;
      }
    }
    return f;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  n, t;
  cin >> n >> t;
  s1.resize(n), s2.resize(n);
  for (auto &z : s1) cin >> z;
  for (auto &z : s2) cin >> z;
  pair<int, int> boma;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (s1[i][j] == 'Z') boma = {i, j};
    }
  queue<pair<int, int>> q;
  q.push(boma);
  vector<vector<int>> d(n, vector<int>(n, inf));
  d[boma.first][boma.second] = 0;
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    int i1 = u.first, j1 = u.second;
    for (int k = 0; k < 4; k++) {
      if (thikase(i1 + dx[k], j1 + dy[k]) and
          d[i1 + dx[k]][j1 + dy[k]] > d[i1][j1] + 1) {
        q.push({i1 + dx[k], j1 + dy[k]}),
            d[i1 + dx[k]][j1 + dy[k]] = d[i1][j1] + 1;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (s1[i][j] != 'Z' and s1[i][j] != 'Y') {
        vector<vector<int>> d2(n, vector<int>(n, inf));
        q.push({i, j});
        d2[i][j] = 0;
        while (!q.empty()) {
          pair<int, int> u = q.front();
          q.pop();
          int i1 = u.first, j1 = u.second;
          if (d2[i1][j1] == d[i1][j1]) continue;
          for (int k = 0; k < 4; k++) {
            if (thikase(i1 + dx[k], j1 + dy[k]) and
                d2[i1 + dx[k]][j1 + dy[k]] > d2[i1][j1] + 1) {
              q.push({i1 + dx[k], j1 + dy[k]}),
                  d2[i1 + dx[k]][j1 + dy[k]] = d2[i1][j1] + 1;
            }
          }
        }
        for (int i1 = 0; i1 < n; i1++) {
          for (int j1 = 0; j1 < n; j1++) {
            if (s1[i1][j1] != 'Z' and s1[i1][j1] != 'Y') {
              if (d2[i1][j1] <= d[i1][j1] and d2[i1][j1] <= t)
                g[i][j][i1][j1] = 1;
            }
          }
        }
      }
    }
  }
  Dinic flu(2 * n * n + 2, 0, 2 * n * n + 1);
  int sink = 2 * n * n + 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (s1[i][j] != 'Z' and s1[i][j] != 'Y') {
        flu.add_edge(0, i * n + j + 1, s1[i][j] - '0');
        flu.add_edge(i * n + j + 1 + n * n, sink, s2[i][j] - '0');
      }
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      for (int i1 = 0; i1 < n; i1++)
        for (int j1 = 0; j1 < n; j1++) {
          if (g[i][j][i1][j1]) {
            flu.add_edge(i * n + j + 1, i1 * n + j1 + 1 + n * n, inf);
          }
        }
    }
  cout << flu.flow() << endl;
}
