class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
perometer = 0
up, left, right, down = [0, 0, 0, 0]
bounding = 0
for i in xrange(len(grid)):
for j in xrange(len(grid[i])):
if grid[i][j]==0:
if i==0:
up = 0
if i+1 <= len(grid)1:
down = grid[i+1][j]
if j==0:
left = 0
if j+1 <= len(grid[i])1:
right = grid[i][j+1]
elif j==(len(grid[i])1):
right = 0
left = grid[i][j1]
else:
left = grid[i][j1]
right = grid[i][j+1]
elif i==(len(grid)1):
down = 0
up = grid[i1][j]
if j==0:
left = 0
if j+1 <= len(grid[i])1:
right = grid[i][j+1]
elif j==(len(grid[i])1):
right = 0
left = grid[i][j1]
else:
left = grid[i][j1]
right = grid[i][j+1]
else:
down = grid[i+1][j]
up = grid[i1][j]
if j==0:
left = 0
if j+1 <= len(grid[i])1:
right = grid[i][j+1]
elif j==(len(grid[i])1):
right = 0
left = grid[i][j1]
else:
left = grid[i][j1]
right = grid[i][j+1]
perometer += up + down + left + right
elif grid[i][j]==1:
if i==0:
bounding += 1
if i==(len(grid)1):
bounding += 1
if j==0:
bounding += 1
if j==(len(grid[i])1):
bounding += 1
perometer += bounding
return perometer

count the number of
1
around0
, and the direction is just down the row or col, then get theperometer
; 
count the number of
1
which is in the boundary, get thebounding
; 
add
perometer += bounding
as the result;