https://cn.vjudge.net/problem/Gym-101982E
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;
#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int N=1e4+9;
const int M=4e2+9;
char mp[N][N];
int w[N];
//struct E
//{
// int to,nex,cap,flow;
//}edge[M];
//int head[M];
//int tot;
//int gap[N],dep[N],cur[N];
//void init()
//{
// tot=0;
// mem(head,-1);
//}
//void addedge(int u,int v,int w,int rw=0)
//{
// edge[tot].to=v;
// edge[tot].cap=w;
// edge[tot].flow=0;
// edge[tot].nex=head[u];
// head[u]=tot++;
//
// edge[tot].to=u;
// edge[tot].cap=rw;
// edge[tot].flow=0;
// edge[tot].nex=head[v];
// head[v]=tot++;
//}
//int Q[N];
//void bfs(int st,int ed)
//{
// mem(dep,-1);
// mem(gap,0);
// gap[0]=1;
// int fr=0,re=0;
// dep[ed]=0;
// Q[re++]=ed;
// while(fr!=re)
// {
// int u=Q[fr++];
// for(int i=head[u];i!=-1;i=edge[i].nex)
// {
// int v=edge[i].to;
// if(dep[v]!=-1) continue;
// Q[re++]=v;
// dep[v]=dep[u]+1;
// gap[dep[v]]++;
// }
// }
//}
//int S[N];
//int sap(int st,int ed,int n)
//{
// bfs(st,ed);
// memcpy(cur,head,sizeof(head));
// int top=0;
// int u=st;
// int ans=0;
// while(dep[st]<n)
// {
// if(u==ed)
// {
// int Min=INF;
// int inser;
// for(int i=0;i<top;i++)
// {
// if(Min>edge[S[i]].cap-edge[S[i]].flow)
// {
// Min=edge[S[i]].cap-edge[S[i]].flow;
// inser=i;
// }
// }
// for(int i=0;i<top;i++)
// {
// edge[S[i]].flow+=Min;
// edge[S[i]^1].flow-=Min;
// }
// ans+=Min;
// top=inser;
// u=edge[S[top]^1].to;
// continue;
// }
// bool flag=false;
// int v;
// for(int i=cur[u];i!=-1;i=edge[i].nex)
// {
// v=edge[i].to;
// if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
// {
// flag=true;
// cur[u]=i;
// break;
// }
// }
// if(flag)
// {
// S[top++]=cur[u];
// u=v;
// continue;
// }
// int Min=n;
// for(int i=head[u];i!=-1;i=edge[i].nex)
// {
// if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
// {
// Min=dep[edge[i].to];
// cur[u]=i;
// }
// }
// gap[dep[u]]--;
// if(!gap[dep[u]]) return ans;
// dep[u]=Min+1;
// gap[dep[u]]++;
// if(u!=st) u=edge[S[--top]^1].to;
// }
// return ans;
//}
int idx[4]={0,0,1,-1};
int idy[4]={1,-1,0,0};
int head[N],tot,dist[N],cur[N];
int s,t;
struct Edge
{
int from,to,next,flow,cap;
}edge[N*10];
void addedge(int a,int b,int c)
{
edge[tot].from = a;edge[tot].to = b;edge[tot].cap = c;edge[tot].flow = 0;edge[tot].next = head[a];head[a] = tot++;
edge[tot].from = b;edge[tot].to = a;edge[tot].cap = 0;edge[tot].flow = 0;edge[tot].next = head[b];head[b] = tot++;
}
bool BFS()
{
memset(dist,0,sizeof(dist));
dist[s] = 1;
queue<int> que;
que.push(s);
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i = head[u];~i;i = edge[i].next)
{
int to = edge[i].to;
if(!dist[to]&&edge[i].cap > edge[i].flow)
{
dist[to] = dist[u] + 1;
que.push(to);
}
}
}
if(!dist[t])return false;
return true;
}
int DFS(int pos,int MinFlow)
{
if(pos==t||MinFlow==0)return MinFlow;
int flow = 0;
for(int &i = cur[pos];~i;i = edge[i].next)
{
int to = edge[i].to;
if(dist[to]==dist[pos]+1&&edge[i].cap > edge[i].flow){
int res = DFS(edge[i].to,min(MinFlow,edge[i].cap-edge[i].flow));
edge[i].flow+=res;
edge[i^1].flow-=res;
flow+=res;
MinFlow-=res;
if(MinFlow==0)break;
}
}
return flow;
}
int Dinic(int st,int et)
{
if(st==et)return 0;
int MaxFlow = 0;
while(BFS())
{
for(int i = 0;i<=t;i++)cur[i] = head[i];
int ans = DFS(st,INF);
if(ans>=INF)return -1;
MaxFlow+=ans;
}
return MaxFlow;
}
void init()
{
tot=0;
mem(head,-1);
}
int main()
{
//FAST_IO;
//freopen("input.txt","r",stdin);
int stx,sty;
int n,m,c;
init();
scanf("%d%d%d",&m,&n,&c);
for(int i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='B')
{
stx=i;
sty=j;
}
}
}
for(int i=0;i<c;i++) scanf("%d",&w[i]);
s=(stx-1)*m+sty;
t=2*n*m+100;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int id=(i-1)*m+j;
if(mp[i][j]=='.'||mp[i][j]=='B')
{
addedge(id,id+n*m,INF);
}
else addedge(id,id+n*m,w[mp[i][j]-'a']);
for(int k=0;k<4;k++)
{
int x=i+idx[k];
int y=j+idy[k];
int id2=(x-1)*m+y;
if(x>=1&&x<=n&&y>=1&&y<=m) addedge(id+n*m,id2,INF);
else addedge(id+n*m,t,INF);
}
}
}
printf("%d\n",Dinic(s,t));
return 0;
}