# 3 6
# HFDFFB
# AJHGDH
# DGAGEH
R,C = map(int,input().split(" "))
maps = [[] for _ in range(R)]
for i in range(R):
word = input()
for j in range(len(word)):
maps[i].append(word[j])
visited=[0]*26
dx=[-1,0,1,0]
dy=[0,1,-0,-1]
max_cnt = 0
def DFS(x,y,cnt):
global max_cnt
max_cnt = max(max_cnt,cnt)
for i in range(4):
nx,ny = x+dx[i],y+dy[i]
if nx>=0 and nx<R and ny>=0 and ny<C and visited[ord(maps[nx][ny]) - ord('A')]==0:
visited[ord(maps[nx][ny]) - ord('A')] = 1
DFS(nx,ny,cnt+1)
visited[ord(maps[nx][ny]) - ord('A')] = 0
visited[ord(maps[0][0]) - ord('A')] = 1
DFS(0,0,1)
print(max_cnt)