#include <iostream>
#include <fstream>
int l,lmax,verif_trecere[26];
using namespace std;
void rezolvare(int x,int y, char s[21][21],int R, int C){
if(verif_trecere[s[x][y] - 'A'] || x < 0 || y < 0 || x >= R || y >= C) return;
verif_trecere[s[x][y] - 'A'] = 1;
l++;
if(l>lmax) lmax = l;
rezolvare(x-1,y,s,R,C);
rezolvare(x+1,y,s,R,C);
rezolvare(x,y-1,s,R,C);
rezolvare(x,y+1,s,R,C);
verif_trecere[s[x][y] - 'A'] = 0;
l--;
}
int main()
{
int R,C;
char s[21][21];
ifstream fin("monkey.in");
ofstream fout("monkey.out");
fin>>R>>C;
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)
fin>>s[i][j];
rezolvare(0,0,s,R,C);
fout<<lmax;
fin.close();
fout.close();
return 0;
}
Bafta in continuare la probleme!