<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def from_mask(cls, dh_mask, lmax, nmax=None):
 """ Construct Slepian functions that are optimally concentrated within the region specified by a mask. Usage ----- x = Slepian.from_mask(dh_mask, lmax, [nmax]) Returns ------- x : Slepian class instance Parameters dh_mask :ndarray, shape (nlat, nlon) A Driscoll and Healy (1994) sampled grid describing the concentration region R. All elements should either be 1 (for inside the concentration region) or 0 (for outside the concentration region). The grid must have dimensions nlon=nlat or nlon=2*nlat, where nlat is even. lmax : int The spherical harmonic bandwidth of the Slepian functions. nmax : int, optional, default = (lmax+1)**2 The number of best-concentrated eigenvalues and eigenfunctions to return. """

        if nmax is None:
            nmax = (lmax + 1)**2
        else:
            if nmax > (lmax + 1)**2:
                raise ValueError('nmax must be less than or equal to ' +
                                 '(lmax + 1)**2. lmax = {:d} and nmax = {:d}'
                                 .format(lmax, nmax))

        if dh_mask.shape[0] % 2 != 0:
            raise ValueError('The number of latitude bands in dh_mask ' +
                             'must be even. nlat = {:d}'
                             .format(dh_mask.shape[0]))

        if dh_mask.shape[1] == dh_mask.shape[0]:
            _sampling = 1
        elif dh_mask.shape[1] == 2 * dh_mask.shape[0]:
            _sampling = 2
        else:
            raise ValueError('dh_mask must be dimensioned as (n, n) or ' +
                             '(n, 2 * n). Input shape is ({:d}, {:d})'
                             .format(dh_mask.shape[0], dh_mask.shape[1]))

        mask_lm = _shtools.SHExpandDH(dh_mask, sampling=_sampling, lmax_calc=0)
        area = mask_lm[0, 0, 0] * 4 * _np.pi

        tapers, eigenvalues = _shtools.SHReturnTapersMap(dh_mask, lmax,
                                                         ntapers=nmax)

        return SlepianMask(tapers, eigenvalues, area, copy=False)