I know if we need to find maximum square, it can be solved with DP. Can this Problem be solved in DP?
Please check out this: http://www.geeksforgeeks.org/maximumsizesubmatrixwithall1sinabinarymatrix/
Can this Problem be solved in DP?


My DP solution for your reference. It definitely can be simplified, but it should be clear and easy to follow.
The main idea is:
Keep a list of lists, in which each element is the index of the left most element of contiguous '1's in the same row if this index greater than the left most index of the '1' in the contiguous '1's of the current position and current scanning line, otherwise this element is the index of the first '1' in in the contiguous '1's of the contiguous '1's of the current position and current scanning line.One example is :
1 1 0 0 1 1 1 0
0 0 1 1 1 1 1 1
1 0 0 1 1 1 1 0
When you scan the first line, the list looks like:
storage = [[0], [0], [], [], [4], [4], [4], []]
When you scan the second line, the list looks like:
storage = [[], [], [2], [2], [4,2], [4,2], [4,2], [2]]
When you scan the third line, the list looks like:
storage = [[0], [], [], [3,3], [4,3,3], [4,3,3], [4,3,3], []]
In the list of 'storage', no element is smaller than the last added element. And if a position in the scanning row is '0', then empty the list of that position in the 'storage'
Use this list to calculate the maximum rectangular area:
For each position in a row, you need to iterate through the list and calculate the maximum area by:max_area = max(max_area, (len(storage[X])  idx)*(X + 1  storage[X][idx]))
Where the 'idx' is the iterator of the list of the current position starts from 0.
The time complexity of my method is O(n^2), where n is the total number of all the elements in the matrix.
class Solution: # @param matrix, a list of lists of 1 length string # @return an integer def maximalRectangle(self, matrix): if len(matrix) == 0 or len(matrix[0]) == 0: return 0 storage = [] max_area = 0 for idx, each in enumerate(matrix[0]): if idx == 0: if each == '1': storage.append([0]) max_area = 1 else: storage.append([]) else: if each == '1': if storage[idx1] == []: storage.append([idx]) max_area = max(max_area, 1) else: storage.append([storage[idx1][1]]) max_area = max(max_area, (idx + 1  storage[idx][1])) else: storage.append([]) for Y in xrange(1, len(matrix)): for X in xrange(len(matrix[0])): if X == 0: if matrix[Y][X] == '1': max_area = max(max_area, len(storage[0])+1) storage[0].append(0) else: storage[0] = [] else: if matrix[Y][X] == '1': if storage[X1] == []: max_area = max(max_area, len(storage[X])+1) for idx in xrange(len(storage[X])): if storage[X][idx] < X: storage[X][idx] = X storage[X].append(X) else: for idx in xrange(len(storage[X])): if storage[X][idx] < storage[X1][1]: storage[X][idx] = storage[X1][1] max_area = max(max_area, (len(storage[X]) + 1  idx)*(X + 1  storage[X1][1])) else: max_area = max(max_area, (len(storage[X]) + 1  idx)*(X + 1  storage[X][idx])) max_area = max(max_area, X + 1  storage[X1][1]) storage[X].append(storage[X1][1]) else: storage[X] = [] return max_area

I initially approached this problem with a DP solution, similar to the one described at the link you provided.
Let
w(i,j)
be the width of max rectangle for the cell(i,j)
,h(i,j)
be the height.
Letleft(i,j)
be the number of contigious series of 1 to the left of(i, j)
cell, including the cell itself
Lettop(i, j)
be the number of contigious series of 1 on top of(i,j)
cell, including the cell itself.I was thinking that the problem exhibits optimal substructure in the following way: the maximal rectangle for
(i, j)
will always be one of the following:
an extension of a maximal rectangle for
(i1, j)
so that:h(i,j) = h(i1, j) +1
,w(i, j) = min(left(i, j), w(i1, j))

an extension of a maximal rectangle for
(i, j1)
so thath(i, j) = min(h(i, j1), top(i, j))
,w(i, j) = w(i, j1) + 1

an extension of a maximal rectangle for
(i1, j1)
, with a similar idea  extend the rectangle as much as we can in both directions. 
a horizontal line,
h(i, j) = 1, w(i, j) = left(i, j)

a vertical line,
w(i, j) = 1, h(i, j) = top(i, j)
My O(m*n) solution, where m, n are the dimentsions of the matrix, passed the OJ, BUT when I tried to prove its correctness, I found a counterexample.
It is a histogram with the following bar heights (see the histogram problem):
[3, 3, 3, 3, 3, 3, 3, 3, 8, 8, 8, 30, 19]
. If you draw this, you'll see that the maximum rectangle for the down right corner does not fall into any of the categories described in 15.
