#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define double long double
using namespace std;
using namespace __gnu_pbds;
long dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
long dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = (int)1e9 + 7;
double logBase(int a, int b) { return (double)log2l(a) / log2l(b); }
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// int inv(int a, int mod) { return a <= 1 ? a : mod - (int)(mod / a) * inv(mod % a, mod) % mod; }
int pmod(int x, int p, int mod)
{
int ans = 1;
int pre = x % mod;
while (p)
{
if (p % 2)
ans *= (pre) % mod;
p /= 2;
ans %= mod;
pre = (pre * pre) % mod;
}
return ans;
}
int inv(int a, int mod) { return pmod(a, mod - 2, mod); }
void fileIO()
{
#ifndef ONLINE_JUDGE
freopen("odometer.in", "r", stdin);
freopen("odometer.out", "w", stdout);
#endif
}
/////ost.order_of_key (k) : number of items strictly smaller than k .
//// ost.find_by_order(k) : K-th element in a set (counting from zero).
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 2;
int h, w;
vector<string> S;
bool inside(int x, int y)
{
return (x >= 0) && (x < h) && (y >= 0) && (y < w) && (S[x][y] != '.');
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
// fileIO();
int Test_cases = 1;
// cin >> Test_cases;
while (Test_cases--)
{
cin >> h >> w;
S.resize(h);
for (int i = 0; i < h; i++)
{
cin >> S[i];
}
deque<pair<int, int>> dq;
dq.push_back({0, 0});
vector<vector<int>> dis(h, vector<int>(w, -1));
dis[0][0] = 1;
int ans = 0;
while (dq.size())
{
auto top = dq.front();
dq.pop_front();
ans = max(ans, dis[top.first][top.second]);
for (int i = 0; i < 4; i++)
{
int x = top.first + dx[i];
int y = top.second + dy[i];
if (!inside(x, y))
{
continue;
}
if (dis[x][y] != -1)
{
continue;
}
if (S[x][y] == S[top.first][top.second])
{
dq.push_front({x, y});
dis[x][y] = dis[top.first][top.second];
}
else
{
dq.push_back({x, y});
dis[x][y] = dis[top.first][top.second] + 1;
}
}
}
cout << ans << endl;
}
return 0;
}