Fill dp [ ] [ ] by traversing each column from bottom to up
for i in range ( col ) : NEW_LINE INDENT cnt = 0 NEW_LINE for j in range ( row - 1 , - 1 , - 1 ) : NEW_LINE INDENT dp [ j ] [ i ] = 0 NEW_LINE if ( a [ j ] [ i ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE dp [ j ] [ i ] = cnt NEW_LINE DEDENT else : NEW_LINE INDENT cnt = 0 NEW_LINE DEDENT DEDENT print ( " cnt ▁ " , cnt ) NEW_LINE DEDENT ans = - 1 NEW_LINE for i in range ( row ) : NEW_LINE