Every island (1 in grid) increases perimeter by 4, while every neighbor island decreases overall perimeter by 1. So, overall perimeter is 4*island_count- neighbor_count.

```
int islandPerimeter(vector<vector<int>>& grid) {
int island_count=0;
int neighbor_count=0;
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[i].size();j++){
if(grid[i][j]==1){
island_count++;
if(j>0 && grid[i][j-1]==1)
neighbor_count++;
if(j+1<grid[i].size() && grid[i][j+1]==1)
neighbor_count++;
if(i>0 && grid[i-1][j]==1)
neighbor_count++;
if(i+1<grid.size() && grid[i+1][j]==1)
neighbor_count++;
}
}
}
return (4*island_count)-(neighbor_count);
}
```