QUESTION: Given an $m\times n$ grid of squares, is a formula known for the number of closed loops that can be drawn along the perimeters of these squares? For a better description of what I mean, see the link below.
MOTIVATION: I have been considering the puzzle game "Slitherlink" in which the solution to the puzzle consists of a closed loop drawn in a square lattice grid satisfying certain constraints. So, given an $m\times n$ grid of squares, I would like to calculate the number of closed loops that one can draw in that grid.
Does anyone know of a formula that computes this, or a quick algorithm that one can use to determine this number in terms of $m,n$? Are there any special cases that are easier to determine than others? Where can I read more about this problem?
An equivalent problem: given a graph $G$, how can one calculate the number of cyclic subgraphs it contains?
EDIT: I would like to note that OEIS mentions all largest known values are found by means of a transfer matrix method. Hence, I like to believe the algorithm I present here is asymptotically very close to the best known algorithm.
Here is an $\mathcal{O}(f(n)^3\log m)$ time algorithm where $f(n)=\mathcal{O}(3^n/n)$. The idea is to define an $f(n)\times f(n)$ transition matrix $M$ and calculate $M^m$. This takes $\mathcal{O}(f(n)^3\log m)$ time using the standard matrix multiplication algorithm, but can be sped up to $\mathcal{O}(f(n)^{2.807}\log m)$ using Strassen's algorithm. There are asymptotically faster matrix multiplication algorithms to speed this up to $\mathcal{O}(f(n)^{2.373}\log m)$, but don't bother with that.
Note that this means we can calculate the answer for a $5\times 10^{18}$ grid in mere seconds, while the answer for a $10\times10$ grid would take forever. Pretty funny, if you ask me :) This does assume that multiplication is a constant time operation, which is far from true with such big numbers, unless you only want to know the last few digits of the answer.
So the idea is to use a transition matrix. The state we will consider is the set of horizontal segments that the closed path uses in a certain strip. Consider the following image.
The state of this closed path is $110110$ in the second column and $111010$ in the fourth. What you need to do is define a matrix $M$ where every row and every column represents a state. The entry at row $x$ and column $y$ should be $1$ if you can go from state $y$ to state $x$ and $0$ otherwise.
The point of this technique is described by the following fact. The entry at row $x$ and column $y$ of $M^i$ is the amount of ways to go from state $y$ to state $x$ in $i$ columns. This concludes the theory. The rest of the work is in the implementation. I will note some very important pitfalls for this technique.
Pitfall 1: You might have notices the purple stars in the image. The point is that the set of horizontal segments that the closed path uses in a certain strip is not enough information. Some horizontal segments are allowed to be combined, while others are not. This is to prevent to get two disjoint closed paths count as one. In the image I drew stars between segments that are not allowed to be combined. You need to come up with a way to encode this in your state.
Pitfall 2: Actually calculating $M$ is not easy. For example, note that you can not go from state $1100$ to state $0011$ because vertical line segments will need to cross.
Pitfall 3: To prevent getting two disjoint closed paths, you will also need two extra states. A start state and an end state. They both have no horizontal line segments, but they need to be distinguished between to prevent getting two disjoint closed paths. To read the final answer, just look at the entry of $M^m$ at the start state row and end state column.