You are given a D grid where represents land and represents water. The grid contains exactly one island (a connected group of land cells). The island has no lakes (water inside that is not connected to the outside water). Each cell is a square with side length . Find the perimeter of the island.
This problem does not need graph traversal. You can solve it by counting edges directly. For each land cell, count how many of its neighbors are water or out of bounds. That count is its contribution to the perimeter.
Time: to scan all cells. Space: since you only need counters.