from collections import deque
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
r, c = map(int, input().split())
g = [list(input()) for _ in range(r)]
ans = 0
q = set()
q.add((0, 0, g[0][0]))
def bfs(q):
global ans
while q:
x, y, visited = q.pop()
ans = max(ans, len(visited))
for d in range(4):
nx, ny = x + dx[d], y + dy[d]
if nx < 0 or nx >= r or ny < 0 or ny >= c:
continue
if g[nx][ny] not in visited:
q.add((nx, ny, visited + g[nx][ny]))
bfs(q)
print(ans)
다른 bfs와 달리 큐를 집합으로 만들고 check배열은 만들지 않음. 모든 경우를 탐색하는데 같은 경로는 탐색하지 않기 위해 check는 안쓰고 큐를 집합으로 만들어서 중복을 피해줌
다시 풀어볼 문제