code: def _cofactor_matrix(M, method="berkowitz"):
      """Return a matrix containing the cofactor of each element.
  
      Parameters
      ==========
  
      method : string, optional
          Method to use to find the cofactors, can be "bareiss", "berkowitz",
          "bird", "laplace" or "lu".
  
      Examples
      ========
  
      >>> from sympy import Matrix
      >>> M = Matrix([[1, 2], [3, 4]])
      >>> M.cofactor_matrix()
      Matrix([
      [ 4, -3],
      [-2,  1]])
  
      See Also
      ========
  
      cofactor
      minor
      minor_submatrix
      """
  
      if not M.is_square:
          raise NonSquareMatrixError()
  
      return M._new(
          M.rows, M.cols, lambda i, j: M.cofactor(i, j, method)
      )
  
imports: from types import FunctionType
  from sympy.core.cache import cacheit
  from sympy.core.numbers import Float, Integer
  from sympy.core.singleton import S
  from sympy.core.symbol import uniquely_named_symbol
  from sympy.core.mul import Mul
  from sympy.polys import PurePoly, cancel
  from sympy.functions.combinatorial.numbers import nC
  from sympy.polys.matrices.domainmatrix import DomainMatrix
  from sympy.polys.matrices.ddm import DDM
  from .exceptions import NonSquareMatrixError
  from .utilities import _get_intermediate_simp, _get_intermediate_simp_bool, _iszero, _is_zero_after_expand_mul, _dotprodsimp, _simplify
label_desc: Use image node_id 8 for calling a global function with example usage: _cofactor_matrix(M, method) and returns: M