When I submitted it, it fails at the case with a large matrix with Runtime Error but when I ran with this case it gave correct result. I wonder whether it's because the space cost of the recursion. Can anyone give a exact reason?

```
class Solution {
public:
void solve(vector<vector<char>>& board) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
helper(board, i, j);
}
}
// change all 'Y's to 'O's
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
board[i][j] = board[i][j] == 'Y' ? 'O' : board[i][j];
}
}
}
bool helper(vector<vector<char>>& board, int i, int j) {
if (i < 0 || j < 0 || i > board.size() - 1 || j > board[0].size() - 1 || board[i][j] == 'Y') { // reach to the edge
return true;
}
if (board[i][j] == 'X') {
return false;
}
board[i][j] = 'X';
bool res = helper(board, i - 1, j) || helper(board, i + 1, j) || helper(board, i, j - 1) || helper(board, i, j + 1);
if (res) {
board[i][j] = 'Y'; // if res is true, meaning this one is not surrounded, so set it to 'Y'
}
return res;
}
};
```