  A general-purpose, self-adapting Monte Carlo (MC) algorithm implemented in
the program {\tt Foam} is described. The high efficiency of the MC, that is
small maximum weight or variance of the MC weight is achieved by means of
dividing the integration domain into small cells. The cells can be
$n$-dimensional simplices, hyperrectangles or a Cartesian product of them. The
grid of cells, ``foam'', is produced in the process of the binary split of the
cells. The next cell to be divided and the position/direction of the division
hyperplane is chosen by the algorithm which optimizes the ratio of the maximum
weight to the average weight or (optionally) the total variance. The algorithm
is able to deal, in principle, with an arbitrary pattern of the singularities
in the distribution.
