<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

518. Kidnapping
Time limit per test: 0.5 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

Berland's Police has a serious problem. A foreign ambassador arrived to Berland with an important mission, and his daughter was kidnapped just from the Royal Palace! Inspired by adventures of Erast Fandorin, the Police Chief developed the following ingenious plan.

The ambassador agrees to pay ransom, but only if the kidnappers allow his servant to visit the girl and ensure that she is alive. The kidnappers take the blindfolded servant into a coach and transport him to the secret place, where they keep the ambassador's daughter. Certainly, the role of the servant is certainly played by a secret agent of the Police. The Police Chief knows that when the coach is moving, the wheels are creaking once on each full rotation. So, by counting the number of creaks and multiplying it by the length of the rim, one can easily calculate the distance covered by the coach.

In spite of this brilliant idea, the affair turned to be much more difficult than it could be in a detective story. There are n intersections in the city numbered from 1 to n, some pairs of intersections are connected by bidirectional roads. The kidnappers agreed to take the "servant" to the secret place, and the servant is quite sure that this place is located at one of the intersections. Also the agent has calculated the lengths of roads between each pair of consecutive intersections on the route passed by the coach. But during the trip the agent was concentrated on counting creaks, so he could not remember in which directions the coach turned at the intersections.

Now the route probably couldn't be restored uniquely! Moreover, the agent has a suspicion that the kidnappers could intentionally pass the same intersection or even the same road more than once to confuse the Police.

Your task is to determine all possible locations of the secret place, given that the trip starts at the intersection number 1.

Input
The first line of the input contains a single integer n (2 ≤ n ≤ 200). Each of the next n lines contains n integers each. The i-th number in the j-th line lij is the length of the road between the i-th and the j-th intersections. If lij = 0 then the road doesn't exist.

It is guaranteed that 0 ≤ lij ≤ 200, lii = 0 and lij = lji. The next line contains one integer k (1 ≤ k ≤ 200) — the number of roads passed by the couch. The following line contains k integers r1, r2,..., rk (1 ≤ ri ≤ 200) — the lengths of roads between each pair of consecutive intersections on the route passed by the coach from the starting point to the secret place.

Output
To the first line of the output write m — the number of all possible locations of the secret place. The second line should contain the numbers of intersections in increasing order separated by spaces.

If there are no possible locations of the secret place, the output must contain the only integer 0.

Example(s)
sample input
sample output
4
0 1 2 0
1 0 1 0
2 1 0 2
0 0 2 0
3
1 1 2
3
1 3 4

<|response|>
1. Abridged problem statement  
We have an undirected graph of n intersections (nodes 1..n) where each pair (i,j) may have a road of positive integer length lij (lij = 0 means no road). A coach starts at node 1 and traverses exactly k roads, producing a recorded sequence of lengths r₁…rₖ in order. The coach may revisit nodes or roads. We need to find all end‐nodes where the coach could finish so that the sequence of edge lengths along the path exactly equals r₁…rₖ. Output the count of such nodes and their labels in increasing order; if none, output 0.

2. Key observations  
- We only care about matching the given sequence of edge lengths, not the actual directions or nodes visited in between, beyond matching lengths.  
- Revisiting nodes or edges is allowed, so we must consider all walks, not just simple paths.  
- A naive enumeration of all walks of length k is exponential. Instead we use a DP/BFS over a state space of size O(n × (k+1)).  
- Define state (u, p) meaning “the coach is at node u after matching p lengths of the sequence.” From (u, p) with p<k, we can go to (v, p+1) for any neighbor v where the edge length between u and v equals rₚ₊₁.  
- Mark each state visited to avoid reprocessing. When we reach any state (u, k), u is a valid end‐node.

3. Full solution approach  
- Read n and the n×n adjacency matrix graph[i][j].  
- Read k and the sequence r[0…k−1].  
- Create a 2D boolean array visited[n+1][k+1], all false initially.  
- Use a queue of pairs (node, matched), initialize with (1, 0) and mark visited[1][0] = true.  
- While the queue is not empty:  
  • Dequeue (u, p).  
  • If p == k, record u in a result set and continue to next state.  
  • Otherwise, look at every v from 1 to n. If graph[u][v] == r[p] and visited[v][p+1] is false, mark it true and enqueue (v, p+1).  
- After BFS finishes, collect all recorded end‐nodes, sort them, and print the count + list (or 0 if empty).  
- Time complexity: O(n² × k), which for n,k ≤ 200 is about 8 · 10⁶ edge checks, acceptable in 0.5 s.

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    // Read adjacency matrix: graph[i][j] = length or 0 if no road
    vector<vector<int>> graph(n+1, vector<int>(n+1));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> graph[i][j];
        }
    }

    int k;
    cin >> k;
    // Read the recorded sequence of road lengths
    vector<int> r(k);
    for (int i = 0; i < k; i++) {
        cin >> r[i];
    }

    // visited[u][p] = true if we've reached node u after matching p lengths
    vector<vector<bool>> visited(n+1, vector<bool>(k+1, false));
    queue<pair<int,int>> q;

    // Start from node 1, having matched 0 steps
    visited[1][0] = true;
    q.emplace(1, 0);

    // Use a set to accumulate all possible end‐nodes
    set<int> possible;

    while (!q.empty()) {
        auto [u, p] = q.front();
        q.pop();

        // If we've matched all k edges, record this node
        if (p == k) {
            possible.insert(u);
            continue;
        }

        // Otherwise, attempt to match the next required length r[p]
        int need = r[p];
        for (int v = 1; v <= n; v++) {
            // If there is a road of the required length and we haven't visited (v,p+1) yet
            if (graph[u][v] == need && !visited[v][p+1]) {
                visited[v][p+1] = true;
                q.emplace(v, p+1);
            }
        }
    }

    // Output the result
    if (possible.empty()) {
        cout << 0 << "\n";
    } else {
        cout << possible.size() << "\n";
        // Print in increasing order, converting to 1-based (already 1-based here)
        bool first = true;
        for (int u : possible) {
            if (!first) cout << ' ';
            first = false;
            cout << u;
        }
        cout << "\n";
    }

    return 0;
}
```

5. Python implementation with detailed comments  
```python
from collections import deque

def solve():
    # Number of intersections
    n = int(input())
    # Read adjacency matrix: 1-based in logic, but store in 0-based lists
    graph = [list(map(int, input().split())) for _ in range(n)]

    # Number of roads traversed
    k = int(input())
    # Sequence of observed lengths
    r = list(map(int, input().split()))

    # visited[node][p]: reached `node` after matching p edges
    visited = [[False] * (k + 1) for _ in range(n)]
    queue = deque()
    # start at node 0 (which represents intersection 1), matched 0 edges
    visited[0][0] = True
    queue.append((0, 0))

    possible = set()

    while queue:
        u, p = queue.popleft()
        # If we've matched all edges, record this node
        if p == k:
            possible.add(u)
            continue

        need = r[p]
        # Try all neighbors v
        for v in range(n):
            # graph[u][v] == need means an edge of required length exists
            if graph[u][v] == need and not visited[v][p+1]:
                visited[v][p+1] = True
                queue.append((v, p+1))

    # Prepare and print output
    if not possible:
        print(0)
    else:
        # convert to 1-based and sort
        result = sorted(u+1 for u in possible)
        print(len(result))
        print(*result)

if __name__ == "__main__":
    solve()
```