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

<p>Given a <code>row x col</code> <code>grid</code> of integers, how many <code>3 x 3</code> magic square subgrids are there?</p>

<p>Note: while a magic square can only contain numbers from 1 to 9, <code>grid</code> may contain numbers up to 15.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/11/magic_main.jpg" style="width: 322px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
<strong>Output:</strong> 1
<strong>Explanation: </strong>
The following subgrid is a 3 x 3 magic square:
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/11/magic_valid.jpg" style="width: 242px; height: 242px;" />
while this one is not:
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/11/magic_invalid.jpg" style="width: 242px; height: 242px;" />
In total, there is only one magic square inside the given grid.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[8]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>row == grid.length</code></li>
	<li><code>col == grid[i].length</code></li>
	<li><code>1 &lt;= row, col &lt;= 10</code></li>
	<li><code>0 &lt;= grid[i][j] &lt;= 15</code></li>
</ul>

class Solution {
public:
    int magic(vector<vector<int>>& grid, int r, int c) {

        set<int>values;

        // checking all values are between 1 and 9 and are distinct;
        for(int r2 = r; r2 < r + 3; r2++)
        {
            for(int c2 = c; c2 < c + 3; c2++)
            {
                if(values.find(grid[r2][c2]) != values.end() || grid[r2][c2] < 1 || grid[r2][c2] > 9)
                    return 0;
                values.insert(grid[r2][c2]);
            }
        }
        // checking all rows are sum to 15
        
        for(int r2 = r; r2 < r + 3; r2++)
        {
            int sum = grid[r2][c] + grid[r2][c + 1] + grid[r2][c + 2];
            if(sum != 15)
                return 0;
        }
        
        // checking all cols are sum to 15
        
        for(int c2 = c; c2 < c + 3; c2++)
        {
            int sum = grid[r][c2] + grid[r + 1][c2] + grid[r + 2][c2];
            if(sum != 15)
                return 0;
        }

        // checking diagonals are sum to 15;

        if(grid[r][c] + grid[r + 1][c + 1] + grid[r + 2][c + 2] != 15)
            return 0;
        if(grid[r][c + 2] + grid[r + 1][c + 1] + grid[r + 2][c] != 15)
            return 0;
        return 1;
    }
    int magic2(vector<vector<int>>& grid, int r, int c) {

        if(grid[r][c] != 5)
            return 0;
        
        string pattern1 = "2761834927618349";
        string pattern2 = "9438167294381672";

        vector<int>neighbours;

        neighbours.push_back(grid[r - 1][c - 1]);
        neighbours.push_back(grid[r - 1][c]);
        neighbours.push_back(grid[r - 1][c + 1]);
        neighbours.push_back(grid[r][c + 1]);
        neighbours.push_back(grid[r + 1][c + 1]);
        neighbours.push_back(grid[r + 1][c]);
        neighbours.push_back(grid[r + 1][c - 1]);
        neighbours.push_back(grid[r][c - 1]);

        string seq = "";

        for(int i = 0; i < 8; i++)
            seq += (neighbours[i] + 48);
        cout << seq << endl;
        
        for(int i = 0; i < pattern1.size() - 8; i++)
        {
            string cur_seq = "";
            for(int j = 0; j < 8; j++)
                cur_seq += pattern1[i + j];
            
            if(seq == cur_seq)
                return 1;
        }
        for(int i = 0; i < pattern2.size() - 8; i++)
        {
            string cur_seq = "";
            for(int j = 0; j < 8; j++)
                cur_seq += pattern2[i + j];
            
            if(seq == cur_seq)
                return 1;
        }
        return 0;
    }
    int numMagicSquaresInside(vector<vector<int>>& grid) {

        // solution 1 

        int rows = grid.size();
        int cols = grid[0].size();

        int res = 0;

        for(int r = 0; r < rows - 2; r++)
        {
            for(int c = 0; c < cols - 2; c++)
            {
                res += magic(grid, r, c);
            }
        }
        return res;
        

        /* solution 2 

        int rows = grid.size();
        int cols = grid[0].size();

        int res = 0;

        for(int r = 1; r < rows - 1; r++)
        {
            for(int c = 1; c < cols - 1; c++)
            {
                res += magic2(grid, r, c);
            }
        }
        return res;
        */
    }
};
11900