问题 I: Diagonal Fancy

内存限制:512 MB 时间限制:8 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

Given a matrix A with n rows and m columns, your objective is to compute the total number of continuous sub-square matrices B that are diagonal fancy.

A square matrix B is designated as diagonal fancy if it satisfies the subsequent criteria:

  • For any indices �1i1�1j1�2i2, and �2j2, if �1−�1=�2−�2i1j1=i2j2, then ��1,�1=��2,�2Bi1,j1=Bi2,j2.
  • For any indices �1i1�1j1�2i2, and �2j2, if �1−�1≠�2−�2i1j1=i2j2, then ��1,�1≠��2,�2Bi1,j1=Bi2,j2.

Here, ��,�Bi,j signifies the element located at the i-th row and j-th column of matrix B.

A continuous sub-square matrix from matrix A with n rows and n columns is defined as the matrix derived from A by selecting continuous n rows and continuous n columns.

输入格式

Ensure to use cin/cout and disable synchronization with stdio to avoid unexpected TLE verdict.

The input consists of multiple test cases. The first line of the input contains an integer T (1≤�≤1001T100), which represents the number of test cases.

The first line of each test case contains two integers n and m (1≤�,�≤10001n,m1000), representing the number of rows and columns in the matrix A.

Each of the next n lines contains m space-separated integers ��,1,��,2,…,��,�Ai,1,Ai,2,,Ai,m (1≤��,�≤�×�1Ai,jn×m), representing the elements of the matrix A for that particular test case.

It is guaranteed that ∑�×�≤107n×m107 over all test cases.

输出格式

For each test case, output a single integer in a single line, which represents the count of continuous sub-square matrices in matrix A that are diagonal fancy.

输入样例 复制

3
2 2
1 2
3 1
2 2
1 2
2 1
3 3
1 2 3
4 1 2
5 4 1

输出样例 复制

5
4
14

数据范围与提示

In the first test case, there are 55 diagonal fancy subsquares in total. They are listed in bold below.
1 2    1 2    1 2    1 2    1 2
3 1    3 1    3 1    3 1    3 1

分类标签