Approach: Iterating and checking for shores class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) def not_land(i, j): if 0 <= i < m and 0 <= j < n: return grid[i][j] == 0 return 1 perim = 0 for i in range(m): for j in range(n): if grid[i][j] == 1: perim += int(not_land(i - 1, j)) + \ int(not_land(i + 1, j)) + \ int(not_land(i, j + 1)) + \ int(not_land(i, j - 1)) return perim Complexity Time: O(n) - where n is the number of cells in the grid. Space: O(1)