#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define pb push_back
#define all(x) begin(x), end(x)
#define space " "
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
void solve() {
int n, m; cin >> n >> m;
vector<string> adj(n);
for (int i = 0; i < n; i++) {
cin >> adj[i];
}
deque<pair<int, int>> dq;
dq.pb({0, 0});
vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<vector<int>> res(n, vector<int>(m, INT32_MAX));
res[0][0] = 1;
int ans = 1;
while (!dq.empty()) {
auto [x, y] = dq.front(); dq.pop_front();
for (auto [nx, ny] : dirs) {
if (x + nx >= 0 && x + nx < n && y + ny >= 0 && y + ny < m && res[x + nx][y + ny] == INT32_MAX && adj[x + nx][y + ny] != '.') {
if (adj[x + nx][y + ny] == adj[x][y]) {
dq.push_front({x + nx, y + ny});
res[x + nx][y + ny] = res[x][y];
}
else {
dq.push_back({x + nx, y + ny});
res[x + nx][y + ny] = res[x][y] + 1;
ans = max(ans, res[x + nx][y + ny]);
}
}
}
}
cout << ans;
}
int main() {
FAST_IO;
//freopen("piepie.in", "r", stdin);
//freopen("piepie.out", "w", stdout);
//TEST_CASES;
solve(); cout << endl;
/*int a; cin >> a;
for (int i = 1; i <= a; i++){
cout << "Case #" << i << ": ";
solve();
cout << endl;
}*/
}