Counting paths uses the same structure as probability. Sum over transitions instead of weighted sum.
What counts as "success" changes the base case. Here, leaving the grid returns .
Modular arithmetic is needed when counting. Probabilities stay between and , but counts explode. Notice how similar the code is to Knight Probability? State and transition structure are identical. Only the base cases and arithmetic differ.