def METHOD_NAME(corner, pos, prev):
    # Exit conditions
    if any([pos[i]>=shape[i] for i in range(len(shape))]): return
    edges =   list(rank_arr[corner[0]:pos[0]+1, pos[1], pos[2]]) \
            + list(rank_arr[pos[0], corner[1]:pos[1]+1, pos[2]]) \
            + list(rank_arr[pos[0], pos[1], corner[2]:pos[2]+1]) \
            if self.is_3D else                                   \
              list(rank_arr[corner[0]:pos[0]+1, pos[1]])         \
            + list(rank_arr[pos[0], corner[1]:pos[1]+1])
    if visited[pos] or not set(edges).issubset(set([prev, -1])): return
    visited[pos] = True
    if rank_arr[pos] not in [-1, prev]: prev, corner = rank_arr[pos], pos
    else: rank_arr[pos] = prev
    args = [[0, 1] for _ in range(len(shape))]
    neighbors = [tuple(np.array(pos) + np.array(p)) for p in product(*args)
                 if not p == (0,)*len(shape)]
    for n in neighbors: METHOD_NAME(corner, n, prev)