Wow, that's a scary close call, glad nothing worse happened. I read an article a while back that said poor ventilation is a huge cause of stove issues, not just fires but also carbon monoxide with gas models. Jamie_moore has the right idea following the manual, those clearance rules exist for a reason. I always measure it out with a tape now because eyeballing it is way too risky. What kind of wall were you near, was it just regular drywall or something else?
Check the manual for your specific model, mine says 12 inches from anything combustible. I stick to that rule and sometimes add an extra few inches for peace of mind. Good airflow is everything for getting that heat away from surfaces.