```
class Solution(object):
def islandPerimeter(self, grid):
ans = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
ans += 4
if i < len(grid)-1 and grid[i+1][j] == 1:
ans -= 2
if j < len(grid[0])-1 and grid[i][j+1] == 1:
ans -= 2
return ans
```