Please tell why my code is not passing one of the test cases

Your code fails sample testcase as well.
You are checking all 8 directions , you need to check only 4, not the diagonals.

still not giving correct output after changing to only adjacent
please ammend the code

Okay I don’t know what changes you have made in your code.
But here are some possible errors. First of all that a[n][m] array has to be of char type, because they are giving string input. Second reason can be that your array size is not sufficient, so try to make it dynamic.

This is how I have solved the problem. Problem was to simply check the number of connected components. So I do it with a help of a visit array and bfs.

I have tried to write a readable code. Please have a look https://ide.codingblocks.com/s/427197

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.