Why does my function return 0 even when it shouldn't? : I am trying to count and update the immediate neighbors of a cell with non zero values. So left, right, upper, lower, upper-left ... 
Anyway all the eight directions.

    int grid[100][100];

    int genNeibrs(int x, int y, int n, int m)
    {
        m--;
        n--;
        int c = 0, xs = ((x - 1) < 0) ? 0 : (x - 1), xe = ((x + 1) > n) ? n : (x + 1), ys = ((y - 1) < 0) ? 0 : (y - 1), ye = ((y + 1) > m) ? m : (y + 1);

        for (auto i = xs; i <= xe; i++)
        {
            for (auto j = ys; j <= ye; j++)
            {
                if (grid[i][j])
                    ++c;
            }
        }
        return c;
    }

    void getGrs(int n, int m) //m
    {
        for (auto i = 0; i < n; i++)
        {
            for (auto j = 0; j < m; j++)
                if (grid[i][j])
                {
                    int q = genNeibrs(i, j, m, n);
                    if (q)
                        grid[i][j] = q;
                }
        }
    }

Here the value of q becomes 0 for successive iterations other than 0,0 for
`grid =  {{1, 0, 1, 1, 0, 1, 1, 1, 1, 0 <repeats 91 times>}, {0, 0, 0, 1, 0, 1, 0, 0, 1, 0 <repeats 91 times>}, {0 <repeats 100 times>} <repeats 98 times>}`, m and n being 9 and 2 respectively.
While debugging with gdb the function `genNeibrs` returns as expected but over execution I managed to pin it down to the line where q is being initialized. It somehow gets initialized to zero even when it shouldn't.
So what am I doing wrong or is this a compiler problem?
Expected value of `grid = {{1, 0 ,2 ,2 ,0, 2, 3, 3, 2, 0 <repeats 91 times>},{0, 0, 0, 2, 0, 2, 0, 0, 2, 0 <repeats 91 times>}, {0 <repeats 100 times>} <repeats 98 times>}`

