class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
"""
grid = [[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
"""
def inArea(i, j, rows, columns):
if i >= 0 and i < rows:
if j >= 0 and j < columns:
return True
return False
# up right down left
d = [(-1, 0), (0, 1), (1, 0), (0, -1)]
perimeter = 0
rows = len(grid)
columns = len(grid[0])
for i in range(rows):
for j in range(columns):
if grid[i][j] == 1:
pos = (i, j)
for k in range(len(d)):
pos_around = (pos[0]+d[k][0], pos[1]+d[k][1])
is_inArea = inArea(pos_around[0], pos_around[1], rows, columns)
if is_inArea:
if grid[pos_around[0]][pos_around[1]] == 1:
continue
perimeter += 1
return perimeter