#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const LL SHIFT = 1000000000LL;
static const LL INF   = 1000001LL;
static const LL MAX   = 1000000LL;
static const int NV = 5002;
typedef LL            flow;
typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;
//typedef flow          flow_graph[NV][NV];
typedef map< vert,map<vert,flow> > flow_graph;

flow dinic_dfs( graph& G, flow_graph& F, vert v, vert D,
                int LV[], flow flow_in, int blocked[] )
{
	flow flow_out = 0;
	for(int i=0; i!=G[v].size(); ++i) {
		int u = G[v][i];
		if( LV[v]+1==LV[u] && F[v][u] ) {
			flow f = min(flow_in-flow_out, F[v][u]);
			if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blocked)) ) {
				F[v][u]  -= f;
				F[u][v]  += f;
				flow_out += f;
				if( flow_in == flow_out ) return flow_out;
			}
		}
	}
	blocked[v] = (flow_out==0);
	return flow_out;
}

flow maxFlow( graph& G, flow_graph& F, vert S, vert D )
{
	for( flow total=0 ;; ) {
		int LV[NV] = {0};
		vector<int> Q(1, S);
		for(int lv=1; !Q.empty(); ++lv) {
			vector<int> Q2;
			for(int i=0; i!=Q.size(); ++i) {
				edges& ne = G[Q[i]];
				for(int j=0; j!=ne.size(); ++j)
					if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
						LV[ne[j]]=lv, Q2.push_back(ne[j]);
			}
			Q.swap(Q2);
		}

		if( !LV[D] )
			return total;

		int blocked[NV] = {};
		total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked );
	}
}

flow_graph F;
class BarbarianInvasion {
public:
	int minimalDetachment(vector <string> countryMap, vector <int> detachmentSize) 
	{
		int H = countryMap.size();
		int W = countryMap[0].size();
		vert OUT = 0, CAP = 1;

		graph G(H*W*2+2);
		flow_graph F;

		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
		{
			if( countryMap[y][x] == '-' )
				continue;

			vert CUR_A = countryMap[y][x] == '*' ? CAP : (y*W+x)*2+2; // ==>A->B==>
			vert CUR_B = countryMap[y][x] == '*' ? CAP : (y*W+x)*2+3;
			if( countryMap[y][x] != '*' && countryMap[y][x] != '-' ) {
				G[CUR_A].push_back(CUR_B);
				G[CUR_B].push_back(CUR_A);
				F[CUR_A][CUR_B] = SHIFT + detachmentSize[countryMap[y][x]-'A'];
				F[CUR_B][CUR_A] = 0;
			}

			int DY[] = {-1,+1,0,0}, DX[]={0,0,-1,+1};
			for(int d=0; d<4; ++d)
			{
				int dy = DY[d], dx = DX[d];
				if( 0<=y+dy && y+dy<H && 0<=x+dx && x+dx<W )
				{
					if( countryMap[y+dy][x+dx] == '-' )
						continue;
					if( countryMap[y+dy][x+dx] == '*' )
						continue;
					vert ADJ_B = ((y+dy)*W+(x+dx))*2+3;
					G[ADJ_B].push_back(CUR_A);
					G[CUR_A].push_back(ADJ_B);
					F[ADJ_B][CUR_A] = SHIFT+INF;
					F[CUR_A][ADJ_B] = 0;
				}
				else
				{
					G[OUT].push_back(CUR_A);
					G[CUR_A].push_back(OUT);
					F[OUT][CUR_A] = SHIFT+INF;
					F[CUR_A][OUT] = 0;
				}
			}
		}

		flow mf = maxFlow(G, F, OUT, CAP);
		return mf%SHIFT;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string countryMap_[] = {"ABA",
 "A*A",
 "AAA"};
	  vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); 
	int detachmentSize_[] = {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	  vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); 
	int RetVal = 5; 
	return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); }
int Test_(Case_<1>) {
	string countryMap_[] = {"CCCC",
 "-BAC",
 "-*AC",
 "--AC"};
	  vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); 
	int detachmentSize_[] = {5,20,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	  vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); 
	int RetVal = 25; 
	return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); }
int Test_(Case_<2>) {
	string countryMap_[] = {"A----A",
 "-AAAA-",
 "-AA*A-",
 "-AAAA-",
 "A----A"};
	  vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); 
	int detachmentSize_[] = {9,8,2,5,3,2,1,2,6,10,4,6,7,1,7,8,8,8,2,9,7,6,5,1,5,10};
	  vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); 
	int RetVal = 0; 
	return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); }
int Test_(Case_<3>) {
	string countryMap_[] = {"-A-----",
 "-BCCC*-",
 "-A-----"};
	  vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); 
	int detachmentSize_[] = {1,5,10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	  vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); 
	int RetVal = 5; 
	return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); }
int Test_(Case_<4>) {
	string countryMap_[] = {"WANNABRUTEFORCEMEHUH",
 "ASUDQWNHIOCASFIUQISA",
 "UWQD-ASFFC-AJSQOOWE-",
 "-----*Y--AVSSFIUQISA",
 "UWQD-ASFFC-AJSQOOWE-",
 "JUFDIFD-CHBVISBOOWE-",
 "WANNABRUTEFORCEMEHUH"};
	  vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); 
	int detachmentSize_[] = {87,78,20,43,30,12,9,18,57,93,32,60,64,9,69,74,74,78,12,81,63,57,48,8,44,95};
	  vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); 
	int RetVal = 218; 
	return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); }
int Test_(Case_<5>) {
	string countryMap_[] = {
 "WOMP-PVE-LV-NB-J---E",
 "K--MKNWSOLFKK-JHP--W",
 "BE-K--MPP-YGZI-DMVKG",
 "JGWRXQ-UYEROYGC--SZR",
 "KFYN--CRX*N--JS-M-S-",
 "TBFGG-OL--D-DY--RR-A",
 "AMJT-NV--P--XF-YUSBM",
 "HJ-C--KHU-KDZHERUUHM",
 "-FGQZOI-A--ZIPB--QOT",
 "PNLI-RWYZG-MNV-SUNJD",
 "YJ-EJLD-H---K-HSVZF-",
 "LKPGZGF-IQ-ETYQJ-VAR",
 "UGS--V-BOUGG-SMO--Q-",
 "DFVWRHC-D-XES-MEQMCQ",
 "L-PUXR-C-HL-WX-NFJJR",
 "-A-JLUQX--VWPFENHJZ-",
 "HNG-NQAF-VVCUU-WU-HL"};
	  vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); 
	int detachmentSize_[] = {
96882, 90320, 19358, 82732, 80637, //ABCDE
88638, 43236, 34294, 49732, 86156, //FGHIJ
 1186, 84308, 49312, 92789, 49312, //KLMNO
14324, 27420, 32881, 73865, 91069, //PQRST
 7266, 60052, 98451, 84652, 3991, 58948}; //UVWXYZ
	  vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); 
	int RetVal = 69753; 
	return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE

