package main
import (
"bufio"
"fmt"
"os"
)
var dr = [...]int{-1, 1, 0, 0}
var dc = [...]int{0, 0, -1, 1}
var R, C int
var field [20]string
var visited [20][20]bool
var chk [26]bool
var ans int
func isValid(r, c int) bool {
return 0 <= r && r < R && 0 <= c && c < C
}
func dfs(r, c, len int) {
if ans < len {
ans = len
}
visited[r][c] = true
chk[field[r][c]-'A'] = true // use
for d := 0; d != 4; d++ {
nr := r + dr[d]
nc := c + dc[d]
if isValid(nr, nc) && !visited[nr][nc] && !chk[field[nr][nc]-'A'] {
dfs(nr, nc, len+1)
}
}
chk[field[r][c]-'A'] = false // unuse
visited[r][c] = false
}
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
fmt.Fscanln(reader, &R, &C)
for r := 0; r < R; r++ {
fmt.Fscanln(reader, &field[r])
}
dfs(0, 0, 1)
fmt.Fprintln(writer, ans)
writer.Flush()
}