def count_dict_keys(d, max_level=None):
    """Count the number of keys of a nested dictionary"""
    keys = 0
    for key, value in d.items():
        if isinstance(value, dict):
            if max_level is None:
                keys += count_dict_keys(value)
            elif max_level > 1:
                keys += count_dict_keys(value, max_level - 1)
            else:
                keys += 1
        else:
            keys += 1

    return keys