= 0 && $x < $rows && $y >= 0 && $y < $cols && $grid[$x][$y] === 1) { $uf->union($i * $cols + $j, $x * $cols + $y); } } } } } return $uf->count; } }