title: Find the total number of cycles in the given matrix
body: <p>Gary has a board of size NxM. Each cell in the board is a coloured dot. There exist only 26 colours denoted by uppercase Latin characters (i.e. A,B,...,Z). Now Gary is getting bored and wants to play a game. The key to this game is to find a cycle that contains dots of the same colour. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:
  1. These k dots are different: if i ≠ j then di is different from dj.
  2. k is at least 4.
  3. All dots belong to the same colour.
  4. For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.</p>
  
  <p><strong>Input Format</strong>
  Line 1:  Two integers N and M, the number of rows and columns of the board</p>
  
  <p>Next N lines:  A string consisting of M characters, expressing colours of dots in each line. Each character is an uppercase Latin letter.</p>
  
  <p><strong>Output Format</strong> : Return 1 if there is a cycle else return 0</p>
  
  <ol>
  <li>I thought that I'll make a 'visited' matrix which will mark 1 for an index if it has already been looked into.</li>
  <li>Then, I'll traverse for each element and pass it to a new function which will work recursively and return 1 if the cycle exists.
  3.I'll pass the visited matrix, dimensions of original matrix(will be same for visited matrix),int i, int j(i and j are the original indices passes to this function by our original function and does not change during recursive calls), int p, int q(p and q keep on changing to alternate eligible indices), count(if count>=4 and p and q becomes adjacent to i and j) return 1;</li>
  <li>I'll also create an <strong>iseligible</strong> function to check if the given index is not out of bounds.</li>
  </ol>
  
  <p><em>I need help with the approach too. I guess I've overcomplicated this problem. Please help me with it, like in what direction should I think.  I've already spent more than two days on it.</em></p>
  
  <p><strong>Solution.h</strong></p>
  
  <pre><code>
  int x[4]={0,0,1,-1};
  int y[4]={1,-1,0,0};
  
  
  int iseligible(char board[][MAXN], int n, int m, int i, int j, int** visited){
  
      if (i&lt;m &amp;&amp; j&lt;n &amp;&amp; i&gt;=0 &amp;&amp; j&gt;=0 &amp;&amp; visited[i][j]==0)
          return 1;
  
      return 0;
  }
  
  int solver(char board[][MAXN], int n, int m,int i, int j,int p, int q, int count, int** visited){
  
      //Base case-if our pq are surrounding of ij and count&gt;=4
  
      for(int k=0;k&lt;4;k++){
  
          if(count&gt;=4 &amp;&amp; p==i+x[k] &amp;&amp; q==j+y[k]){
              return 1;
          }
      }
  
      for(int k=0;k&lt;4;k++){
          if(iseligible(board, n, m, i+x[k], j+y[k], visited)==1){
  
              visited[i+x[k]][j+y[k]]==1;
  
              if(solver(board, n, m, i+x[k], j+y[k], i+x[k], j+y[k], 1, visited)){    
                  return 1;
              }
  
              if(solver(board, n, m, i, j, i+x[k], j+y[k], count+1, visited)==1){
                  return 1;   
              }
  
          }
      }
  
  
      return 0;
  }
  
  
  int solve(char board[][MAXN],int n, int m)
  {
      int result=0;
      int** visited = new int*[m];
  
      for(int i=0;i&lt;m;i++){
          visited[i] = new int[n];
  
          for(int j = 0; i&lt;n; j++){
              visited[i][j] = 0;
  
          }
      }
  
      for(int i = 0;i&lt;n;i++){
          for(int j = 0;j&lt;m;j++){
              visited[i][j]=1;
              if(solver(board, n, m, i, j, i, j, 1, visited) == 1){
                  return 1;
              }
          }
      }
  
      return 0;
  
  }
  
  
  </code></pre>
  
  <p><strong>Input handling is done below and it cannot be altered</strong></p>
  
  <pre><code>#include&lt;bits/stdc++.h&gt;
  using namespace std;
  #define MAXN 51
  #include "solution.h"
  int main()
  {
      int N,M,i;
      char board[MAXN][MAXN];
      cin&gt;&gt;N&gt;&gt;M;
      for(i = 0;i &lt; N; i++){
          cin&gt;&gt;board[i];
      }
      cout&lt;&lt;solve(board,N,M)&lt;&lt;endl;
  }
  
  </code></pre>
  
  <p>Input:
  3 4
  AAAA
  ABCA
  AAAA</p>
  
  <p>Expected Output:
  1</p>
  
  <p>The output that I am getting:
  Runtime error</p>