Exercise 1.23 (Stein and Shakarchi): Suppose $f(x,y)$ is a function on $\mathbb{R}^2$ that is separably continuous: for each fixed variable, $f$ is continuous in the other variable. Prove that $f$ is measurable on $\mathbb{R}^2$.
So far my intuition leads me to use the following property given for measurable functions:
The finite-valued function $f$ is measurable if and only if $f^{-1}(\mathcal{O})$ is measurable for every open set $\mathcal{O}$.
I imagine that we assume $f$ is finite valued, as we almost always find that a function takes on infinite values on at most a set of measure zero (within the context of this text). Now, it is clear that $f$ is measurable on every "horizontal" slice if we were to fix $y \in \mathbb{R}$ due to the continuity of $f$ in $x$. Similarly, on every "vertical" slice $f$ is measurable.
Now, here is where I find myself stuck, and am asking if my intuition is leading me in the correct direction. If we let $y \in \mathbb{Q}$ be arbitrary, then taking the countable union of these horizontal slices (or/and vertical slices if you desire), $f$ remains continuous on this set. I'm assuming that since these slices are dense in $\mathbb{R}^2$, due to the density of $\mathbb{Q}$, it follows that $f$ remains continuous in the closure of this dense set, being $\mathbb{R}^2$. I feel iffy making this logical step, and I feel like I'm missing something.
Is this the right direction for proving this? I am approaching this by focusing on the dense subset, as it should be easier than the entirity of $\mathbb{R}^2$. Moreover, I'm assuming that this idea generalizes to $\mathbb{R}^d$ through a simple induction proof correct?