connect all 4 adjacent cell to current cell
if ( isSafe ( i , j + 1 , M ) ) g . addEdge ( k , k + 1 ) ; if ( isSafe ( i , j - 1 , M ) ) g . addEdge ( k , k - 1 ) ; if ( j < N - 1 && isSafe ( i + 1 , j , M ) ) g . addEdge ( k , k + N ) ; if ( i > 0 && isSafe ( i - 1 , j , M ) ) g . addEdge ( k , k - N ) ; }