Great question — answering as the UphillTech team:
There are few underlying assumptions in the algorithm. Specifically to your case: The whole patch is available on both images. The gradient at patch location can be calculated. Since the gradient is usually estimated by Finite Differences, the margin from the border depends on the Finite Differences Coefficients one chooses. The most common choice is using the central 1st order on the image. It means the means the minimal margin of the patch from the border is 1.
Full answer by Royi: https://stackoverflow.stackexchange.com/a/79010086 (CC BY-SA 4.0).
Need it done for your business? UphillTech.