function dem = demFill( dem, max_iters )

hw=1;
height = size(dem,1);
width = size(dem,2);

for iters=1:max_iters
    if(~any(isnan(dem)))
        break;
    end
    new_dem = dem;
    for w=1+hw:width-hw
        for h=1+hw:height-hw
            if(~isnan(dem(h,w))),continue; end
            
            patch = dem(h-hw:h+hw,w-hw:w+hw);
            new_dem(h,w) = nanmedian(patch(:));
        end
    end
    
    dem = new_dem;
end

end

