The country of Cheon has N villages, numbered 1 to N. Between two villages there may or may not be a road, and every road has length 1. A road can be traveled in one direction only.
Minho wrote the connections between the villages into an N×N adjacency matrix. If the j-th number on line i is 1, there is a road from village i to village j; if it is 0, that road does not exist. A 1 on the diagonal means the village has a road back to itself.
A path of length K is a sequence of village numbers v0,v1,…,vK such that for every 1≤t≤K there is a road from village vt−1 to village vt. A path may pass through the same village several times, and v0 and vK are unrestricted. Two paths count as different if their sequences differ in at least one position.
Given the adjacency matrix, count the different paths of length K.