TITLE: Number of closed loops in a square grid
QUESTION [8 upvotes]: 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?

REPLY [2 votes]: The number of cycles with an exact length of $n$, meaning and max height of 2 is this:
$$ S(n,2) = 1 + \sum_{m=1}^n \sum_{k=1}^m \binom{n-m-k+1}{k}\binom{m}{k} \cdot 2^k $$
Here, $m$ represents the number of columns with 1 square in them, and $k$ represents the number of groups of columns which have 1 square in them. It's very reminiscent of multichoose, except there's two options for each group of columns (either the squares are on the bottom, or top).
Thus, for the total number of cycles in a $n \times 2$ board is:
$$ f(n,2) =\sum_{i=1}^n (n-i+1) \cdot S(i,2) $$
$S(n,3)$ is where things get ugly. Basically, you consider every possible configuration of columns with three squares in them, and then calculate the number of possible ways to connect these columns without creating another in between. I'd suggest a recursive formula dependent upon the squares visible at one end. Let $g(n,3)(b,b,b)$ be the number of cycles with exact width $n$, and the second part is binary conditions on where or not the $j$-th highest square in the rightmost column is in the cycle. The recursion would look something like this:
$$ g(n,3)(1,0,0) = g(n-1,3)(1,1,1)+g(n-1,3)(1,1,0) $$
We'd also want to include a clause about $g(n,3)(1,0,1)$, which sorta of depends on what cycles are allowed.