A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.

Given a row x col&nbsp;grid&nbsp;of integers, how many 3 x 3 "magic square" subgrids are there?&nbsp; (Each subgrid is contiguous).

&nbsp;
Example 1:

Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
Output: 1
Explanation: 
The following subgrid is a 3 x 3 magic square:

while this one is not:

In total, there is only one magic square inside the given grid.


Example 2:

Input: grid = [[8]]
Output: 0


&nbsp;
Constraints:


	row == grid.length
	col == grid[i].length
	1 &lt;= row, col &lt;= 10
	0 &lt;= grid[i][j] &lt;= 15


class Solution:

    digits = {1, 2, 3, 4, 5, 6, 7, 8, 9}

    @classmethod
    def magic_3_3(cls, square: List[List[int]]) -> bool:
        if set(sum(square, [])) != Solution.digits:
            return False
        sum_row0 = sum(square[0])
        for r in range(1, 3):
            if sum(square[r]) != sum_row0:
                return False
        if any(sum(col) != sum_row0 for col in zip(*square)):
            return False
        sum_main_diagonal = sum_second_diagonal = 0
        for i in range(3):
            sum_main_diagonal += square[i][i]
            sum_second_diagonal += square[i][2 - i]
        return sum_main_diagonal == sum_second_diagonal == sum_row0

    def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
        count = 0
        rows, cols = len(grid), len(grid[0])
        for r in range(rows - 2):
            for c in range(cols - 2):
                if Solution.magic_3_3([grid[row_idx][c: c + 3]
                                       for row_idx in range(r, r + 3)]):
                    count += 1
        return count
class Solution {
	public int numMagicSquaresInside(int[][] grid) {
		int n=grid.length,m=grid[0].length,count=0;
		for(int i=0;i<n-2;i++)
		{
			for(int j=0;j<m-2;j++)
			{
				if(sum(i,j,grid))
					count++;
			}
		}
		return count;
	}
	public boolean sum(int x,int y,int[][] grid)
	{
		int sum=grid[x][y]+grid[x][y+1]+grid[x][y+2],sum1=0,sum2=0;
		int []count=new int[10];
		for(int i=0;i<3;i++)
		{
			sum1=0;
			sum2=0;   
			for(int j=0;j<3;j++)
			{
				sum1+=grid[x+i][y+j];
				sum2+=grid[x+j][y+i];
				if(grid[x+i][y+j]<1 ||grid[x+i][y+j]>9 ||count[grid[x+i][y+j]]!=0)
					return false;
				count[grid[x+i][y+j]]=1;

			}
			if(sum1!=sum || sum!=sum2 || sum1!=sum2)
				return false;
		}
		sum1=grid[x][y]+grid[x+1][y+1]+grid[x+2][y+2];
		sum2=grid[x][y+2]+grid[x+1][y+1]+grid[x+2][y];
		if(sum1!=sum2 || sum1!=sum)
			return false;
		return true;
	}
class Solution {
public:
    int numMagicSquaresInside(vector<vector<int>>& grid) {
        int result = 0;
        for(int i = 0; i < grid.size(); i++){
            for(int j = 0; j < grid[i].size() ; j++){
                if(isMagicSquare(grid, i, j)){
                    result++;
                }
            }
        }
        return result;
    }
    bool isMagicSquare(vector<vector<int>>& grid, int i, int j){
        if(i + 2 < grid.size() && j+2 < grid[i].size()){
            int col1 = grid[i][j] + grid[i+1][j] + grid[i+2][j];
            int col2 = grid[i][j+1] + grid[i+1][j+1] + grid[i+2][j+1];
            int col3 = grid[i][j+2] + grid[i+1][j+2] + grid[i+2][j+2];
            int row1 = grid[i][j] + grid[i][j+1] + grid[i][j+2];
            int row2 = grid[i+1][j] + grid[i+1][j+1] + grid[i+1][j+2];
            int row3 = grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2];
            int diag1 = grid[i][j] + grid[i+1][j+1] + grid[i+2][j+2];
            int diag2 = grid[i+2][j] + grid[i+1][j+1] + grid[i][j+2];
            if(
                (col1 == col2) &&
                (col1 == col3) &&
                (col1 == row1) &&
                (col1 == row2) &&
                (col1 == row3) &&
                (col1 == diag1) &&
                (col1 == diag2)) {
                    set<int> s({1,2,3,4,5,6,7,8,9});
                    for(int r = 0 ; r < 3 ; r++){
                        for(int c = 0; c < 3 ; c++){
                            s.erase(grid[i + r][j + c]);
                        }
                    }
                    return s.empty();
            }
        }
        return false;
    }
};
var numMagicSquaresInside = function(grid) {
    let res = 0;
    for(let i = 0; i < grid.length - 2; i++){
       for(let j = 0; j < grid[0].length - 2; j++){
           //only check 4
           if(grid[i][j]+grid[i][j+1]+grid[i][j+2]==15
           && grid[i][j]+grid[i+1][j]+grid[i+2][j]==15
           && grid[i][j]+grid[i+1][j+1]+grid[i+2][j+2]==15
           && grid[i+2][j]+grid[i+2][j+1]+grid[i+2][j+2]==15){
               let set = new Set();
           for(let a = i; a<=i+2; a++){
              for(let b = j; b<=j+2; b++){
                  if(grid[a][b]>=1&&grid[a][b]<=9) set.add(grid[a][b]);
           }}
           if(set.size===9) res++;
       }}}
    return res;
};
Magic Squares In Grid