```
public int islandPerimeter(int[][] grid) {
int totalSides = 0;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == 1) {
int sides = 4;
if (row != 0) sides -= grid[row - 1][col]; // one above
if (row != grid.length - 1) sides-= grid[row + 1][col]; // one below
if (col != 0) sides -= grid[row][col - 1]; // one left
if (col != grid[row].length - 1) sides -= grid[row][col + 1]; // one right
totalSides += sides;
}
}
}
return totalSides;
}
```