#include <bits/stdc++.h>
#define mid (int)((lt + rt) >> 1)
#define Max(a, b, c) max(a, max(b, c))
#define Min(a, b, c) min(a, min(b, c))
using namespace std;
const int N = 1005;
int n, m, d, s, t, cnt = 1, maxi;
int pre[N], inc[N], head[N], dis[N][N];
bool vis[N];
char c[N][N];
struct node{
int to, nxt, val;
}edge[N * N];
void add(int ss, int ee, double cc){
cnt++;
edge[cnt].to = ee;
edge[cnt].val = cc;
edge[cnt].nxt = head[ss];
head[ss] = cnt;
cnt++;
edge[cnt].to = ss;
edge[cnt].val = 0;
edge[cnt].nxt = head[ee];
head[ee] = cnt;
}
bool bfs(){
memset(vis, 0, sizeof vis);
queue<int> q;
q.push(s);
vis[s] = 1;
inc[s] = INT_MAX;
while(!q.empty()){
int x = q.front();
q.pop();
for(int i = head[x]; i; i = edge[i].nxt){
if(edge[i].val){
int y = edge[i].to;
if(vis[y]) continue;
inc[y] = min(inc[x], edge[i].val);
vis[y] = 1;
pre[y] = i;
q.push(y);
if(y == t) return 1;
}
}
}
return 0;
}
void update(){
int x = t;
while(x != s){
int i = pre[x];
edge[i].val -= inc[t];
edge[i ^ 1].val += inc[t];
x = edge[i ^ 1].to;
}
maxi += inc[t];
}
int cal(int sx, int sy, int ex, int ey){ return pow(sx - ex, 2) + pow(sy - ey, 2); }
int p(int x, int y){ return (x - 1) * n + y; }
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int sum = 0;
cin >> n >> m >> d;
s = 0, t = n * m * 2 + 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
dis[i][j] = c[i][j] - '0';
if(c[i][j] != '0') add(p(i, j), p(i, j) + n * m, c[i][j] - '0');
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
if(c[i][j] == 'L'){
sum++;
add(s, p(i, j), 1);
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(!dis[i][j]) continue;
for(int k = 1; k <= n; k++){
for(int l = 1; l <= m; l++){
if(k == i && l == j || !dis[k][l]) continue;
if(cal(i, j, k, l) <= d * d) add(p(i, j) + n * m, p(k, l), INT_MAX);
}
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(!dis[i][j]) continue;
if(min(min(i, j), min(n - i + 1, m - j + 1)) <= d) add(p(i, j), t, INT_MAX);
}
}
while(bfs()) update();
cout << sum - maxi;
return 0;
}
求助,悬关!!