Fill dp [ ] [ ] by traversing each column from bottom to up
for ( int i = 0 ; i < col ; i ++ ) { int cnt = 0 ; for ( int j = row - 1 ; j >= 0 ; j -- ) { dp [ j ] [ i ] = 0 ; if ( a [ j ] [ i ] == 1 ) { cnt ++ ; dp [ j ] [ i ] = cnt ; } else { cnt = 0 ; } } } int ans = -1 ; for ( int i = 0 ; i < row ; i ++ ) {