#P11001. [蓝桥杯 2024 省 Python B] 连连看

[蓝桥杯 2024 省 Python B] 连连看

Problem Description

Xiao Lan is playing a new link-up game with friends. In an n×mn \times m rectangular grid, each cell contains an integer. The integer in row ii and column jj is Ai,jA_{i,j}. The player needs to find a pair of cells (a,b)(a,b) and (c,d)(c,d) such that the integers in these two cells Aa,bA_{a,b} and Ac,dA_{c,d} are equal, and their positions satisfy ac=bd>0|a-c| = |b-d| > 0. Please determine how many pairs of cells in this n×mn \times m rectangular grid satisfy the condition.

Input Format

The first line contains two positive integers nn and mm, separated by a single space.

Next are nn lines. The ii-th line contains mm positive integers Ai,1,Ai,2,,Ai,mA_{i,1}, A_{i,2}, \dots, A_{i,m}, separated by single spaces.

Output Format

Output one line containing one integer representing the answer.

3 2 
1 2 
2 3 
3 2

6

Hint

There are the following 6 pairs of cells: (1,2)(2,1)(1,2)−(2,1), (2,2)(3,1)(2,2)−(3,1), (2,1)(3,2)(2,1)−(3,2), (2,1)(1,2)(2,1)−(1,2), (3,1)(2,2)(3,1)−(2,2), (3,2)(2,1)(3,2)−(2,1).

Constraints

For 20%20\% of the test cases, 1n,m501 \leq n, m \leq 50.

For all test cases, 1n,m10001 \leq n, m \leq 1000, 1Ai,j10001 \leq A_{i,j} \leq 1000.

Translated by ChatGPT 5