1 #include<bits/stdc++.h> 2 #define myp pair<int,int> 3 #define mp make_pair 4 using namespace std; 5 const int N=30,N2=900,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},oo=2e9; 6 int n,m,d,tot,super_s,super_t,h[N][N],sum; 7 inline int getid(int a,int b){return (a-1)*m+b;} 8 inline int getdis(int x1,int y1,int x2,int y2){return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);} 9 struct Edge{ 10 int from,to,flow,cap; 11 Edge(int _from=0,int _to=0,int _flow=0,int _cap=0):from(_from),to(_to),flow(_flow),cap(_cap){} 12 }; 13 vector<Edge>edge; 14 vector<int>point[N2]; 15 int edge_tot; 16 void add_edge(int f,int t,int c){ 17 // cout<<f<<" "<<t<<" "<<c<<endl; 18 edge.push_back(Edge(f,t,0,c)); 19 point[f].push_back(edge_tot++); 20 edge.push_back(Edge(t,f,0,0)); 21 point[t].push_back(edge_tot++); 22 return; 23 } 24 int dis[N][N]; 25 int level[N2]; 26 bool d_bfs(){ 27 memset(level,0,sizeof(level)); 28 queue<int>q; 29 int x; 30 q.push(super_s); 31 level[super_s]=1; 32 while(!q.empty()){ 33 x=q.front();q.pop(); 34 for(int i=0;i<point[x].size();i++){ 35 if(edge[point[x][i]].cap<=edge[point[x][i]].flow) continue; 36 if(!level[edge[point[x][i]].to]){ 37 level[edge[point[x][i]].to]=level[x]+1; 38 q.push(edge[point[x][i]].to); 39 } 40 } 41 } 42 return level[super_t]; 43 } 44 int dfs(int k,int a){ 45 if(!a||k==super_t) return a; 46 int ans=0,temp,x; 47 for(int i=0;i<point[k].size();i++){ 48 Edge& e=edge[point[k][i]]; 49 x=edge[point[k][i]].to; 50 if(level[x]==level[k]+1&&edge[point[k][i]].cap>edge[point[k][i]].flow&&(temp=dfs(x,min(a,edge[point[k][i]].cap-edge[point[k][i]].flow)))){ 51 ans+=temp,a-=temp; 52 edge[point[k][i]].flow+=temp,edge[point[k][i]^1].flow-=temp; 53 if(!a) return ans; 54 } 55 } 56 return ans; 57 } 58 int dinic(){ 59 int ans=0; 60 while(d_bfs()) ans+=dfs(super_s,oo); 61 return ans; 62 } 63 int main(){ 64 int t1; 65 char c[30]; 66 scanf("%d%d%d",&n,&m,&d); 67 tot=n*m,super_s=tot+tot+1,super_t=super_s+1; 68 for(int i=1;i<=n;i++){ 69 scanf("%s",&c); 70 for(int j=1;j<=m;j++){ 71 h[i][j]=c[j-1]-'0'; 72 if(h[i][j]){t1=getid(i,j);add_edge(t1,t1+tot,h[i][j]);} 73 } 74 } 75 for(int i=1;i<=n;i++) 76 for(int j=1;j<=m;j++) if(h[i][j]){ 77 if(i<d+1||j<d+1||i+d>n||j+d>m) add_edge(getid(i,j)+tot,super_t,oo); 78 for(int ii=1;ii<=n;ii++) 79 for(int jj=1;jj<=m;jj++)if(h[ii][jj]&&getdis(i,j,ii,jj)<=d*d) add_edge(getid(i,j)+tot,getid(ii,jj),oo); 80 } 81 for(int i=1;i<=n;i++){ 82 scanf("%s",&c); 83 for(int j=1;j<=m;j++){ 84 if(c[j-1]=='L'){ 85 add_edge(super_s,getid(i,j),1); 86 sum++; 87 } 88 } 89 } 90 printf("%d ",sum-dinic()); 91 return 0; 92 }