wrong ans!!

plz…help me
whats wrong in my code ??
which test cases i m missing??
heres the qus http://www.spoj.com/problems/HERDING/
and my sol

#include<stdio.h>

int ans;int p;
int visited[1005][1005]={{0}};
char maze[1005][1005];int pc[1005][1005]={{0}};
int dfs(int r,int c,int n,int m);
int main()
{
    int t,a,b,c,i,j,k,l,n,m;
    scanf("%d",&n);
    scanf("%d",&m);
 
    
   getchar();
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {scanf("%c",&maze[i][j]);}getchar();
    }
  ans=0;int cnt=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {if(visited[i][j]==0)dfs(i,j,n,m);}
    }
    
    printf("%d\n",ans);
  return 0;
}


int dfs(int r,int c,int n,int m)
{
   
     visited[r][c]=1;
    if((maze[r][c]=='S')&&(r+1<=n))
    {
        if(visited[r+1][c]==0)
    {dfs(r+1,c,n,m);}
    else
    {
        if((visited[r+1][c]==1)&&(pc[r+1][c]==0))
        ans++;
    }}    
    else if((maze[r][c]=='N')&&(r-1>=1))
    {
        if(visited[r-1][c]==0)
    {dfs(r-1,c,n,m);}
    else
    {
        if((visited[r-1][c]==1)&&(pc[r-1][c]==0))
        ans++;
    }} 
    else if((maze[r][c]=='E')&&(c+1<=m))
       {
        if(visited[r][c+1]==0)
    {dfs(r,c+1,n,m);}
    else
    {
        if((visited[r][c+1]==1)&&(pc[r][c+1]==0))
        ans++;
    }} 
    else if((maze[r][c]=='W')&&(c-1>=1))
       {
        if(visited[r][c-1]==0)
    {dfs(r,c-1,n,m);}
    else
    {
        if((visited[r][c-1]==1)&&(pc[r][c-1]==0))
        ans++;
    }} 
    pc[r][c]=1;
  
}
//