How to calculate the distance between specific side of an object and the closest to it surface's edge

I have some object which is located on the surface of free shape. Which is the best way to get the distance between A and B points? Let’s take A1-B1 as world X axis and A2-B2 as world Z axis.

I want to know how much (by N meters) I can move the object by X axis (for example) until it reaches the edge of the surface.

See the picture below:


I guess I need to compare object’s and surface’s bounds somehow, but I have no idea how to get the farthest point from the center of each in the specific direction.

Another idea is to do raycasting from the object towards the surface edge, but it’s flat, so there is no “wall” to get the hit.

Thank you in advance for the help!

If you have a freeform surface, checking it’s bounds wouldn’t be enough, because the bounds are like a cube which includes the whole gameobject, meaning it would be inaccurate in some cases. The easiest way would probably be
giving the surface mesh a meshcollider and raycasting down towards the surface step by step untill the raycast doesn’t hit the surface anymore.