def topological_visit(g, v, color, order):
    """Run iterative DFS from node V"""
    total = 0
    stack = [v]
    while stack:
        v = stack[-1]
        if color[v]:
            v = stack.pop()
            if color[v] == 1:
                order.append(v)
                color[v] = 2
        else:
            color[v] = 1
            total += 1
            for w in range(len(g[0])):
                if w != v and g[v][w] and not color[w]:
                    stack.append(w)
    return total
