// Directions array to check all 8 neighboring cells
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
bool isValid(int x, int y, int n, int m) {
return (x >= 0 && x < n && y >= 0 && y < m);
vector<vector<char>> minefield(n, vector<char>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
// Create a new vector to store the result
// Attention that the vector is a copy of the origin one
vector<vector<char>> result = minefield;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (minefield[i][j] == '?') {
for (int d = 0; d < 8; ++d) {
if (isValid(newX, newY, n, m) && minefield[newX][newY] == '*') {
// Convert the number to a character
// Since there would be always less than 8 mines,
// this part would add the ascii of '0' with mine_count and would be printed in char.
// The step converted the number to a char again, and just the correct add-up number.
result[i][j] = '0' + mine_count;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {