#define judge
// Author: oceanlvr
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define mp(a, b) make_pair(a, b)
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int ninf = 0xcfcfcfcf;
const double pi = acos(-1.0);
static int faster_iostream = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
return 0;
}();
const int maxn = 30;
int used[maxn][maxn];
char maze[maxn][maxn];
int n, m, res = -inf;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
map<char, int> mm;
bool in(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }
void dfs(int x, int y, int cur) {
if (x == n - 1 && y == m - 1) {
res = max(cur, res);
return;
}
res = max(cur, res);
used[x][y] = true;
for (int i = 0; i < 4; i++) {
int dx = x + dir[i][0];
int dy = y + dir[i][1];
if (in(dx, dy) && !mm.count(maze[dx][dy]) && !used[dx][dy]) {
mm[maze[dx][dy]] = true;
used[dx][dy] = true;
dfs(dx, dy, cur + 1);
mm.erase(maze[dx][dy]);
used[dx][dy] = false;
}
}
}
int main() {
#ifndef judge
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> maze[i];
mm[maze[0][0]] = true;
dfs(0, 0, 1);
cout << res << endl;
return 0;
}