<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 m = grid.size();
        int n = grid[0].size();

        if (m < 3 || n < 3) {
            return 0;
        }

        int max_v = 10;
        vector<int> counter(max_v + 1, 0);
        int k = 0;
        int ans = 0;

        auto is_within = [] (int v) -> bool {
            return 0 < v && v < 10;
        };

        auto has_same_sum = [] (vector<vector<int>> &area, int x, int y) -> bool {

            int v = area[x][y] + area[x][y + 1] + area[x][y + 2];

            for (int i = x + 1; i < x + 3; i++) {

                if (accumulate(area[i].begin() + y, area[i].begin() + y + 3, 0) != v) {
                    return false;
                }
            }

            for (int j = 0; j < 3; j++) {

                if (area[x][y + j] + area[x + 1][y + j] + area[x + 2][y + j] != v) {
                    return false;
                }
            }

            return ((area[x][y] + area[x + 1][y + 1] + area[x + 2][y + 2] == v) && (area[x][y + 2] + area[x + 1][y + 1] + area[x + 2][y] == v));
        };

        int kind = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (is_within(grid[i][j]) && ++counter[grid[i][j]] == 1) {
                    ++kind;
                }
            }
        }

        if (kind == 9 && has_same_sum(grid, 0, 0)) {
            ++ans;
        }

        for (int i = 0; i < m - 2; i++) {

            if (i & 1) {
                for (int j = n - 1; n - 4 < j; j--) {
                    if (is_within(grid[i - 1][j]) && --counter[grid[i - 1][j]] == 0) {
                        --kind;
                    }
                }

                for (int j = n - 1; n - 4 < j; j--) {
                    if (is_within(grid[i + 2][j]) && ++counter[grid[i + 2][j]] == 1) {
                        ++kind;
                    }
                }

                if (kind == 9 && has_same_sum(grid, i,  n - 3)) {
                    ++ans;
                }

                for (int j = n - 4; -1 < j; j--) {
                    for (int k = 0; k < 3; k++) {
                        if (is_within(grid[i + k][j + 3]) && --counter[grid[i + k][j + 3]] == 0) {
                            --kind;
                        }
                    }

                    for (int k = 0; k < 3; k++) {
                        if (is_within(grid[i + k][j]) && ++counter[grid[i + k][j]] == 1) {
                            ++kind;
                        }
                    }

                    if (kind == 9 && has_same_sum(grid, i, j)) {
                        ++ans;
                    }
                }

            } else {
                if (0 < i) {
                    for (int j = 0; j < 3; j++) {
                        if (is_within(grid[i - 1][j]) && --counter[grid[i - 1][j]] == 0) {
                            --kind;
                        }
                    }

                    for (int j = 0; j < 3; j++) {
                        if (is_within(grid[i + 2][j]) && ++counter[grid[i + 2][j]] == 1) {
                            ++kind;
                        }
                    }

                    if (kind == 9 && has_same_sum(grid, i, 0)) {
                        ++ans;
                    }
                }

                for (int j = 1; j < n - 2; j++) {
                    for (int k = 0; k < 3; k++) {
                        if (is_within(grid[i + k][j - 1]) && --counter[grid[i + k][j - 1]] == 0) {
                            --kind;
                        }
                    }

                    for (int k = 0; k < 3; k++) {
                        if (is_within(grid[i + k][j + 2]) && ++counter[grid[i + k][j + 2]] == 1) {
                            ++kind;
                        }
                    }

                    if (kind == 9 && has_same_sum(grid, i, j)) {
                        ++ans;
                    }
                }

            }
        }

        return ans;
    }
};

10300