<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 numMagicSquaresInside(vector<vector<int>>& grid) {
        int count = 0, r = 0;
        if (grid.size() < 3 || grid[0].size() < 3) return 0;

        while (r <= grid.size() - 3) {
            int c = 0;
            while (c <= grid[0].size() - 3) {
                vector<int> L;
                int s = 0, check = 0;
                for (int q = c; q < c + 3; q++) {
                    s += grid[r][q];
                }
                
                // Checking sum of rows ->
                for (int a = r; a < r + 3; a++) {
                    int rSum = 0;    
                    for (int b = c; b < c + 3; b++) {
                        rSum += grid[a][b];
                        L.push_back(grid[a][b]);
                    }
                    if (rSum != s) {
                        check++;
                        break;
                    }
                }
                
                // Checking if there are unique values in the grid ->
                int check1 = 0;
                for (int i = 0; i < L.size(); i++) {
                    for (int j = i + 1; j < L.size(); j++) {
                        if (L[i] == L[j] || L[i] == 0 || L[j] == 0 || L[i] > 9 || L[j] > 9){
                            check++;
                            check1++;
                            break;
                        }
                    }
                    if (check1 == 1) break;
                }

                // Checking sum of columns ->
                for(int b = c; b < c + 3; b++) {
                    int cSum = 0;    
                    for (int a = r; a < r + 3; a++) {
                        cSum += grid[a][b];
                    }
                    if (cSum != s) {
                        check++;
                        break;
                    }
                }

                // Checking sum of diagonals ->
                int d1 = grid[r][c] + grid[r + 1][c + 1] + grid[r + 2][c + 2];
                int d2 = grid[r][c + 2] + grid[r + 1][c + 1] + grid[r + 2][c];
                
                if (d1 != s || d2 != s) {
                    check++;
                }

                if (check == 0) {
                    count++;
                }
                c++;
            }
            r++;
        }
        return count;
    }
};
11200