V = adj_V^2;
<span class="keyword">while</span> (sum(temp_H(:))&gt;0)
    paths_H = paths_H + temp_H;
    temp_H = temp_H*adj_H;
<span class="keyword">end</span>
<span class="keyword">while</span> (sum(temp_V(:))&gt;0)
    paths_V = paths_V + temp_V;
    temp_V = temp_V*adj_V;
<span class="keyword">end</span>

hh = paths_H + paths_H';
vv = paths_V + paths_V';
p = hh+vv+eye(n);
all_paths = p&gt;0;
<span class="keyword">if</span> sum(all_paths(:)) ~= n^2
    error(<span class="string">'There must be a directe