Jarmo has a strong possible way of solving the issue. I have taken a longer route, which demonstrates one way of working backwards. The approach does all of the work just as the formula starts processing by using an As Constant() function, which runs the contained code once. In this section, it loops from the last row to the first row and creates a result matrix called "Values". Then in the actual formula, the only thing to do is to apply the value from the Values matrix to each of the rows.
As Constant(
values = J( 1, N Rows( Current Data Table() ), . );
For( i = Length( values ) - 1, i >= 1, i--,
if(:col 29[i] == 0 & :Col 29[i + 1] == 1,
values[i]=1,
:col 29[i] == 0 & :Col 29[i + 1] == 0,
values[i] = values[i-1]-1,
values[i]=.
));
Show( values );
);
values[Row()];
Jim