2373. Largest Local Values in a Matrix

https://leetcode.com/problems/largest-local-values-in-a-matrix/

class Solution:
    def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:

        # Traverse based on center 
        def traverse(x, y):
            cX = x + 1
            cY = y + 1

            maxVal = grid[cX][cY] 
            temp = [-1, 0, 1]

            for p in temp:
                for q in temp:
                    val = grid[cX + p][cY + q]
                    if val > maxVal:
                        maxVal = val

            return maxVal


        n = len(grid) - 2

        # list of len n for n times
        output = [[0] * n for i in range(n)]

        for x in range(n):
            for y in range(n):
                output[x][y] = traverse(x, y)      

        return output