
"""simple docstring"""





from heapq import heappop, heappush

import numpy as np







def   A__ ( A__      ,      A__      ,      A__      ,      A__      ,      )		->			Optional[int]:





   '''simple docstring'''




   _UpperCAmelCase         =      grid.shape
   _UpperCAmelCase         =      [-1, 1, 0, 0]
   _UpperCAmelCase         =      [0, 0, -1, 1]
   if allow_diagonal:
      dx += [-1, -1, 1, 1]
      dy += [-1, 1, -1, 1]

   _UpperCAmelCase         =      [(0, source)], set()
   _UpperCAmelCase         =      np.full((rows, cols)      ,      np.inf		)
   _UpperCAmelCase         =      0
   _UpperCAmelCase         =      np.empty((rows, cols)      ,      dtype=_lowercase		)
   _UpperCAmelCase         =      None

   while queue:
      (_UpperCAmelCase)         =      heappop(_lowercase		)
      if (x, y) in visited:
         continue
      visited.add((x, y)		)

      if (x, y) == destination:
         _UpperCAmelCase         =      []
         while (x, y) != source:
            path.append((x, y)		)
            _UpperCAmelCase         =      predecessors[x, y]
         path.append(_lowercase		)  # add the source manually
         path.reverse()
         return matrix[destination], path

      for i in range(len(_lowercase		)		):
         _UpperCAmelCase         =      x + dx[i], y + dy[i]
         if 0 <= nx < rows and 0 <= ny < cols:
            _UpperCAmelCase         =      grid[nx][ny]
            if next_node == 1 and matrix[nx, ny] > dist + 1:
               heappush(_lowercase      ,      (dist + 1, (nx, ny))		)
               _UpperCAmelCase         =      dist + 1
               _UpperCAmelCase         =      (x, y)

   return np.inf, []


if __name__ == "__main__":
 import doctest

 doctest.testmod()







def lowerCamelCase__  (     _lowercase					,		_lowercase ):

     '''simple docstring'''


     while a != 0:
          UpperCAmelCase_,       UpperCAmelCase_     :				Optional[int]  =     b % a, a
     return b







def lowerCamelCase__  (     _lowercase					,		_lowercase ):

     '''simple docstring'''


     if gcd(_lowercase					,		_lowercase ) != 1:
          UpperCAmelCase_     :				int  =     f'''mod inverse of {a!r} and {m!r} does not exist'''
          raise ValueError(_lowercase )
     UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_     :				Union[str, Any]  =     1, 0, a
     UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_     :				Dict  =     0, 1, m
     while va != 0:
          UpperCAmelCase_     :				List[Any]  =     ua // va
          UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_,       UpperCAmelCase_     :				Any  =     (ua - q * va), (ua - q * va), (ua - q * va), va, va, va
     return ua % m