# Why does this DFS island counter miss cells on some grids?

**URL:** <https://forum.kirupa.com/t/why-does-this-dfs-island-counter-miss-cells-on-some-grids/680147>\
**Category:** web dev\
**Created:** [April 7, 2026, 10:00am UTC](https://forum.kirupa.com/t/why-does-this-dfs-island-counter-miss-cells-on-some-grids/680147 "2026-04-07T10:00:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 7, 2026, 10:00am UTC](https://forum.kirupa.com/t/why-does-this-dfs-island-counter-miss-cells-on-some-grids/680147/1 "2026-04-07T10:00:12Z")

</div>

Hey everyone, I’m working on a quick grid traversal helper in JavaScript for a practice problem, and I’m trying to keep it in-place so I don’t burn extra memory on a visited set, but on a few test cases it undercounts islands.

```js
function countIslands(grid) {
  let count = 0;

  function dfs(r, c) {
    if (r < 0 || c < 0 || r >= grid.length || c >= grid[0].length) return;
    if (grid[r][c] === '0') return;

    grid[r][c] = '0';

    dfs(r + 1, c);
    dfs(r - 1, c);
    dfs(r, c + 1);
    dfs(r, c - 1);
  }

  for (let r = 0; r < grid.length; r++) {
    for (let c = 0; c < grid[0].length; c++) {
      if (grid[r][c] === '1') {
        dfs(r, c);
      }
      count++;
    }
  }

  return count;
}

```

What am I overlooking here that makes this return the wrong island count even though the DFS itself seems fine?

Ellen

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [April 7, 2026, 10:07am UTC](https://forum.kirupa.com/t/why-does-this-dfs-island-counter-miss-cells-on-some-grids/680147/2 "2026-04-07T10:07:33Z")

</div>

@Ellen1979 yup, the DFS part is basically fine. The problem is just that `count++` is outside the `if`, so it runs for every cell in the grid instead of only when you find a new island.

Move it inside the `if`, right after you start DFS from a `'1'`:

```auto

js
function countIslands(grid) {
  let count = 0;

  function dfs(r, c) {
    if (r < 0 || c < 0 || r >= grid.length || c >= grid[0].length) return;
    if (grid[r][c] === '0') return;

    grid[r][c] = '0';
    dfs(r + 1, c);
    dfs(r - 1, c);
    dfs(r, c + 1);
    dfs(r, c - 1);
  }

  for (let r = 0; r < grid.length; r++) {
    for (let c = 0; c < grid[0].length; c++) {
      if (grid[r][c] === '1') {
        dfs(r, c);
        count++;
      }
    }
  }

  return count;
}

```

One other thing: this also changes the input grid, so don’t reuse the same grid in later tests or you’ll get weird results.

Hari

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 7, 2026, 10:35am UTC](https://forum.kirupa.com/t/why-does-this-dfs-island-counter-miss-cells-on-some-grids/680147/3 "2026-04-07T10:35:11Z")

</div>

@HariSeldon the note about mutating `grid` is the other gotcha, and jagged input can also bite here because `grid[0].length` assumes every row matches.

MechaPrime
