------
## 最终结论
```python
def hasCycle(grid):
    m, n = len(grid), len(grid[0])
    visited = [[False] * n for _ in range(m)]  # Initialize the visited array
    
    def dfs(x, y, from_x, from_y):
        if visited[x][y]:
            return True  # Found a cycle
        
        visited[x][y] = True
        directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]  # Down, Up, Right, Left
        for dx, dy in directions:
            nx, ny = x + dx, y + dy
            if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] == grid[x][y]:
                if (nx != from_x or ny != from_y) and dfs(nx, ny, x, y):
                    return True
        
        visited[x][y] = False
        return False

    for i in range(m):
        for j in range(n):
            if not visited[i][j]:  # If the cell is not visited
                if dfs(i, j, -1, -1):  # Start DFS search
                    return True
                
    return False
```