/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function(grid) {
let count = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if (grid[i][j] !== '1') continue;
traverse(grid, i, j);
count++;
}
}
return count;
};
function traverse(grid, row, col) {
if (row < 0) return;
if (row >= grid.length) return;
if (col < 0) return;
if (col >= grid[0].length) return;
if (grid[row][col] !== '1') return;
grid[row][col] = 2;
const left = traverse(grid, row, col - 1);
const right = traverse(grid, row, col + 1);
const up = traverse(grid, row - 1, col);
const down = traverse(grid, row + 1, col);
}