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

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int N = 405;
const int INF = 1e9;
char board[N][N];
int h,w;

bool is_in(int x, int y) {
  if(0 <= x && x < h && 0 <= y && y < w) return true;
  return false;
}

signed main() {
  cin >> h >> w;

  rep(i,h) rep(j,w) cin >> board[i][j];
  vector<vector<int>> d(h, vector<int> (w, INF));

  int ans = 0;
  rep(ii,h) rep(jj,w) {
    if(board[ii][jj] != '#' || d[ii][jj] != INF) continue;
    int sx = ii, sy = jj;

    queue<P> que;
    d[sx][sy] = 0;
    que.push({sx,sy});

    int cnt = 0;
    int black = 1;
    while(!que.empty()) {
      int cx = que.front().first;
      int cy = que.front().second;
      que.pop();

      for (int i = 0; i < 4; i++) {
        int nx = cx + dx[i];
        int ny = cy + dy[i];
        if(!is_in(nx, ny)) continue;
        if(board[nx][ny] == board[cx][cy] || d[nx][ny] != INF) continue;

        if(board[nx][ny] == '.') cnt++;
        else if(board[nx][ny] == '#') black++;

        d[nx][ny] = d[cx][cy] + 1;
        que.push({nx, ny});
      }
    }

    ans += cnt * black;
  }
  cout << ans << '\n';
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1<<30)-1;
const long long LINF = (1LL<<62)-1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using T = tuple<ll, ll, ll>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  string s;
  cin >> n >> s >> q;
  rep(_, q) {
    int k;
    cin >> k;
    vector<T> res(n);
    ll ans = 0;
    rep(i, n) {
      if (i)
        res[i] = res[i-1];
      if (i-k >= 0) {
        if (s[i-k] == 'D') {
          get<2>(res[i]) -= get<1>(res[i]);
          get<0>(res[i])--;
        }
        if (s[i-k] == 'M')
          get<1>(res[i])--;
      }
      if (s[i] == 'D')
        get<0>(res[i])++;
      if (s[i] == 'M') {
        get<1>(res[i])++;
        get<2>(res[i]) += get<0>(res[i]);
      }
      if (s[i] == 'C')
        ans += get<2>(res[i]);
    }
    cout << ans << '\n';
  }
  return 0;
}