The problem can be broken down by looking at the entire neighborhood (including the element in consideration). Lets call this neighborhood sum of 9 elements N(i), where i is the element whose neighborhood we are currently looking at. The state of the next can be deduced by looking at N(i) as follows:

if N(i) is 4, the next state will be the same as current state.This can be explained as follows. If i is 0, means there are 4 elements in the neighborhood which are 1. Hence, next state is still 0. If i is 1, three neighbors are alive(1). Hence, the state i remains 1 in the next update.

If N(i) is 3, the next state is 1 for i. If i is 0, there are exactly 3 alive neighbors , so it will come back to life in the next state. If i is 1, two neighbours are alive, so it will live on to the next state.

for any other case, the next state will be zero(i.e for N(i)<3 or N(i)>4).
The code is as follows:
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) { vector<vector<int>> next;
int row = board.size();
int col = board[0].size();
for(int i =0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
int sum = findSum(i,j,board, row, col);
// if sum of all 9 neighbours is 3, then the element is always 1
//if sum is 4, the current state is retained. In any other case, the state becomes 0
if(sum==3)
{
board[i][j] = (board[i][j] == 0 ? 2 :3);
}
else if(sum ==4)
{
board[i][j] = (board[i][j]==0 ?0 :3);
}
else board[i][j] = (board[i][j] ==0 ? 0 : 1);
}
}
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
board[i][j] = board[i][j]>>1;
}
}
}
int findSum(int a, int b, vector<vector<int>> matrix, int m, int n)
{
int count= 0;
for(int i = a1; i <=a+1; i++)
{
for(int j = b1; j <= b+1; j++ )
{
if(i>=0 && i<m && j>=0 && j<n)
{
count = count + (matrix[i][j] & 1);
}
}
}
return count;
}
};