If $X$ and $Y$ be graphs, then their product $X\times Y$ has vertex set $V(X)\times V(Y)$, and $(x,y)\sim (x', y')$ iff $x\sim x'$ and $y\sim y'$.
Now, Show that if $X\times X\simeq Y\times Y$, then $X\simeq Y$.
Two graphs $X$ and $Y$ are isomorphic if there is a bijection, $\varphi$ say, from $V(X)$ to $V(Y)$ such that $x\sim y$ in $X$ iff $\varphi(x)\sim \varphi(y)$ in $Y$.
Thanks
For the finite case this is a result of Lovasz. Two facts, first if F is a fixed graph then the number of homomorphisms from $F$ to $X\times X$ is the square of the number of homomorphisms from $F$ to $X$. Second if the number of homomorphisms from $F$ to $X$ equals the number from $F$ to $Y$, then $X$ and $Y$ are isomorphic.