If a line segment from one corner of a pixel to another corner has length less than $L$, we can move the endpoints a tiny bit to avoid ever intersecting a corner between pixels, and still have length less than $L$.
In this case, if the original corners were at horizontal distance $x \cdot dX$ and vertical distance $y \cdot dY$, the new segment will cross $x+1$ vertical cell boundaries and $y+1$ horizontal cell boundaries; since it will cross $x+y+2$ cell boundaries total, it will pass through $x+y+3$ cells.
So we want to maximize $x+y+3$ subject to $(x \cdot dX)^2 + (y \cdot dY)^2 < L^2$; the $<$ condition will usually not bother us, but it is there to make sure that a line segment beginning at a corner of a cell does not count as intersecting the cell.
If we forget about the integer constraint (that $x,y \in \mathbb Z$), then the method of Lagrange multipliers tells us that the maximum occurs close to the point where the ellipse $(x \cdot dX)^2 + (y \cdot dY)^2 = L^2$ has a tangent line with slope $-1$ (equal to the slope of $x+y+3=k$). This solution is
$$
(x,y) = \left(\frac{dY}{dX} \cdot \frac{L}{\sqrt{dX^2 + dY^2}}, \frac{dX}{dY} \cdot \frac{L}{\sqrt{dX^2 + dY^2}}\right)
$$
with $x+y+3 = L \sqrt{\frac1{dX^2} + \frac1{dY^2}} + 3$.
If we replace $x,y$ by $\lceil x\rceil - 1$ and $\lceil y \rceil-1$, we get an integer point where $(x \cdot dX)^2 + (y \cdot dY)^2 < L^2$, and the value $x+y+3$ decreases by at most $2$. (If it decreased by exactly $2$, then the original value $x+y+3$ was already unachievable, since it required having $(x \cdot dX)^2 + (y \cdot dY)^2 = L^2$ exactly.) Therefore this gives us the correct answer up to an error of $1$.
Unfortunately, fixing this error would require a search of integer points in some elliptical sliver of the form $(x \cdot dX)^2 + (y \cdot dY)^2 < L^2$ and $x+y \ge k$. Essentially, we are looking for fractional solutions that are worse than the optimal one but "round better". This is something that has to be done case-by-case.
But if you are okay with being off by $1$ sometimes, then we do have a closed-form solution: we get $$\left\lceil \frac{dY}{dX} \cdot \frac{L}{ \sqrt{dX^2+dY^2}}\right\rceil +\left\lceil \frac{dX}{dY} \cdot \frac{L}{ \sqrt{dX^2+dY^2}}\right\rceil +1$$ (or possibly $+2$) intersections.