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