TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description



We say a positive integer n is an perfect number if the sum of its all proper factors is exactly n. Now we have a matrix of R*C perfect numbers. If after swapping two neighboring numbers, there are at least three same number consecutively in a row/column, we say this move is “good”. How many “good” moves here?

Input Format

First line contains an integer T (T<=100) indicating the number of test cases.
For each test case, first line contains two integer R, C (3<=R, C<=50) indicating the number of rows and columns respectively. Next R lines contains a sequence of C perfect numbers each. It is guaranteed that no three consecutive same numbers in a row/column initially.

Output Format

For each test case, please output the number of “good” moves.

Sample Input 1

1
3 3
6 6 28
28 6 28
28 28 6

Sample Output 1

3

Hints

Problem Source

Migrated from old NTUJ.

tmt514

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 5000 65536 200