<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p>

<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]
]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;]
]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 300</code></li>
	<li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

from typing import List, Self


class Position:
    rows: int
    columns: int

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def next(self) -> List[Self]:
        result = []
        x, y = self.x + 1, self.y
        if x < Position.columns:
            result.append(Position(x, y))
        x, y = self.x - 1, self.y
        if x >= 0:
            result.append(Position(x, y))
        x, y = self.x, self.y + 1
        if y < Position.rows:
            result.append(Position(x, y))
        x, y = self.x, self.y - 1
        if y >= 0:
            result.append(Position(x, y))
        return result

    def __eq__(self, value: Self) -> bool:
        return (self.x == value.x) and (self.y == value.y)

    def __hash__(self) -> int:
        return hash((self.x, self.y))

    def __repr__(self) -> str:
        return f"{self.x}, {self.y}"


class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        visited: set[Position] = set()
        num_islands = 0
        Position.rows = len(grid)
        Position.columns = len(grid[0])
        for row_n, row in enumerate(grid):
            for column_n, element in enumerate(row):
                p = Position(column_n, row_n)
                if element == "1" and p not in visited:
                    num_islands += 1
                    self.explore(grid, p, visited)
        return num_islands

    def explore(self, grid: List[List[str]], pos: Position, visited: set[Position]):
        next = {pos}
        while len(next) > 0:
            new_next = set()
            print(next)
            for n in next:
                visited.add(n)
                new_next.update(
                    i for i in n.next() if i not in visited and grid[i.y][i.x] == "1"
                )
            next = new_next
25448