Logical Chain

방향 그래프의 간선이 m일에 걸쳐 뒤집힐 때, 매일 변경이 끝난 뒤 서로 도달 가능한 두 정점 쌍의 개수를 구한다.

어려움8그래프동적 계획법비트 연산아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Every time you come across a problem you've never seen before, haven't you thought of something that is familiar to you? If so, you might think of something else, then more and more things will come to your mind. This is what is called a logical chain.  Lu Xun's work also described this interesting phenomenon.

Assume there are nn concepts, labeled by 11, 22, \ldots, nn. Little Q's mind can be expressed by a n×nn \times n binary matrix gg. If he can think of concept jj when he comes across concept ii, then g_i,jg\_{i, j} is 11, otherwise it is 00. For two different concepts uu and vv, if uu can lead to vv  directly or indirectly and vv can also lead to uu directly or indirectly, then the pair (u,v)(u, v) is called a looping pair.

Little Q's mind changes all the time. On ii-th day, a total of k_ik\_i positions (u,v)(u, v) in matrix gg are flipped (00s become 11s and 11s become 00s). Your task is to write a program to find the number of looping pairs each day after all changes on that day.

When counting, pairs (u,v)(u, v) and (v,u)(v, u) should be considered the same.

입력

The first line of the input contains two integers nn and mm, the number of concepts and the number of days (1n2501 \leq n \leq 250, 1m25,0001 \leq m \leq 25\\,000).

In the next nn lines, line ii contains nn integers g_i,1g\_{i, 1}, g_i,2g\_{i, 2}, \ldots, g_i,ng\_{i, n} (0g_i,j10 \leq g\_{i, j} \leq 1, g_i,i=0g\_{i, i} = 0). Together, these lines define the matrix gg.

Each of the next mm parts describes a single day and starts with a line with a single integer k_ik\_i, the number of changes which happened on day ii (1k_i101 \leq k\_i \leq 10). Each of the next k_ik\_i lines contains two integers uu and vv denoting a changed position in gg (1u,vn1 \leq u, v \leq n, uvu \neq v).

It is guaranteed that each position will be changed at most once per day.

출력

For each day, print a single line containing a single integer: the number of looping pairs after all changes of that day.