#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int n, m;
  char x;
  cin >> n >> m;
  cin >> x;
  vector<vector<char> > c(n + 1);
  char gar;
  vector<char> u1(m + 1, '.');
  for (int j = 0; j < (n + 1); ++j) c[j] = u1;
  bool b[n][m];
  stack<pair<int, int> > s, t;
  for (int i = 0; i < (n); ++i) {
    scanf("%c", &gar);
    for (int j = 0; j < (m); ++j) {
      scanf("%c", &c[i][j]);
      if (c[i][j] == x) s.push({i, j});
      b[i][j] = false;
    }
  }
  while (!s.empty()) {
    auto k = s.top();
    int u = k.first;
    int v = k.second;
    s.pop();
    if (u > 0 && c[u - 1][v] != x) t.push({u - 1, v});
    if (v > 0 && c[u][v - 1] != x) t.push({u, v - 1});
    if (u + 1 < n && c[u + 1][v] != x) t.push({u + 1, v});
    if (v + 1 < m && c[u][v + 1] != x) t.push({u, v + 1});
  }
  int cou = 0;
  while (!t.empty()) {
    auto k = t.top();
    t.pop();
    int u = k.first;
    int v = k.second;
    if (c[u][v] == '.') continue;
    if ((u > 0 && b[u - 1][v] == 1 && c[u - 1][v] == c[u][v]) ||
        (v > 0 && c[u][v - 1] == c[u][v] && b[u][v - 1] == 1) ||
        (u + 1 < n && c[u + 1][v] == c[u][v] && b[u + 1][v] == 1) ||
        (v + 1 < m && c[u][v + 1] == c[u][v] && b[u][v + 1] == 1))
      b[u][v] = 1;
    else {
      cou++;
      b[u][v] = 1;
    }
  }
  cout << cou << '\n';
  return 0;
}
