


"""simple docstring"""



from __future__ import annotations

from collections import deque







class _lowerCAmelCase    :







   def __init__(     self						,     UpperCamelCase__						)     ->		Optional[Any]:

       '''simple docstring'''




       snake_case   :				list[dict]				 =   []
       self.adlist.append(
           {"value": "", "next_states": [], "fail_state": 0, "output": []}						)

       for keyword in keywords:
           self.add_keyword(UpperCamelCase__						)
       self.set_fail_transitions()







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		int | None:

       '''simple docstring'''




       for state in self.adlist[current_state]["next_states"]:
           if char == self.adlist[state]["value"]:
               return state
       return None







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						)     ->		None:

       '''simple docstring'''




       snake_case   :				List[str]				 =   0
       for character in keyword:
           snake_case   :				int				 =   self.find_next_state(UpperCamelCase__						,     UpperCamelCase__						)
           if next_state is None:
               self.adlist.append(
                   {
                       "value": character,
                       "next_states": [],
                       "fail_state": 0,
                       "output": [],
                   }						)
               self.adlist[current_state]["next_states"].append(len(self.adlist						) - 1						)
               snake_case   :				List[Any]				 =   len(self.adlist						) - 1
           else:
               snake_case   :				Tuple				 =   next_state
       self.adlist[current_state]["output"].append(UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		None:

       '''simple docstring'''




       snake_case   :				deque				 =   deque()
       for node in self.adlist[0]["next_states"]:
           q.append(UpperCamelCase__						)
           snake_case   :				Tuple				 =   0
       while q:
           snake_case   :				Any				 =   q.popleft()
           for child in self.adlist[r]["next_states"]:
               q.append(UpperCamelCase__						)
               snake_case   :				List[Any]				 =   self.adlist[r]["fail_state"]
               while (
                   self.find_next_state(UpperCamelCase__						,     self.adlist[child]["value"]						) is None
                   and state != 0
               ):
                   snake_case   :				Union[str, Any]				 =   self.adlist[state]["fail_state"]
               snake_case   :				str				 =   self.find_next_state(
                   UpperCamelCase__						,     self.adlist[child]["value"]						)
               if self.adlist[child]["fail_state"] is None:
                   snake_case   :				Any				 =   0
               snake_case   :				int				 =   (
                   self.adlist[child]["output"]
                   + self.adlist[self.adlist[child]["fail_state"]]["output"]
               )







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						)     ->		dict[str, list[int]]:

       '''simple docstring'''




       snake_case   :				dict				 =   {}  # returns a dict with keywords and list of its occurrences
       snake_case   :				Union[str, Any]				 =   0
       for i in range(len(UpperCamelCase__						)						):
           while (
               self.find_next_state(UpperCamelCase__						,     string[i]						) is None
               and current_state != 0
           ):
               snake_case   :				Union[str, Any]				 =   self.adlist[current_state]["fail_state"]
           snake_case   :				int				 =   self.find_next_state(UpperCamelCase__						,     string[i]						)
           if next_state is None:
               snake_case   :				Union[str, Any]				 =   0
           else:
               snake_case   :				Optional[Any]				 =   next_state
               for key in self.adlist[current_state]["output"]:
                   if key not in result:
                       snake_case   :				Tuple				 =   []
                   result[key].append(i - len(UpperCamelCase__						) + 1						)
       return result


if __name__ == "__main__":
  import doctest

  doctest.testmod()









"""simple docstring"""



def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    def update_area_of_max_square(lowercase  :  int						,							lowercase  :  int			) -> int:
        # BASE CASE
        if row >= rows or col >= cols:
            return 0

        snake_case   :				Dict				 =   update_area_of_max_square(lowercase						,							col + 1			)
        snake_case   :				Tuple				 =   update_area_of_max_square(row + 1						,							col + 1			)
        snake_case   :				Any				 =   update_area_of_max_square(row + 1						,							lowercase			)

        if mat[row][col]:
            snake_case   :				List[Any]				 =   1 + min([right, diagonal, down]			)
            snake_case   :				Any				 =   max(largest_square_area[0]						,							lowercase			)
            return sub_problem_sol
        else:
            return 0

    snake_case   :				int				 =   [0]
    update_area_of_max_square(0						,							0			)
    return largest_square_area[0]







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    def update_area_of_max_square_using_dp_array(
        lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			) -> int:
        if row >= rows or col >= cols:
            return 0
        if dp_array[row][col] != -1:
            return dp_array[row][col]

        snake_case   :				List[str]				 =   update_area_of_max_square_using_dp_array(lowercase						,							col + 1						,							lowercase			)
        snake_case   :				Optional[int]				 =   update_area_of_max_square_using_dp_array(row + 1						,							col + 1						,							lowercase			)
        snake_case   :				Optional[int]				 =   update_area_of_max_square_using_dp_array(row + 1						,							lowercase						,							lowercase			)

        if mat[row][col]:
            snake_case   :				Dict				 =   1 + min([right, diagonal, down]			)
            snake_case   :				Union[str, Any]				 =   max(largest_square_area[0]						,							lowercase			)
            snake_case   :				str				 =   sub_problem_sol
            return sub_problem_sol
        else:
            return 0

    snake_case   :				Union[str, Any]				 =   [0]
    snake_case   :				int				 =   [[-1] * cols for _ in range(lowercase			)]
    update_area_of_max_square_using_dp_array(0						,							0						,							lowercase			)

    return largest_square_area[0]







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    snake_case   :				int				 =   [[0] * (cols + 1) for _ in range(rows + 1			)]
    snake_case   :				List[Any]				 =   0
    for row in range(rows - 1						,							-1						,							-1			):
        for col in range(cols - 1						,							-1						,							-1			):
            snake_case   :				Tuple				 =   dp_array[row][col + 1]
            snake_case   :				Any				 =   dp_array[row + 1][col + 1]
            snake_case   :				List[str]				 =   dp_array[row + 1][col]

            if mat[row][col] == 1:
                snake_case   :				Optional[int]				 =   1 + min(lowercase						,							lowercase						,							lowercase			)
                snake_case   :				Tuple				 =   max(dp_array[row][col]						,							lowercase			)
            else:
                snake_case   :				List[Any]				 =   0

    return largest_square_area







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  int						,							lowercase  :  list[list[int]]			)     ->						int:


    """simple docstring"""





    snake_case   :				Any				 =   [0] * (cols + 1)
    snake_case   :				Any				 =   [0] * (cols + 1)
    snake_case   :				Any				 =   0
    for row in range(rows - 1						,							-1						,							-1			):
        for col in range(cols - 1						,							-1						,							-1			):
            snake_case   :				Dict				 =   current_row[col + 1]
            snake_case   :				List[Any]				 =   next_row[col + 1]
            snake_case   :				Dict				 =   next_row[col]

            if mat[row][col] == 1:
                snake_case   :				Union[str, Any]				 =   1 + min(lowercase						,							lowercase						,							lowercase			)
                snake_case   :				Optional[int]				 =   max(current_row[col]						,							lowercase			)
            else:
                snake_case   :				Optional[Any]				 =   0
        snake_case   :				str				 =   current_row

    return largest_square_area


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))





