class Solution(object):
    def numMagicSquaresInside(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        if len(grid)<3:
            return 0
        elif len(grid[0])<3:
            return 0
        ans=0
        for i2 in range(2,len(grid)):
            for j in range(2,len(grid[0])):
                a,b,c=grid[i2-2][j-2],grid[i2-2][j-1],grid[i2-2][j]
                d,e,f=grid[i2-1][j-2],grid[i2-1][j-1],grid[i2-1][j]
                g,h,i=grid[i2][j-2],grid[i2][j-1],grid[i2][j]
                if (a+b+c==d+e+f==g+h+i==a+d+g==b+e+h==c+f+i==a+e+i==c+e+g==15) and sorted([a,b,c,d,e,f,g,h,i])==[k for k in range(1,10)]:
                    ans+=1
        return ans


solution=Solution()
grid=[[4,3,8,4],
        [9,5,1,9],
        [2,7,6,2]]
print(solution.numMagicSquaresInside(grid))
