> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Island Perimeter Python Solution with Tests

> Tested Python solution for LeetCode 463 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 463, Easy. Topics: Array, Depth-First Search, Breadth-First Search, Matrix. [View on LeetCode](https://leetcode.com/problems/island-perimeter/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 463   # by problem number
lcpy gen -s island_perimeter   # by problem name
```

## Problem

You are given `row x col` `grid` representing a map where `grid[i][j] = 1` represents land and `grid[i][j] = 0` represents water.

Grid cells are connected **horizontally/vertically** (not diagonally). The `grid` is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).

The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100.

Determine the perimeter of the island.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/10/12/island.png)

```
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
```

```
Input: grid = [[1]]
Output: 4
```

```
Input: grid = [[1,0]]
Output: 4
```

### Constraints

* row == grid.length
* col == grid\[i].length
* 1 \<= row, col \<= 100
* grid\[i]\[j] is 0 or 1.
* There is exactly one island in grid.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/island_perimeter/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/island_perimeter/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(row * col) every cell inspected once
    # Space: O(1)
    def island_perimeter(self, grid: list[list[int]]) -> int:
        rows = len(grid)
        cols = len(grid[0])
        perimeter = 0
        for r in range(rows):
            for c in range(cols):
                if grid[r][c] == 0:
                    continue
                # Each land cell starts with 4 exposed sides; subtract shared edges.
                exposed = 4
                for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                    nr, nc = r + dr, c + dc
                    if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 1:
                        exposed -= 1
                perimeter += exposed
        return perimeter
```

## Complexity

| Time                                    | Space |
| --------------------------------------- | ----- |
| O(row \* col) every cell inspected once | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
