Solution (source code)

= Solution

For the <simple symmetric random walk>, $S_n$ is a martingale and the <square-minus-time martingale of a simple symmetric random walk> is
$$
M_n=S_n^2-n
$$
Indeed, conditioning on $\mathcal F_n$ and using $\mathbb E[X_{n+1}]=0$ and $X_{n+1}^2=1$ gives $\mathbb E[S_{n+1}^2\mid\mathcal F_n]=S_n^2+1$.

Apply the <optional sampling theorem for a supermartingale> to the bounded <stopping time> $T\wedge n$:
$$
\mathbb E[S_{T\wedge n}^2]=\mathbb E[T\wedge n]\leq\mathbb E[T].
$$
Thus the stopped martingale $(S_{T\wedge n})$ is bounded in $L^2$. The <L2 martingale convergence theorem> gives convergence in $L^2$, and because $T<\infty$ almost surely its limit is $S_T$. Meanwhile the <monotone convergence theorem> gives $\mathbb E[T\wedge n]\to\mathbb E[T]$. Therefore
$$
\mathbb E[S_T^2]=\mathbb E[T].
$$

Finite mean is essential. Let $T=\inf\{n\geq1:S_n=0\}$ be the first return to zero. The one-dimensional <simple symmetric random walk> is recurrent, so $T<\infty$ almost surely, but its first-return time has infinite mean. Since $S_T=0$,
$$
\mathbb E[S_T^2]=0\ne\infty=\mathbb E[T].
$$