2.21. Exercise: Original Minesweeper 2

Consider the same Minesweeper game in the question above. Assume that we have to comply with the original version and count the mined neighbours of a clear cell now in 8 directions including the north-west, north-east, south-east, and south-west.
Write a program which outputs the board with the calculated numbers (according to new neighborhood) as a nested list, for a given board where clear and mined cells are denoted by ’_’ and ’m’ characters respectively. For the mined cells, you can use ’m’ character again.
  • The board can be in any dimensions. In other words, \(M\) and \(N\) can be any positive integer.

  • Assume that the nested list of characters will be given to you as variable \(\texttt{B}\).

Sample I/O:

Input:
B = [['m', 'm', '_', '_', '_'],
     ['m', 'm', '_', '_', '_'],
     ['_', '_', 'm', '_', '_']]

Output:
[['m', 'm', 2, 0, 0], ['m', 'm', 3, 1, 0], [2, 3, 'm', 1, 0]]

Input:
B = [['_', '_', 'm', '_', '_'],
     ['m', '_', 'm', 'm', 'm'],
     ['m', '_', '_', '_', '_'],
     ['_', '_', 'm', '_', 'm'],
     ['_', '_', '_', '_', '_'],
     ['m', '_', '_', 'm', 'm'],
     ['_', 'm', '_', 'm', '_']]

Output:
[[1, 3, 'm', 4, 2], ['m', 4, 'm', 'm', 'm'], ['m', 4, 3, 5, 3], [1, 2, 'm', 2, 'm'], [1, 2, 2, 4, 3], ['m', 2, 3, 'm', 'm'], [2, 'm', 3, 'm', 3]]

Input:
B = [['_', '_', '_', 'm', '_', '_', '_'],
     ['_', '_', '_', 'm', 'm', 'm', 'm'],
     ['_', '_', '_', '_', '_', 'm', 'm'],
     ['m', '_', 'm', '_', 'm', '_', 'm'],
     ['m', '_', '_', 'm', 'm', 'm', '_'],
     ['_', '_', '_', '_', 'm', 'm', '_'],
     ['_', '_', '_', 'm', '_', 'm', '_']]

Output (Shown in separate lines for better visibility):
[[0, 0, 2, 'm', 4, 3, 2],
 [0, 0, 2, 'm', 'm', 'm', 'm'],
 [1, 2, 2, 4, 5, 'm', 'm'],
 ['m', 3, 'm', 4, 'm', 6, 'm'],
 ['m', 3, 2, 'm', 'm', 'm', 3],
 [1, 1, 2, 4, 'm', 'm', 3],
 [0, 0, 1, 'm', 4, 'm', 2]]