Let's play the minesweeper game (Wikipedia, online game)!

You are given an m x n char matrix board representing the game board where:


	'M' represents an unrevealed mine,
	'E' represents an unrevealed empty square,
	'B' represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),
	digit ('1' to '8') represents how many mines are adjacent to this revealed square, and
	'X' represents a revealed mine.


You are also given an integer array click where click = [clickr, clickc] represents the next click position among all the unrevealed squares ('M' or 'E').

Return the board after revealing this position according to the following rules:


	If a mine 'M' is revealed, then the game is over. You should change it to 'X'.
	If an empty square 'E' with no adjacent mines is revealed, then change it to a revealed blank 'B' and all of its adjacent unrevealed squares should be revealed recursively.
	If an empty square 'E' with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
	Return the board when no more squares will be revealed.


&nbsp;
Example 1:

Input: board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
Output: [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]


Example 2:

Input: board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
Output: [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]


&nbsp;
Constraints:


	m == board.length
	n == board[i].length
	1 &lt;= m, n &lt;= 50
	board[i][j] is either 'M', 'E', 'B', or a digit from '1' to '8'.
	click.length == 2
	0 &lt;= clickr &lt; m
	0 &lt;= clickc &lt; n
	board[clickr][clickc] is either 'M' or 'E'.


class Solution:
    def calMines(self,board,x,y):
        directions = [(-1,-1), (0,-1), (1,-1), (1,0), (1,1), (0,1), (-1,1), (-1,0)]
        mines = 0
        for d in directions:
            r, c = x+d[0],y+d[1]
            if self.isValid(board,r,c) and (board[r][c] == 'M' or board[r][c] == 'X'):
                mines+=1
        return mines

    def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
        x,y = click[0],click[1]
        options = []
        if board[x][y] == "M":
            board[x][y] = "X"
        else:
            count = self.calMines(board,x,y)
            if count == 0:
                board[x][y] = "B"
                for r in range(x-1,x+2):
                    for c in range(y-1,y+2):
                        if self.isValid(board,r,c) and board[r][c]!='B':
                            self.updateBoard(board,[r,c])
            else:
                board[x][y] = str(count)

        return board
    
    
    def isValid(self,board,a,b):
            return 0<=a<len(board) and 0<=b<len(board[0])


        
class Solution {
    public char[][] updateBoard(char[][] board, int[] click) {
        int r = click[0];
        int c = click[1];
        if(board[r][c] == 'M')
        {
            board[r][c] = 'X';
            return board;
        }
        dfs(board, r, c);
        return board;
    }

    private void dfs(char[][]board, int r, int c)
    {
        if(r < 0 || r >= board.length || c >= board[0].length || c < 0 || board[r][c] == 'B')//Stop case
            return;
        int num = countMine(board, r, c);//count how many adjacent mines
        if(num != 0)
        {
            board[r][c] = (char)('0' + num);
            return;
        }
        else
        {
            board[r][c] = 'B';
            dfs(board, r + 1, c);//recursively search all neighbors
            dfs(board, r - 1, c);
            dfs(board, r, c + 1);
            dfs(board, r, c - 1);
            dfs(board, r - 1, c - 1);
            dfs(board, r + 1, c - 1);
            dfs(board, r - 1, c + 1);
            dfs(board, r + 1, c + 1);
        }
    }

    private int countMine(char[][]board, int r, int c)
    {
        int count = 0;
        for(int i = r - 1; i <= r + 1; ++i)
        {
            for(int j = c - 1; j <= c + 1; ++j)
            {
                if(i >= 0 && i < board.length && j >= 0 && j < board[0].length)
                {
                    if(board[i][j] == 'M')
                        count++;
                }
            }
        }
        return count;
    }
}
class Solution {
public:
    int m, n ;
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        if(board[click[0]][click[1]] == 'M'){
            board[click[0]][click[1]] = 'X' ;
            return board ;
        }
        else{
            m = board.size(), n = board[0].size() ;
            dfs(click[0], click[1], board) ;
        }
        return board ;
    }

    const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
    const int dy[8] = {0, -1, 0, 1, 1, -1, -1, 1};
    void dfs(int cr, int cc, vector<vector<char>> &board){
        int count = 0 ;
        for(int i = 0 ; i < 8 ; i++){
            int nr = cr + dx[i], nc = cc + dy[i] ;
            if(nr<0 || nr>=m || nc<0 || nc >=n || board[nr][nc]!='M') continue;
            count++ ;
        }

        if(count!=0){
            board[cr][cc] = '0'+count ;
            return ;
        }else{
            board[cr][cc] = 'B' ;
            for(int i = 0 ; i < 8 ; i++){
                int nr = cr + dx[i], nc = cc + dy[i] ;
                if(nr<0 || nr>=m || nc<0 || nc >=n || board[nr][nc]!='E') continue;
                dfs(nr, nc, board) ;
            }
        }
    }
};
var updateBoard = function(board, click) {
	const [clickR, clickC] = click;
	const traverseAround = ({ currentRow, currentCol, fun }) => {
		for (let row = -1; row <= 1; row++) {
			for (let col = -1; col <= 1; col++) {
				fun(currentRow + row, currentCol + col);
			}
		}
	};
	const getMinesCount = (currentRow, currentCol) => {
		let result = 0;

		function check(row, col) {
			const value = board[row]?.[col];
			if (value == 'M') result += 1;
		}
		traverseAround({ currentRow, currentCol, fun: check });
		return result;
	};
	const dfs = (row = clickR, col = clickC) => {
		const currnet = board[row]?.[col];
		if (currnet !== 'E') return;
		const minesCount = getMinesCount(row, col);
		board[row][col] = minesCount === 0 ? 'B' : `${minesCount}`;
		if (minesCount > 0) return;

		traverseAround({ currentRow: row, currentCol: col, fun: dfs });
	};

	board[clickR][clickC] === 'M'
		? board[clickR][clickC] = 'X'
		: dfs();
	return board;
};
Minesweeper