http://www.codechef.com/COOK50/problems/GRID (Sherlock and the Grid) problem

My Approach :
step 1 -> Read the first row as a string and replace all the occurrences of ‘#’ with ‘0’ in that row.

(tmp[j] == '#') ? (tmp[j] = '0') : (tmp[j] = '1');

For remaining rows :
step 2 -> Read the next row and replace all the occurrences of ‘#’ with ‘0’ and for the ‘.’ :
Read the number in above cell (that is in the above row and same column) and increase this number by 1 and put the incremented value in the current cell .

 (tmp[j] == '#') ? (tmp[j] = '0') : (tmp[j] = grid[i-1][j] + 1);

Code for reading grid :

        cin >> tmp;
		
		for(j=0 ; j<n ;j++)
		{
			
			(tmp[j] == '#') ? (tmp[j] = '0') : (tmp[j] = '1');
		}
		
		grid.push_back(tmp);
		
		for(i=1;i<n;i++)
		{	
			cin >> tmp;
			
			for(j=0 ; j<n ;j++)
			{	
				(tmp[j] == '#') ? (tmp[j] = '0') : (tmp[j] = grid[i-1][j] + 1);
			}
			
			grid.push_back(tmp);
		}

Now i will start traversing the grid from first row and i will find the last occurence of ‘0’ in that row. Lets suppose that the last occurrence of ‘0’ is ‘index’ . Now i will start from index+1 and check each column upto column <= n .For each column i will calculate :

grid[n-1][j]-grid[i][j] == n-1-i   

If it is true then (i,j) is a valid position otherwise it is invalid position .
Here is my code for this :

    for(i=0 ; i<n ; i++)
	{
		index = grid[i].find_last_of('0');

		for(j = index+1 ; j<n ; j++)
		{
			if(n-1-i == grid[n-1][j]-grid[i][j])
			{
				count++;
			}
					
		}
	}

But i am getting wrong answer . If anyone one can find where i am doing wrong then please let me know ?