A farmer has a rectangular grid of land with m rows and n columns that can be divided into unit cells. Each cell is either fertile (represented by a 1) or barren (represented by a 0). All cells outside the grid are considered barren.
A pyramidal plot of land can be defined as a set of cells with the following criteria:
The number of cells in the set has to be greater than1 and all cells must be fertile.
The apex of a pyramid is the topmost cell of the pyramid. The height of a pyramid is the number of rows it covers. Let (r, c) be the apex of the pyramid, and its height be h. Then, the plot comprises of cells (i, j) where r <= i <= r + h - 1andc - (i - r) <= j <= c + (i - r).
An inverse pyramidal plot of land can be defined as a set of cells with similar criteria:
The number of cells in the set has to be greater than1 and all cells must be fertile.
The apex of an inverse pyramid is the bottommost cell of the inverse pyramid. The height of an inverse pyramid is the number of rows it covers. Let (r, c) be the apex of the pyramid, and its height be h. Then, the plot comprises of cells (i, j) where r - h + 1 <= i <= randc - (r - i) <= j <= c + (r - i).
Some examples of valid and invalid pyramidal (and inverse pyramidal) plots are shown below. Black cells indicate fertile cells.
Given a 0-indexedm x n binary matrix grid representing the farmland, return thetotal number of pyramidal and inverse pyramidal plots that can be found ingrid.

Input: grid =[[0,1,1,0],[1,1,1,1]] Output:2 Explanation: The 2 possible pyramidal plots are shown in blue and red respectively. There are no inverse pyramidal plots inthis grid. Hence total number of pyramidal and inverse pyramidal plots is2+0=2.

Input: grid =[[1,1,1],[1,1,1]] Output:2 Explanation: The pyramidal plot is shown in blue, and the inverse pyramidal plot is shown in red. Hence the total number of plots is1+1=2.

Input: grid =[[1,1,1,1,0],[1,1,1,1,1],[1,1,1,1,1],[0,1,0,0,1]] Output:13 Explanation: There are 7 pyramidal plots,3 of which are shown in the 2nd and 3rd figures. There are 6 inverse pyramidal plots,2 of which are shown in the last figure. The total number of plots is7+6=13.
A cell can be the apex of a pyramid of height h if the three cells directly below it can be the apex of pyramids of height at least h-1, and the current cell is fertile. We can use DP to compute the maximum height of a pyramid with apex at each cell, both for normal and inverse pyramids.
classSolution {
public:int countPyramids(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(), ans =0;
auto dp = grid;
for (int i =1; i < m; ++i) {
for (int j =1; j < n-1; ++j) {
if (grid[i][j] && grid[i-1][j-1] && grid[i-1][j] && grid[i-1][j+1])
dp[i][j] = min({dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]}) +1;
}
}
for (int i =0; i < m; ++i) for (int j =0; j < n; ++j) ans += dp[i][j] -1;
reverse(grid.begin(), grid.end());
dp = grid;
for (int i =1; i < m; ++i) {
for (int j =1; j < n-1; ++j) {
if (grid[i][j] && grid[i-1][j-1] && grid[i-1][j] && grid[i-1][j+1])
dp[i][j] = min({dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]}) +1;
}
}
for (int i =0; i < m; ++i) for (int j =0; j < n; ++j) ans += dp[i][j] -1;
return ans;
}
};
classSolution {
publicintcountPyramids(int[][] grid) {
int m = grid.length, n = grid[0].length, ans = 0;
int[][] dp =newint[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
dp[i][j]= grid[i][j];
for (int i = 1; i < m; i++)
for (int j = 1; j < n-1; j++)
if (grid[i][j]== 1 && grid[i-1][j-1]== 1 && grid[i-1][j]== 1 && grid[i-1][j+1]== 1)
dp[i][j]= Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i-1][j+1]) + 1;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
ans += dp[i][j]- 1;
// Inverse pyramidsfor (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
dp[i][j]= grid[m-1-i][j];
for (int i = 1; i < m; i++)
for (int j = 1; j < n-1; j++)
if (dp[i][j]== 1 && dp[i-1][j-1]== 1 && dp[i-1][j]== 1 && dp[i-1][j+1]== 1)
dp[i][j]= Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i-1][j+1]) + 1;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
ans += dp[i][j]- 1;
return ans;
}
}
classSolution {
funcountPyramids(grid: Array<IntArray>): Int {
val m = grid.size
val n = grid[0].size
var ans = 0val dp = Array(m) { grid[it].clone() }
for (i in1 until m) {
for (j in1 until n-1) {
if (grid[i][j] ==1&& grid[i-1][j-1] ==1&& grid[i-1][j] ==1&& grid[i-1][j+1] ==1)
dp[i][j] = minOf(dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]) + 1 }
}
for (i in0 until m) for (j in0 until n) ans += dp[i][j] - 1for (i in0 until m) for (j in0 until n) dp[i][j] = grid[m-1-i][j]
for (i in1 until m) {
for (j in1 until n-1) {
if (dp[i][j] ==1&& dp[i-1][j-1] ==1&& dp[i-1][j] ==1&& dp[i-1][j+1] ==1)
dp[i][j] = minOf(dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]) + 1 }
}
for (i in0 until m) for (j in0 until n) ans += dp[i][j] - 1return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defcountPyramids(self, grid: list[list[int]]) -> int:
m, n = len(grid), len(grid[0])
ans =0 dp = [row[:] for row in grid]
for i in range(1, m):
for j in range(1, n-1):
if grid[i][j] and grid[i-1][j-1] and grid[i-1][j] and grid[i-1][j+1]:
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]) +1 ans += sum(dp[i][j] -1for i in range(m) for j in range(n))
grid = grid[::-1]
dp = [row[:] for row in grid]
for i in range(1, m):
for j in range(1, n-1):
if grid[i][j] and grid[i-1][j-1] and grid[i-1][j] and grid[i-1][j+1]:
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]) +1 ans += sum(dp[i][j] -1for i in range(m) for j in range(n))
return ans