文件目录

#include <iostream>
using namespace std;
int n, m;
string s[105];
bool visc[128];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int ans = 0;
void dfs(int x, int y, int step)
{
    ans = max(ans, step);
    for (int i = 0; i < 4; i++)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if (xx >= 0 && yy >= 0 && xx < n && yy < m && !visc[s[xx][yy]])
        {
            visc[s[xx][yy]] = true;
            dfs(xx, yy, step + 1);
            visc[s[xx][yy]] = false;
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> s[i];
    visc[s[0][0]] = true;
    dfs(0, 0, 1);
    cout << ans;
    return 0;
}