```
/**
* @param {number[][]} grid
* @return {number}
*/
var islandPerimeter = function(grid) {
var edges = 0;
grid.forEach((row, r) => {
row.forEach((col, c) => {
var left = grid[r][c-1] || 0;
var right = grid[r][c+1] || 0;
var top = (grid[r-1] || [])[c] || 0;
var bottom = (grid[r-1] || [])[c] || 0;
if (col === 1) {
edges += (4 - top - bottom - left - right)
}
})
});
return edges;
};
```