





from __future__ import annotations

from collections.abc import Iterator
from typing import Generic, TypeVar

_UpperCAmelCase             =							TypeVar('T')






class 							snake_case_    (		Generic[T]						):






  def __init__(							self			:					Optional[Any]       ,  _snake_case			:					T      )->List[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	int      =       data
   __lowerCAmelCase					:	Node[T] | None      =       None






  def __str__(							self			:					List[str]      )->str:






   '''simple docstring'''

   return F'''{self.data}'''






class 							snake_case_    (		Generic[T]						):






  def __init__(							self			:					Tuple      )->None:






   '''simple docstring'''

   __lowerCAmelCase					:	Node[T] | None      =       None






  def __iter__(							self			:					int      )->Iterator[T]:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       self.top
   while node:
    yield node.data
    __lowerCAmelCase					:	int      =       node.next






  def __str__(							self			:					List[Any]      )->str:






   '''simple docstring'''

   return "->".join([str(_snake_case      ) for item in self]      )






  def __len__(							self			:					Optional[int]      )->int:






   '''simple docstring'''

   return len(tuple(iter(self      )      )      )






  def 				UpperCAmelCase__						(							self			:					Optional[Any]      )->bool:






   '''simple docstring'''

   return self.top is None






  def 				UpperCAmelCase__						(							self			:					Optional[int]       ,  _snake_case			:					T      )->None:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[int]      =       Node(_snake_case      )
   if not self.is_empty():
    __lowerCAmelCase					:	Optional[int]      =       self.top
   __lowerCAmelCase					:	Optional[int]      =       node






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]      )->T:






   '''simple docstring'''

   if self.is_empty():
    raise IndexError("""pop from empty stack"""      )
   assert isinstance(self.top       ,  _snake_case      )
   __lowerCAmelCase					:	Optional[Any]      =       self.top
   __lowerCAmelCase					:	str      =       self.top.next
   return pop_node.data






  def 				UpperCAmelCase__						(							self			:					Tuple      )->T:






   '''simple docstring'''

   if self.is_empty():
    raise IndexError("""peek from empty stack"""      )

   assert self.top is not None
   return self.top.data






  def 				UpperCAmelCase__						(							self			:					List[str]      )->None:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[int]      =       None


if __name__ == "__main__":
      from doctest import testmod

      testmod()






from __future__ import annotations






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[list[int]]   )      ->   bool:
 __lowerCAmelCase					:	Tuple      =       len(SCREAMING_SNAKE_CASE   )
 # We need to create solution object to save path.
 __lowerCAmelCase					:	str      =       [[0 for _ in range(SCREAMING_SNAKE_CASE   )] for _ in range(SCREAMING_SNAKE_CASE   )]
 __lowerCAmelCase					:	str      =       run_maze(SCREAMING_SNAKE_CASE   ,   0   ,   0   ,   SCREAMING_SNAKE_CASE   )
 if solved:
  print("""\n""".join(str(SCREAMING_SNAKE_CASE   ) for row in solutions   )   )
 else:
  print("""No solution exists!"""   )
 return solved






def 	_SCREAMING_SNAKE_CASE		(      SCREAMING_SNAKE_CASE		:list[list[int]]   ,   SCREAMING_SNAKE_CASE		:int   ,   SCREAMING_SNAKE_CASE		:int   ,   SCREAMING_SNAKE_CASE		:list[list[int]]   )      ->   bool:
 __lowerCAmelCase					:	List[str]      =       len(SCREAMING_SNAKE_CASE   )
 # Final check point.
 if i == j == (size - 1):
  __lowerCAmelCase					:	str      =       1
  return True

 __lowerCAmelCase					:	Optional[Any]      =       (not i < 0) and (not j < 0)  # Check lower bounds
 __lowerCAmelCase					:	Optional[Any]      =       (i < size) and (j < size)  # Check upper bounds

 if lower_flag and upper_flag:
  # check for already visited and block points.
  __lowerCAmelCase					:	int      =       (not solutions[i][j]) and (not maze[i][j])
  if block_flag:
   # check visited
   __lowerCAmelCase					:	Tuple      =       1

   # check for directions
   if (
       run_maze(SCREAMING_SNAKE_CASE   ,   i + 1   ,   SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )
       or run_maze(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ,   j + 1   ,   SCREAMING_SNAKE_CASE   )
       or run_maze(SCREAMING_SNAKE_CASE   ,   i - 1   ,   SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   )
       or run_maze(SCREAMING_SNAKE_CASE   ,   SCREAMING_SNAKE_CASE   ,   j - 1   ,   SCREAMING_SNAKE_CASE   )
   ):
    return True

   __lowerCAmelCase					:	Tuple      =       0
   return False
 return False


if __name__ == "__main__":
      import doctest

      doctest.testmod()