#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

const int INF=0x3f3f3f3f;

char s[2000][2000];
int d[1200][1200];
int dist[32][32];
P pos[32];

P dp[1<<10][32][11];

struct st{
	int i,j,k;
	P cost;
};

bool operator<(P a,P b){
	if(a.first!=b.first)return a.first<b.first;
	return a.second>b.second;
}
bool operator<(st a,st b){
	return a.cost>b.cost;
}

int sc[20][20];

int dx[]{1,-1,0,0},dy[]{0,0,1,-1};

bool isUpper(char c){
	return 'A'<=c&&c<='J';
}
bool isLower(char c){
	return 'a'<=c&&c<='j';
}

int get_id(char c){
	if(isdigit(c))return c-'0';
	if(isUpper(c))return c-'A'+10;
	if(isLower(c))return c-'a'+20;
	if(c=='S')return 30;
	if(c=='T')return 31;
	return -1;
}

int main(){
	int w,h;cin>>w>>h;
	rep(i,32)pos[i]=P(-1,-1);
	memset(dist,0x3f,sizeof(dist));
	rep(i,h){
		scanf("%s",s[i]);
		rep(j,w){
			int id=get_id(s[i][j]);
			if(id!=-1)pos[id]=P(i,j);
		}
	}
	rep(i,10)rep(j,10)scanf("%d",&sc[i][j]);
	rep(i,h)rep(j,w){
		if(get_id(s[i][j])==-1)continue;
		int id=get_id(s[i][j]);
		memset(d,0x3f,sizeof(d));
		queue<P>que;
		d[i][j]=0;
		que.push(P(i,j));
		while(!que.empty()){
			P p=que.front();que.pop();
			rep(k,4){
				int nx=p.first+dx[k],ny=p.second+dy[k];
				if(nx<0||nx>=h||ny<0||ny>=w)continue;
				if(s[nx][ny]=='#')continue;
				if(d[nx][ny]!=INF)continue;
				d[nx][ny]=d[p.first][p.second]+1;
				if(!isLower(s[nx][ny])&&!isUpper(s[nx][ny]))que.push(P(nx,ny));
			}
		}
		rep(k,32){
			if(pos[k].first==-1)continue;
			dist[id][k]=d[pos[k].first][pos[k].second];
		}
	}
	rep(i,1<<10)rep(j,32)rep(k,11)dp[i][j][k]=P(INF,0);
	priority_queue<st>que;
	dp[0][30][10]=P(0,0);
	que.push({0,30,10,{0,0}});
	while(!que.empty()){
		st s=que.top();que.pop();
		int i=s.i,j=s.j,k=s.k;
		if(dp[s.i][s.j][s.k]!=s.cost)continue;
		rep(t,32){
			if(t<10&&(i>>t&1))continue;
			if(10<=t&&t<20){
				if(i>>(t-10)&1)continue;
			}
			if(20<=t&&t<30){
				if(!(i>>(t-20)&1))continue;
			}
			if(dist[j][t]==INF)continue;
			int ni=(i|(t<10?1<<t:0));
			int nj=t;
			int nk=(t<10?t:k);
			P np=dp[i][j][k];
			np.first+=dist[j][nj];
			np.second+=sc[k][nk];
			if(np<dp[ni][nj][nk]){
				dp[ni][nj][nk]=np;
				que.push({ni,nj,nk,np});
			}
		}
	}
	P ans(INF,0);
	rep(k,11){
		auto p=dp[(1<<10)-1][31][k];
		if(p<ans)ans=p;
	}
	if(ans.first==INF)puts("-1");
	else cout<<ans.first<<' '<<ans.second<<endl;
}
