


"""simple docstring"""


class snake_case      :  # Public class to implement a graph







		def __init__(					self      :       int    ,   a__      :       int    ,   a__      :       int    ,   a__      :       list[list[bool]]		)       ->						None:





						'''simple docstring'''







						_A					  =			row
						_A					  =			col
						_A					  =			graph







		def  a_ (					self      :       Tuple    ,   a__      :       int    ,   a__      :       int    ,   a__      :       list[list[bool]]		)       ->						bool:





						'''simple docstring'''







						return (
						    0 <= i < self.ROW
						    and 0 <= j < self.COL
						    and not visited[i][j]
						    and self.graph[i][j]
						)







		def  a_ (					self      :       str    ,   a__      :       int    ,   a__      :       int    ,   a__      :       list[list[bool]]		)       ->						None:





						'''simple docstring'''







						_A					  =			[-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
						_A					  =			[-1, 0, 1, -1, 1, -1, 0, 1]
						_A					  =			True  # Make those cells visited
						for k in range(8		):
										if self.is_safe(i + row_nbr[k]    ,   j + col_nbr[k]    ,   a__		):
														self.diffs(i + row_nbr[k]    ,   j + col_nbr[k]    ,   a__		)







		def  a_ (					self      :       List[str]		)       ->						int:  # And finally, count all islands.





						'''simple docstring'''







						_A					  =			[[False for j in range(self.COL		)] for i in range(self.ROW		)]
						_A					  =			0
						for i in range(self.ROW		):
										for j in range(self.COL		):
														if visited[i][j] is False and self.graph[i][j] == 1:
																		self.diffs(a__    ,   a__    ,   a__		)
																		count += 1
						return count



"""simple docstring"""


def      a__ (				__lowercase     ,			__lowercase   )	->	int:
				while a != 0:
								_A  ,		_A					  =			b % a, a
				return b






def      a__ (				__lowercase     ,			__lowercase   )	->	int:
				if gcd(__lowercase     ,			__lowercase   ) != 1:
								_A					  =			f"""mod inverse of {a!r} and {m!r} does not exist"""
								raise ValueError(__lowercase   )
				_A  ,		_A  ,		_A					  =			1, 0, a
				_A  ,		_A  ,		_A					  =			0, 1, m
				while va != 0:
								_A					  =			ua // va
								_A  ,		_A  ,		_A  ,		_A  ,		_A  ,		_A					  =			(ua - q * va), (ua - q * va), (ua - q * va), va, va, va
				return ua % m