For a row-stochastic transition matrix \(P\), let \(\pi\) be its unique
stationary distribution and define
$$Z = (I - P + \mathbf{1}\pi^T)^{-1}.$$
With hitting times defined by
\(T_j = \inf\{n \ge 0: X_n=j\}\), so that \(m_{jj}=0\), the function
returns
$$K = \sum_j \pi_j m_{ij} = \mathrm{tr}(Z)-1.$$
The value does not depend on the starting state \(i\).
Irreducibility is sufficient; aperiodicity is not required. Reducible chains
can have multiple stationary distributions and are rejected.
Some references instead put the mean first-return time
\(m_{jj}=1/\pi_j\) on the diagonal. Under that convention the corresponding
stationary weighted sum is \(K+1\), not \(K\). This function uses the
zero-diagonal hitting-time convention, consistently with
meanFirstPassageTime().
The implementation uses a dense LAPACK solve for the fundamental matrix
\(Z\). Its time complexity is \(O(n^3)\) and its memory use is
\(O(n^2)\), as expected for a dense exact computation. It supports both
row- and column-stochastic storage.