2
* @param {number[][]} grid5
var closedIsland = function (grid) {6
let rows = grid.length;7
let cols = grid[0].length;8
let islandCount = 0; // Initial island count11
if (rows <= 2 || cols <= 2) return islandCount;13
for (let i = 0; i <= rows - 1; i++) {14
for (let j = 0; j <= cols - 1; j++) {16
If land was found on the border, it can never be enclosed by water.17
So, mark the all the adjacent land across grid as visited.21
(i == 0 || j == 0 || i == rows - 1 || j == cols - 1)28
// If land is found, increase the count and walk around land(adjacent indexes) to mark them as visited.29
for (let i = 1; i <= rows - 1; i++) {30
for (let j = 1; j <= cols - 1; j++) {31
if (grid[i][j] === 0) {38
// To walk around land and mark it as visited40
if (x < 0 || y < 0 || x >= rows || y >= cols) return;41
if (grid[x][y] !== 0) return;