Count Submatrices With Equal Frequency of X and Y
Every submatrix that contains the top-left cell is a prefix rectangle, so two prefix-count tables give its X and Y counts in constant time.
Every submatrix that contains the top-left cell is a prefix rectangle, so two prefix-count tables give its X and Y counts in constant time.