If $Y_1,Y_2,Z_1,Z_2$ are finite groups, and if $Y_1\times Z_1\cong Y_2\times Z_2$ and $Y_1\cong Y_2$, then $Z_1\cong Z_2$. The following argument (which I believe is due to László Lovász) uses no group theory, so it applies to much more general kinds of finite algebraic structures.
For finite groups $X,Y$ let $h(X,Y)$ denote the number of homomorphisms and $h'(X,Y)$ the number of injective homomorphisms from $X$ to $Y$. Since $Y_1\times Z_1\cong Y_2\times Z_2$, for any finite group $X$ we have
$$h(X,Y_1)\cdot h(X,Z_1)=h(X,Y_1\times Z_1)=h(X,Y_2\times Z_2)=h(X,Y_2)\cdot h(X,Z_2).$$
Since $Y_1\cong Y_2$ we also have $h(X,Y_1)=h(X,Y_2)$, whence $$h(X,Z_1)=h(X,Z_2)$$ for any finite group $X$.
Next, we show that $h'(X,Z_1)=h'(X,Z_2)$ for any finite group $X$. Let $X\setminus\{e\}=\{x_1,\dots,x_n\}$. Let $H_i(X,Z)$ denote the set of all homomorphisms $f:X\to Z$ such that $f(x_i)=e$. By the in-and-out principle, we have
$$h'(X,Z_1)=h(X,Z_1)-|\bigcup_{i\in I}H_i(X,Z_1)|=h(X,Z_1)+\sum_{\emptyset\ne I\subseteq[n]}(-1)^{|I|}|\bigcap_{i\in I}H_i(X,Z_1)|$$$$=h(X,Z_2)+\sum_{\emptyset\ne I\subseteq[n]}(-1)^{|I|}|\bigcap_{i\in I}H_i(X,Z_2)|=h'(X,Z_2),$$
the middle equality because $|\bigcap_{i\in I}H_i(X,Z)|$ is the number of homomorphisms from a certain quotient of $X$ to $Z$.
So $h'(X,Z_1)=h'(X,Z_2)$ for any finite group $X$. In particular, $h'(Z_1,Z_2)=h'(Z_1,Z_1)\gt0$ and $h'(Z_2,Z_1)=h'(Z_2,Z_2)\gt0$. Since $Z_1$ and $Z_2$ are finite and have an injective homomorphism in each direction, it follows that $Z_1\cong Z_2$.
P.S. See L. Lovász, Operations with structures, Acta Math. Acad. Sci. Hungar. 18 (1967) 321-328.