Fill the dp matrix vertically . For contiguous ' X ' increment the value of y , otherwise make it 0
for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( arr [ j ] [ i ] == ' X ' ) : NEW_LINE INDENT y += 1 NEW_LINE DEDENT else : NEW_LINE INDENT y = 0 NEW_LINE DEDENT dp [ j ] [ i ] [ 1 ] = y NEW_LINE DEDENT DEDENT