The answer is
$$\frac{d}{\max\left( |\cos\theta|, |\sin\theta| \right)}$$
To understand why the answer has this form, let us look at a related problem.
Let's say we have a convex $n$-gon with origin in its interior. If we shoot a light ray from origin in direction $\vec{t} = (\cos\theta,\sin\theta)$, how far will the light travel before it hit the boundary?
For the $i^{th}$ edge of the polygon, let $\vec{n}_i = (\cos\theta_i,\sin\theta_i)$ be its outward pointing normal and $d_i$ be its distance to origin. The region bounded by the $n$-gon is
the collection of point $\vec{p} = (x,y)$ which satisfies the inequalities
$$\vec{p}\cdot \vec{n}_i = x\cos\theta_i + y \sin\theta_i \le d_i\quad\text{ for } 1 \le i \le n$$
For a point $\vec{p} = \lambda \vec{t} = \lambda (\cos\theta,\sin\theta)$ on the ray, this becomes
$$ \lambda (\cos\theta\cos\theta_i + \sin\theta\sin\theta_i) \le d_i
\quad\iff\quad \lambda \cos(\theta - \theta_i) \le d_i$$
for all $1 \le i \le n$. Since $\lambda > 0$, this is equivalent to
$$\frac{1}{\lambda} \ge \frac{1}{d_i}\cos(\theta - \theta_i)\;\text{ for all }i
\quad\iff\quad \frac{1}{\lambda} \ge \max_{1\le i \le n}\left(\frac{1}{d_i}\cos(\theta - \theta_i)\right)$$
This implies the light will travel along direction $\vec{t}$ for a distance
$$\Lambda(\theta) \stackrel{def}{=} \frac{1}{\max\limits_{1\le i \le n}\left(\frac{1}{d_i}\cos(\theta - \theta_i)\right)}$$
before it hit the boundary.
Back to problem at hand. If we choose a coordinate system to make the square centered at origin with sides parallel to $x$- and $y$- axis, then
$n = 4$ with $(\theta_1,\theta_2,\theta_3,\theta_4) = (0,\frac{\pi}{2},\pi,\frac{3\pi}{2})$ and all $d_i$ equals to $\frac{d}{2}$. This implies
$$\begin{align}\Lambda(\theta)
&= \frac{d}{2\max\left[
\cos\theta, \cos\left(\theta - \frac{\pi}{2}\right),
\cos(\theta - \pi),
\cos\left(\theta - \frac{3\pi}{2}\right)\right]}\\
&= \frac{d}{2\max(|\cos\theta|,|\sin\theta|)}\end{align}$$
The length you seek is simply
$$\Lambda(\theta) + \Lambda(\theta+\pi) = 2\Lambda(\theta) = \frac{d}{\max(|\cos\theta|,|\sin\theta|)}$$
Let us focus on $\Delta ASM$. Let $\angle ABS$ be $x$. We know the values of $\angle ASM$, $AM$ and $SM$. Using the law of sines we get:
$$\frac {AM}{\sin\angle ASM}=\frac {SM}{\sin\angle MAS}$$
$$\frac r{\sin x}=\frac d{\sin \theta}$$
$$\theta=\sin^{-1}\biggr(\frac {d\sin x}r\biggr)$$
Now that you know two angles of the triangle, you can find the angle $\angle AMB$ which subtends the required chord. Here onwards it is really easy.
Best Answer
Here's your diagram with a few modifications. Treating your points as being their equivalent upper case versions (since that's more standard), then I have labeled the midpoint of $AB$ as $G$, the midpoint of $CD$ as $H$, and added the line segments of $OG$ and $OH$.
Since $\lvert OA\rvert = \lvert OB\rvert$, and similarly $\lvert OC\rvert = \lvert OD\rvert$, as they are circle radii, then $\triangle OAB$ and $\triangle OCD$ are both isosceles. Thus, as indicated in Isosceles triangle height, the altitude and median coincide. This is due to, for example, using SSS that $\triangle DHO \cong CHO$ so $\measuredangle DHO = \measuredangle CHO$ and, as they form a straight line, the angles add to $180^{\circ}$ so each is $90^{\circ}$. This results in, as shown,
$$\measuredangle OGA = \measuredangle OHD = 90^\circ$$
Since $\lvert AG\rvert = \lvert DH\rvert$, as they are both half of the equal lengths of $\lvert AB\rvert$ and $\lvert CD\rvert$, then using the Pythagorean theorem with $\triangle OGA$ and $\triangle OHD$, we get that with the hypotenuse and a side length of $2$ right-angled triangles being equal, then so is the third side length (with it being the square root of the difference of squares of the hypotenuse and side lengths), i.e.,
$$\lvert OG\rvert = \lvert OH\rvert$$
Once again using the Pythagorean theorem with $\triangle OGP$ and $\triangle OHP$, we get that
$$\lvert GP\rvert = \lvert HP\rvert$$
By SSS, we now have $\triangle GPO \cong \triangle HPO$, so this finally gives that
$$\measuredangle GPO = \measuredangle HPO \;\;\to\;\; \measuredangle APF = \measuredangle DPF$$