Track/Calculate Valid Pixel 3*3 Region 
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```csharp []
public class Solution {
    public int[][] ResultGrid(int[][] image, int threshold) {
        int n = image.Length, m = image[0].Length;
        int[][] dir = [[0, 1], [1, 0], [0, -1], [-1, 0]];
        bool[,] valid = new bool[n,m];

        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                valid[i,j] = true;
            }
        }

        (int, int)[,] grid = new (int, int)[n, m];
          for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                grid[i,j] = (0, 0);
                if(j > 0 && Math.Abs(image[i][j-1] - image[i][j]) > threshold)
                {
                    valid[i,j] = false;
                }
            }
        }

        int[] dir2 = [-1, 0, 1];
        for(int i = 1; i < n-1; i++)
        {
            for(int j = 1; j < m-1; j++)
            {
                bool subValid = true;
                int sum = 0;
               
                for(int x = -1; x <= 1; x++)
                {
                    for(int y = -1 ; y <= 1; y++)
                    {
                        int newX = i+x, newY = j+y;
                        sum += image[newX][newY];
                        if(x > -1)
                        {
                            if(Math.Abs(image[newX-1][newY] - image[newX][newY]) > threshold)
                            {
                                subValid = false;
                                break;
                            }

                        }
                        
                        if(y > -1)
                        {
                            if(Math.Abs(image[newX][newY-1] - image[newX][newY]) > threshold)
                            {
                                subValid = false;
                                break;
                            }
                        }
                    }
                }

                if(subValid)
                {
                    sum /=9;
                    for(int x = 0; x < 3; x++)
                    {
                        for(int y = 0; y < 3; y++)
                        {
                            int newX= i + dir2[x], newY = j + dir2[y];
                            (int, int)cur = grid[newX,newY];
                            cur.Item1 += subValid ? sum : image[newX][newY];
                            cur.Item2 += 1;
                            grid[newX,newY] = cur;
                        }
                    }
                }
            }
        }

        int[][] res = new int[n][];
        for(int i = 0; i < n; i++)
        {
            res[i] = new int[m];
            for(int j = 0; j < m; j++)
            {
                res[i][j] = grid[i,j].Item2 > 0 ?  grid[i,j].Item1/grid[i,j].Item2 : image[i][j];
            }
        }

        return res;
    }
}
```
['C#']