Given a 2D grid where:
Count the number of islands. An island is:
Constraints:
Examples:
// Example 1:
const grid1 = [
["0", "1", "1", "1", "0"],
["0", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "0", "0", "0"],
];
console.log(numIslands(grid1));
// Output: 1
// Explanation: One island connected horizontally and vertically
// Example 2:
const grid2 = [
["1", "1", "0", "0", "1"],
["1", "1", "0", "0", "1"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"],
];
console.log(numIslands(grid2));
// Output: 4
// Explanation: Four separate islands