#t141. 二阶矩阵

二阶矩阵

Good 2×2 Submatrices

Problem Description

Little A has an n×mn \times m matrix AA.

Little A calls a 2×22 \times 2 matrix DD good if and only if D1,1×D2,2=D1,2×D2,1D_{1,1} \times D_{2,2} = D_{1,2} \times D_{2,1}, where Di,jD_{i,j} denotes the element in the ii-th row and jj-th column of DD.

Little A wants to know how many good submatrices AA contains.

Input Format

The first line contains two positive integers nn and mm. The next nn lines each contain mm integers Ai,1,Ai,2,…,Ai,mA_{i,1}, A_{i,2}, \ldots, A_{i,m}.

Output Format

One line: one integer, the number of good submatrices in AA.

Sample Input

3 4
1 2 1 0
2 4 2 1
0 3 3 0

Sample Output

2

Sample Explanation

In the given 3×43 \times 4 matrix, there are (3−1)×(4−1)=6(3-1) \times (4-1) = 6 possible 2×22 \times 2 submatrices. We check each against D1,1×D2,2=D1,2×D2,1D_{1,1} \times D_{2,2} = D_{1,2} \times D_{2,1}.

  • The submatrix with top-left corner at row 11, column 11 is [1224]\begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}; we get 1×4=41 \times 4 = 4 and 2×2=42 \times 2 = 4, equal, so it is good.
  • The submatrix with top-left corner at row 11, column 22 is [2142]\begin{bmatrix} 2 & 1 \\ 4 & 2 \end{bmatrix}; we get 2×2=42 \times 2 = 4 and 1×4=41 \times 4 = 4, equal, so it is also good.

The other four submatrices fail the equality condition, so there are 22 good submatrices in total.

Constraints

For all test points, 1≤n≤5001 \le n \le 500, 1≤m≤5001 \le m \le 500, −100≤Ai,j≤100-100 \le A_{i,j} \le 100.