If $f: U \to \mathbb{R}^{n}$ with $U \subset \mathbb{R}^{m}$ open connected and $\mathrm{D}f \equiv 0$, then $f$ is constant. Fix $p$ and every $x$ can be connected to $p$ by a polygonal path and so, apply the means value theorm in each segment.
But, if $U$ is disconnected,
how can I find a function $f$, nonconstant, with $\mathrm{D}f \equiv 0$?
Consider $f:\Bbb R\setminus \{0\}\to\Bbb R$ defined by
$f(x)=\begin{cases} -1,&x\in (-\infty,0)\\ 1,&x\in (0,\infty) \end{cases} $.
Can you now generalise this construction for the disconnected set $U $?