#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;
char c[55][55];
bool visited[55][55];
int d[55][55];
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
queue<P> Q;

int main()
{
	int H,W;
	cin >> H >> W;
	int cntW = 0;
	rep(i,H)
	{
		rep(j,W)
		{
			cin >> c[i][j];
			if (c[i][j] == '.') cntW++;
		}
	}
	rep(i,55)
	{
		rep(j,55)
		{
			d[i][j] = 1e8;
		}
	}
	d[0][0] = 0;
	Q.push(P(0,0));
	while(!Q.empty())
	{
		P q = Q.front();
		int f = q.first, s = q.second;
		Q.pop();
		if (visited[f][s]) continue;
		visited[f][s] = true;
		rep(i,4)
		{
			int xx = f+dx[i], yy = s+dy[i];
			if (xx >= 0 && xx < H && yy >= 0 && yy < W && c[xx][yy] == '.')
			{
				d[xx][yy] = min(d[xx][yy],d[f][s]+1);
				Q.push(P(xx,yy));
			}
		}
	}
	/*
	rep(i,H)
	{
		rep(j,W)
		{
			cout << d[i][j] << " ";
		}
		cout << endl;
	}
	*/
	if (d[H-1][W-1] == 1e8) cout << -1 << endl;
	else cout << cntW - d[H-1][W-1] - 1 << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using ll = long long;
using P = pair<ll,ll>;
using graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

struct BIT{
  int N;
  int bit[1000010];
  void add(int a,int w){
    for(int x = a; x < N; x+=(x&-x)) bit[x]+=w;
  }
  int sum(int a){
    int ret = 0;
    for ( int x = a; x > 0;x-=(x&-x)) ret += bit[x];
    return ret;
  }
};

int main() {
  ll N;
  string S;
  cin>>N>>S;
  int Q;
  cin>>Q;
  vector<int> D,M,C;
  vector<vector<ll>> cnt(N+1,vector<ll>(3,0));
  rep(i,N){
    if(S[i]=='D') {
      D.push_back(i);
      cnt[i+1][0]++;
    }
    if(S[i]=='M') {
      M.push_back(i);
      cnt[i+1][1]++;
    }
    if(S[i]=='C') {
      C.push_back(i);
      cnt[i+1][2]++;
    }
  }
  rep(i,N){
    rep(j,3){
      cnt[i+1][j]+=cnt[i][j];
    }
  }
  vector<ll> DMcnt(N+1,0);
  rep(i,N){
    if(S[i]=='M') DMcnt[i+1]+=cnt[i+1][0];
  }
  rep(i,N) DMcnt[i+1]+=DMcnt[i];
  rep(i,Q){
    int k;
    cin>>k;
    ll ans = 0;
    for(auto x:C){
      ll p = DMcnt[x+1]-DMcnt[max(0,x-k+1)]-cnt[max(0,x-k+1)][0]*(cnt[x+1][1]-cnt[max(0,x-k+1)][1]);
      ans += p;
    }
    cout<<ans<<endl;
  }
}