Speed optimization for girth-counting algorithm
What you should be using is a neighboring matrix.

The key is graph theory.

You represent the graph with a matrix.

For example: 

\$
\begin{bmatrix}
0 &amp; 1 &amp; 1 &amp; 0 &amp; 2 \\
1 &amp; 0 &amp; 2 &amp; 0 &amp; 0 \\
1 &amp; 2 &amp; 0 &amp; 1 &amp; 1 \\
0 &amp; 0 &amp; 1 &amp; 0 &amp; 1 \\
2 &amp; 0 &amp; 1 &amp; 1 &amp; 0 \\
\end{bmatrix}
\$

Here is a graph and its corresponding neighbouring matrix.
If you look at the first row you see that A has no one-step connection to itself. But it has a one-step connection to every other node.
Node B has a one-step connection to A and 2 different one-step connections to node C.
As you can see it all sums up if you keep going like this.

Now then, here's the mathemagic. If you multiply the matrix with itself the result will be a matrix which tells you all the connections of two-stop length.
Continue multiplying with the original matrix until you've done it 8 times and you will have a matrix which tells you how many connections of eight-step length you have.

How is matrix multiplication done, you might ask? 
Wikipedia
Another site (with examples) 
Basically, you multiply rows with columns.(It would take another whole answer to explain it)

When you've gotten the neighbouring matrix of the eight power, you just sum up all the elements of the diagonal. That will tell you the number of cycles of length 8. (because the diagonal holds the information of a n-step way back to itself, where n is the power of which the matrix has been multiplied)

This method works for all graphs. All you need to do is make sure the graph is bipartite to begin with.