Count Submatrices With All Ones LeetCode solution in Python
1class Solution:2def numSubmat(self, mat: List[List[int]]) -> int:3from dataclasses import dataclass45@dataclass6class Cell:7left: int = 08top: int = 0910n = len(mat)11m = len(mat[0])12dp = [[Cell() for _ in range(m + 1)] for _ in range(n + 1)]1314ans = 015for i in range(1, n + 1):16for j in range(1, m + 1):17if mat[i - 1][j - 1]:18dp[i][j].top = 1 + dp[i - 1][j].top19dp[i][j].left = 1 + dp[i][j - 1].left2021min_height = dp[i][j].top22for k in range(dp[i][j].left):23min_height = min(min_height, dp[i][j - k].top)24ans += min_height25return ans