


import torch
from torch import autograd



class 							lowercase__(						autograd.Function			):






      '''simple docstring'''







      @staticmethod
      def    UpperCAmelCase							(		__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Union[str, Any]:







         """simple docstring"""







         UpperCamelCase__ :			int 						=inputs.numel()
         UpperCamelCase__ :			int 						=int(0.0_05 * nb_elems) + 1
         if sigmoid:
            UpperCamelCase__ :			List[str] 						=(torch.sigmoid(_snake_case) > threshold).type(inputs.type())
         else:
            UpperCamelCase__ :			List[Any] 						=(inputs > threshold).type(inputs.type())
         if mask.sum() < nb_min:
            # We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
            UpperCamelCase__ :			Optional[Any] 						=inputs.flatten().kthvalue(max(nb_elems - nb_min   ,			1)).values
            UpperCamelCase__ :			Union[str, Any] 						=(inputs > k_threshold).type(inputs.type())
         return mask




      @staticmethod
      def    UpperCAmelCase							(		__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Any:







         """simple docstring"""







         return gradOutput, None, None




class 							lowercase__(						autograd.Function			):






      '''simple docstring'''







      @staticmethod
      def    UpperCAmelCase							(		__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			str 						=inputs.clone()
         UpperCamelCase__ :			List[str] 						=inputs.flatten().sort(descending=_snake_case)
         UpperCamelCase__ :			int 						=int(threshold * inputs.numel())

         # flat_out and mask access the same memory.
         UpperCamelCase__ :			str 						=mask.flatten()
         UpperCamelCase__ :			Tuple 						=0
         UpperCamelCase__ :			str 						=1
         return mask




      @staticmethod
      def    UpperCAmelCase							(		__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						str:







         """simple docstring"""







         return gradOutput, None




class 							lowercase__(						snake_case__			):






      '''simple docstring'''







      @staticmethod
      def    UpperCAmelCase							(		__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Union[str, Any]:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=inputs.clone()
         UpperCamelCase__ :			Dict 						=inputs.abs().flatten().sort(descending=_snake_case)
         UpperCamelCase__ :			Dict 						=int(threshold * inputs.numel())

         # flat_out and mask access the same memory.
         UpperCamelCase__ :			Optional[Any] 						=mask.flatten()
         UpperCamelCase__ :			Optional[int] 						=0
         UpperCamelCase__ :			Any 						=1
         return mask









UpperCamelCase											=      [
    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
]




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      bool:
 return 0 <= row < rows and 0 <= col < cols




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      int:
 lowerCamelCase_							:    Tuple							=     len(lowerCamelCase__				)
 lowerCamelCase_							:    Tuple							=     len(mat[0]				)
 if is_safe(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				) and (row, col) not in seen and mat[row][col] == 1:
  seen.add((row, col)				)
  return (
      1
      + depth_first_search(row + 1 ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
      + depth_first_search(row - 1 ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
      + depth_first_search(lowerCamelCase__ ,			col + 1 ,			lowerCamelCase__ ,			lowerCamelCase__				)
      + depth_first_search(lowerCamelCase__ ,			col - 1 ,			lowerCamelCase__ ,			lowerCamelCase__				)
  )
 else:
  return 0




def       _a   (							lowerCamelCase__				)  ->      int:
 lowerCamelCase_							:    set							=     set()

 lowerCamelCase_							:    int							=     0
 for row, line in enumerate(lowerCamelCase__				):
  for col, item in enumerate(lowerCamelCase__				):
   if item == 1 and (row, col) not in seen:
    # Maximizing the area
    lowerCamelCase_							:    str							=     max(lowerCamelCase__ ,			depth_first_search(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)				)
 return max_area


if __name__ == "__main__":
       import doctest

       print(find_max_area(matrix))  # Output -> 6
       doctest.testmod()
