Click a covered board and watch the reveal spread one breadth-first ring at a time: zeros open their neighbours, numbers stop the flood (LeetCode 529). O(R × C) time and space.
Click a cell with no mines around it and the game opens a whole region at once. That region is a breadth-first search from the click, and the numbered cells are where it stops.
Take a cell off the front of the queue and count the mines among its 8 neighbours, diagonals included.
If the count is above 0, write it on the cell and move on. A numbered cell never pushes its neighbours, because opening them could open a mine.
If the count is 0 the cell opens blank, and every covered neighbour not already queued goes on the back of the queue. They are all safe: a 0 has no mine next to it.
Because the queue is first in, first out, the board opens one ring at a time around the click, until the queue runs dry. The numbers that stopped it form the region’s fence.