#include <bits/stdc++.h>
using namespace std;
int n, m, ans, cnt;
map<char, int> vis;
char s[111][111];
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
void dfs(int x0, int y0) {
  for (int i = 0; i < 4; i++) {
    int x = x0 + dx[i];
    int y = y0 + dy[i];
    if (x >= 0 && x < n && y >= 0 && y < m && s[x][y] != s[x0][y0] &&
        vis[s[x][y]] == 1) {
      vis[s[x][y]] = 2;
      cnt++;
    }
  }
  return;
}
int main() {
  char c;
  while (cin >> n >> m >> c) {
    int x0, y0;
    if (n == 0 && m == 0) break;
    vis.clear();
    memset(s, 0, sizeof(s));
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      for (int j = 0; j < m; j++)
        if (s[i][j] != '.')
          vis[s[i][j]] = 1;
        else
          vis[s[i][j]] = 0;
    }
    ans = -1;
    cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == c) dfs(i, j);
        ans = max(ans, cnt);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
