When we say, the speed of a light in a medium, we are actually referring to the average velocity of light, I'll explain myself.
Think about only one photon reaching the "dense medium", as we know from quantum electrodynamics, light interacts with charged particles, and this dense medium, is nothing but a lot of charged particles: protons, neutrons(even they interact with light because they are "made of" charged quarks), electrons...
So if this photon enters the medium, it will interact, or not, with the rest of the particles, resulting that the time it takes the photon to escape from the medium, may, or may not fulfill the relation c=L/t with L the lenght of the medium, I mean, it may interact with no particle, it may interact "a bit" or it may interact "a lot". So the velocity of light in a medium is more like the average velocity it takes a photon to escape from the medium.
Now think about neutrinos, they are massive particles, so they do not travel at speed of light, but their interaction with matter is so weak, that in some mediums, we see that they go faster than light. https://physics.stackexchange.com/questions/94138/do-neutrinos-travel-faster-than-light-in-air#:~:text=Neutrinos%20will%20travel%20faster%20than,%2Fs%20and%20n%3E1.
Also I want to remark that your calculation is wrong. For massless particles, the relation $E = \gamma m$ does not hold, instead you should use $E =\frac{p}{\beta}$ with $\beta = v$ (all this with units c=1, I'm sorry).
What you should finally understand is that you are using microscopic arguments, to directly solve a macroscopic problem. Understand macroscopic behaviour from microscopic behaviour is possible but complicate, and needs to use average of certain properties between a lot of complicated stuff, for example the famous $T\propto mv^2$ from the kinetic theory of gases that relates the temperature of a gas with the squared average velocity of its particles.
I hope I made my answer clear, if not whe can still comment on this!