#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int r, c;
char a[N][N];
bool vis[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> r >> c;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
cin >> a[i][j];
}
}
char x = a[1][1];
bool only = true;
int sx = -1, sy = -1;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (a[i][j] != '*' && a[i][j] != x) only = false;
if (a[i][j] == x && sx == -1) {
sx = i;
sy = j;
}
}
}
if (only) {
cout << 1 << '\n';
return 0;
}
queue<pair<int,int>> q;
q.push({sx, sy});
vis[sx][sy] = 1;
while (!q.empty()) {
int x1 = q.front().first;
int y1 = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int nx = x1 + dx[k];
int ny = y1 + dy[k];
if (nx < 1 || nx > r || ny < 1 || ny > c) continue;
if (vis[nx][ny]) continue;
if (a[nx][ny] != x) continue;
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
if (a[i][j] == x && !vis[i][j]) {
cout << 3 << '\n';
return 0;
}
}
}
cout << 2 << '\n';
return 0;
}