Island Perimeter

class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
n = len(grid) #rows
m = len(grid[0]) #columns
result = 0
for i in range(n):
for j in range(m):
if grid[i][j] == 0:
continue
if j == 0 or grid[i][j-1] == 0:
result += 1
if i == 0 or grid[i-1][j] == 0:
result += 1
if j == m-1 or grid[i][j+1] == 0:
result += 1
if i == n-1 or grid [i+1][j] == 0:
result += 1
return resultLast updated