OBJECTIVE: Understanding neural sources behind MEG and EEG signals is significant for basic and clinical neuroscience. Existing techniques, such as Recursively Applied and Projected Multiple Signal Classification (RAP-MUSIC) and Alternating Projection (AP), rely on limited current dipoles, representing focal sources with zero spatial extent. However, this oversimplifies realistic neural activity, which exhibits varying spatial extents. METHODS: To address this, we enhanced the AP approach, creating FLEX-AP, capable of localizing discrete and extended sources. FLEX-AP simultaneously optimizes location and spatial extent of candidate sources. RESULTS: FLEX-AP demonstrated superior performance, reducing localization errors compared to AP, RAP-MUSIC, and FLEX-RAP-MUSIC, particularly with extended sources. Moreover, FLEX-AP exhibited enhanced robustness against modeling errors in realistic scenarios. Applying FLEX-AP to MEG recordings of auditory responses validated its effectiveness, underscoring potential in advancing neuroscientific investigations. CONCLUSION: FLEX-AP offers a robust, flexible framework for M/EEG source localization, overcoming limitations of simplistic zero-extent dipole models. By accurately estimating position and spatial extent of neural sources, FLEX-AP bridges the gap between theoretical models and realistic activity, demonstrating utility in simulated and real-world scenarios. SIGNIFICANCE: FLEX-AP advances source imaging techniques with implications for basic neuroscience and clinical applications. Modeling extended sources precisely enables more accurate investigations of brain dynamics, potentially improving diagnostic and therapeutic approaches for neurological and psychiatric disorders.