You don't want or need my linked answer for this. An easy way to do this is to first calculate the "integral image" I[][] of the input matrix M[][] -- that is, a matrix I[][] whose (x, y) entry is the sum of all entries below and to the left of (x, y) in the input matrix. This lets you easily compute the number of 1s in any given rectangle (x1, y1, x2, y2) in constant time using I[x2][y2] - I[x1-1][y2] - I[x2][y1-1] + I[x1-1][y1-1], if (x1, y1) is the bottom-left corner and (x2, y2) is the top-right corner. I[][] can be computed in a one-time O(n^2) preprocessing step.
For a given bottom-left corner (x1, y1), you can then simply loop through all widths w and heights h in increasing order using a pair of nested loops, testing whether the number of 1s in the rectangle (x1, y1, x1+w, y2+h) is the number of 1s that should be there in an all-1 rectangle -- i.e., (b+1)*(h+1). As soon as this fails to hold, exit that loop early (since clearly if some rectangle is not an all-1 rectangle, then no rectangle that contains it can be either).
This algorithm is of course O(n^2)-time for an n*n rectangle, but there is no avoiding this worst case (consider an input matrix in which every entry is 1).