시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 256 MB 33453 10735 6601 30.600%

문제

세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다.

말은 상하좌우로 인접한 네 칸 중의 한 칸으로 이동할 수 있는데, 새로 이동한 칸에 적혀 있는 알파벳은 지금까지 지나온 모든 칸에 적혀 있는 알파벳과는 달라야 한다. 즉, 같은 알파벳이 적힌 칸을 두 번 지날 수 없다.

좌측 상단에서 시작해서, 말이 최대한 몇 칸을 지날 수 있는지를 구하는 프로그램을 작성하시오. 말이 지나는 칸은 좌측 상단의 칸도 포함된다.

입력

첫째 줄에 R과 C가 빈칸을 사이에 두고 주어진다. (1 ≤ R,C ≤ 20) 둘째 줄부터 R개의 줄에 걸쳐서 보드에 적혀 있는 C개의 대문자 알파벳들이 빈칸 없이 주어진다.

출력

첫째 줄에 말이 지날 수 있는 최대의 칸 수를 출력한다.

예제 입력 1

2 4
CAAB
ADCB

예제 출력 1

3
W3sicHJvYmxlbV9pZCI6IjE5ODciLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWM1NGNcdWQzMGNcdWJjYjMiLCJkZXNjcmlwdGlvbiI6IjxwPlx1YzEzOFx1Yjg1YyBSXHVjZTc4LCBcdWFjMDBcdWI4NWMgQ1x1Y2U3OFx1YzczY1x1Yjg1YyBcdWI0MWMgXHVkNDVjIFx1YmFhOFx1YzU5MVx1Yzc1OCBcdWJjZjRcdWI0ZGNcdWFjMDAgXHVjNzg4XHViMmU0LiBcdWJjZjRcdWI0ZGNcdWM3NTggXHVhYzAxIFx1Y2U3OFx1YzVkMFx1YjI5NCBcdWIzMDBcdWJiMzhcdWM3OTAgXHVjNTRjXHVkMzBjXHViY2IzXHVjNzc0IFx1ZDU1OFx1YjA5OFx1YzUyOSBcdWM4MDFcdWQ2MDAgXHVjNzg4XHVhY2UwL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