#P6404. Counting Uniform Submatrices

    ID: 19619 Type: Default 1000ms 256MiB

Counting Uniform Submatrices

Counting Uniform Submatrices

Bob is a famous builder who bought a piece of land with uneven terrain. The land is a rectangle with width n meters and length m meters, divided into an n × m grid of square cells. Bob wishes to build a rectangular house whose sides are parallel to the edges of the land and whose vertices coincide with the grid points. For safety, the house must cover a contiguous block of land cells, all of which have the same altitude.

Formally, given an n × m matrix, count the number of submatrices in which all elements are equal. Note that a submatrix is defined by choosing two rows and two columns as boundaries (with at least one cell) so that the submatrix's sides are parallel to the original matrix.

inputFormat

The first line contains two integers n and m ($1 \le n, m \le 50$), representing the number of rows and columns of the matrix.

Each of the next n lines contains m integers. The j-th integer in the i-th line denotes the altitude of the cell at row i and column j.

outputFormat

Output a single integer representing the total number of submatrices in which all elements are equal.

sample

1 1
5
1