from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

SCREAMING_SNAKE_CASE		:     Tuple         =	TypeVar("KEY")
SCREAMING_SNAKE_CASE		:     Tuple         =	TypeVar("VAL")

@dataclass(frozen=__a	,	slots=__a				)
class        UpperCamelCase						(				Generic[KEY, VAL]				):
						a__		:KEY
						a__		:VAL


class        UpperCamelCase						(				_Item				):





						def __init__(self						)				->				None:
												super().__init__(__UpperCamelCase			,     __UpperCamelCase						)





						def __bool__(self						)				->				bool:
												return False


SCREAMING_SNAKE_CASE		:     int         =	_DeletedItem()

class        UpperCamelCase						(				MutableMapping[KEY, VAL]				):





						def __init__(self			,     __UpperCamelCase = 8			,     __UpperCamelCase = 0.75						)				->				None:
												UpperCamelCase_		:							Union[str, Any]  =		initial_block_size
												UpperCamelCase_		:							list[_Item | None]  =		[None] * initial_block_size
												assert 0.0 < capacity_factor < 1.0
												UpperCamelCase_		:							List[Any]  =		capacity_factor
												UpperCamelCase_		:							Union[str, Any]  =		0





						def        A_							(self			,     __UpperCamelCase						)				->				int:
												return hash(__UpperCamelCase						) % len(self._buckets						)





						def        A_							(self			,     __UpperCamelCase						)				->				int:
												return (ind + 1) % len(self._buckets						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						)				->				bool:
												UpperCamelCase_		:							Dict  =		self._buckets[ind]
												if not stored:
																		UpperCamelCase_		:							Optional[Any]  =		_Item(__UpperCamelCase			,     __UpperCamelCase						)
																		self._len += 1
																		return True
												elif stored.key == key:
																		UpperCamelCase_		:							int  =		_Item(__UpperCamelCase			,     __UpperCamelCase						)
																		return True
												else:
																		return False





						def        A_							(self						)				->				bool:
												UpperCamelCase_		:							Union[str, Any]  =		len(self._buckets						) * self._capacity_factor
												return len(self						) >= int(__UpperCamelCase						)





						def        A_							(self						)				->				bool:
												if len(self._buckets						) <= self._initial_block_size:
																		return False
												UpperCamelCase_		:							Optional[int]  =		len(self._buckets						) * self._capacity_factor / 2
												return len(self						) < limit





						def        A_							(self			,     __UpperCamelCase						)				->				None:
												UpperCamelCase_		:							Dict  =		self._buckets
												UpperCamelCase_		:							Tuple  =		[None] * new_size
												UpperCamelCase_		:							Any  =		0
												for item in old_buckets:
																		if item:
																								self._add_item(item.key			,     item.val						)





						def        A_							(self						)				->				None:
												self._resize(len(self._buckets						) * 2						)





						def        A_							(self						)				->				None:
												self._resize(len(self._buckets						) // 2						)





						def        A_							(self			,     __UpperCamelCase						)				->				Iterator[int]:
												UpperCamelCase_		:							Optional[int]  =		self._get_bucket_index(__UpperCamelCase						)
												for _ in range(len(self._buckets						)						):
																		yield ind
																		UpperCamelCase_		:							Dict  =		self._get_next_ind(__UpperCamelCase						)





						def        A_							(self			,     __UpperCamelCase			,     __UpperCamelCase						)				->				None:
												for ind in self._iterate_buckets(__UpperCamelCase						):
																		if self._try_set(__UpperCamelCase			,     __UpperCamelCase			,     __UpperCamelCase						):
																								break





						def __setitem__(self			,     __UpperCamelCase			,     __UpperCamelCase						)				->				None:
												if self._is_full():
																		self._size_up()

												self._add_item(__UpperCamelCase			,     __UpperCamelCase						)





						def __delitem__(self			,     __UpperCamelCase						)				->				None:
												for ind in self._iterate_buckets(__UpperCamelCase						):
																		UpperCamelCase_		:							Tuple  =		self._buckets[ind]
																		if item is None:
																								raise KeyError(__UpperCamelCase						)
																		if item is _deleted:
																								continue
																		if item.key == key:
																								UpperCamelCase_		:							List[Any]  =		_deleted
																								self._len -= 1
																								break
												if self._is_sparse():
																		self._size_down()





						def __getitem__(self			,     __UpperCamelCase						)				->				VAL:
												for ind in self._iterate_buckets(__UpperCamelCase						):
																		UpperCamelCase_		:							Any  =		self._buckets[ind]
																		if item is None:
																								break
																		if item is _deleted:
																								continue
																		if item.key == key:
																								return item.val
												raise KeyError(__UpperCamelCase						)





						def __len__(self						)				->				int:
												return self._len





						def __iter__(self						)				->				Iterator[KEY]:
												yield from (item.key for item in self._buckets if item)





						def __repr__(self						)				->				str:
												UpperCamelCase_		:							Union[str, Any]  =		""" ,""".join(
												    f'''{item.key}: {item.val}''' for item in self._buckets if item						)
												return f'''HashMap({val_string})'''







from heapq import heappop, heappush

import numpy as np






def     lowerCAmelCase_			(   _SCREAMING_SNAKE_CASE     :				np.ndarray		, _SCREAMING_SNAKE_CASE     :				tuple[int, int]		, _SCREAMING_SNAKE_CASE     :				tuple[int, int]		, _SCREAMING_SNAKE_CASE     :				bool		, ):
						UpperCamelCase_,UpperCamelCase_		:							Union[str, Any]  =		grid.shape
						UpperCamelCase_		:							List[str]  =		[-1, 1, 0, 0]
						UpperCamelCase_		:							Dict  =		[0, 0, -1, 1]
						if allow_diagonal:
												dx += [-1, -1, 1, 1]
												dy += [-1, 1, -1, 1]

						UpperCamelCase_,UpperCamelCase_		:							List[Any]  =		[(0, source)], set()
						UpperCamelCase_		:							Any  =		np.full((rows, cols)		, np.inf			)
						UpperCamelCase_		:							List[str]  =		0
						UpperCamelCase_		:							Union[str, Any]  =		np.empty((rows, cols)		, dtype=_SCREAMING_SNAKE_CASE			)
						UpperCamelCase_		:							Optional[int]  =		None

						while queue:
												((UpperCamelCase_),(UpperCamelCase_))		:							Any  =		heappop(_SCREAMING_SNAKE_CASE			)
												if (x, y) in visited:
																		continue
												visited.add((x, y)			)

												if (x, y) == destination:
																		UpperCamelCase_		:							Tuple  =		[]
																		while (x, y) != source:
																								path.append((x, y)			)
																								UpperCamelCase_,UpperCamelCase_		:							Union[str, Any]  =		predecessors[x, y]
																		path.append(_SCREAMING_SNAKE_CASE			)  # add the source manually
																		path.reverse()
																		return matrix[destination], path

												for i in range(len(_SCREAMING_SNAKE_CASE			)			):
																		UpperCamelCase_,UpperCamelCase_		:							Optional[Any]  =		x + dx[i], y + dy[i]
																		if 0 <= nx < rows and 0 <= ny < cols:
																								UpperCamelCase_		:							Union[str, Any]  =		grid[nx][ny]
																								if next_node == 1 and matrix[nx, ny] > dist + 1:
																														heappush(_SCREAMING_SNAKE_CASE		, (dist + 1, (nx, ny))			)
																														UpperCamelCase_		:							Optional[Any]  =		dist + 1
																														UpperCamelCase_		:							List[Any]  =		(x, y)

						return np.inf, []


if __name__ == "__main__":
	import doctest

	doctest.testmod()






