Put this in B3:

=IF(OR(D3="",M3="X"),"",MAX(B$2:B2)+1)

then copy down.

Hope this helps.

Pete