adding mask_fn for apply_grid