#P6404. [COCI 2014/2015 #2] BOB
[COCI 2014/2015 #2] BOB
Description
Bob is a famous builder. He bought some land and wants to build a house. Unfortunately, the problem is the terrain: the elevation may not be the same at different places on the land.
The land is a rectangle, with width meters and length meters. It can be divided into an grid of cells (see the picture). Bob’s house will be shaped as a rectangle whose sides are parallel to the sides of the land, and whose vertices coincide with grid vertices. All the land covered by Bob’s house must have the same height, so that it will not collapse.

Compute the number of ways Bob can build his house.
Formally, find the number of submatrices in a matrix whose elements are all equal.
Input Format
The first line contains integers and .
Each of the next lines contains integers , the elevation height of each unit square of land.
Since the input is very large, please use a faster input method.
Output Format
Output a single line: the number of ways Bob can build his house.
5 3
2 2 2
2 2 1
1 1 1
2 1 2
1 2 1
27
4 3
1 1 1
1 1 1
2 2 2
2 2 2
36
Hint
Explanation for Sample 1
Some possible house positions are the rectangles with top-left and bottom-right vertices at and (height ), and and (height ). The first number in the parentheses is the row index and the second number is the column index (coordinates start from ).
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
For all valid , .
Note
This problem is translated from COCI2014-2015 CONTEST #2 T4 BOB.
Translated by ChatGPT 5
京公网安备 11011102002149号