Calculation number of islands of a particular lake, or number of enclosed inner polygon

How to calculate number of islands of a particular lake according to current OSM data, like Lac Simard

Some large lakes may composed of thousands islands, so it is impossible to count one by one

You can’t count them directly alone. A ring may be split into multiple inner sections. I don’t know whether someone has done it for =multipolygon , but fortunately there is an example for =roundabout . Counting roundabouts

way.all(if: is_closed() == 0) -> .open;
(.all; - .open;) -> .closed;
make counter num = closed.count(ways) -> .count;
foreach.open {
  if (! lrs_in(u(id()), visited_ids.set(t['ids'])))
  {
    complete {
      node(w);
      way.open(bn);
    }
    make visited
      ids = lrs_union(set(id()), visited_ids.set(t['ids']))
      -> .visited_ids;
    .count convert counter num = t['num'] + 1 -> .count;
  }
}

I used a similar query to find such examples to verify it works overpass turbo
Relation: ‪Lac des Deux-Montagnes‬ (‪331499‬) | OpenStreetMap has 88 closed + 2 rings = 90 islands (selected role:inner closed, and manually observed from JOSM) overpass turbo
Note this can also be used to count islands for water=river