<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the number of special positions in </em><code>mat</code><em>.</em></p>

<p>A position <code>(i, j)</code> is called <strong>special</strong> if <code>mat[i][j] == 1</code> and all other elements in row <code>i</code> and column <code>j</code> are <code>0</code> (rows and columns are <strong>0-indexed</strong>).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/special1.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> mat = [[1,0,0],[0,0,1],[1,0,0]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/special-grid.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> mat = [[1,0,0],[0,1,0],[0,0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> (0, 0), (1, 1) and (2, 2) are special positions.
</pre>

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

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
	<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
public:
    bool finderRow(int row, int col, int cols, vector<vector<int>>& mat){
        for(int i = 0; i < cols; i++){
            if(i != col){
                if(mat[row][i] == 1)
                    return false;
            }
        }
        return true;
    }

    bool finderCol(int col, int row, int rows, vector<vector<int>>& mat){
        for(int i = 0; i < rows; i++){
            if(i != row){
                if(mat[i][col] == 1)
                    return false;
            }
        }
        return true;
    }

    int numSpecial(vector<vector<int>>& mat) {
        vector<pair<int,int>> pos;
        int rows = mat.size(), cols = mat[0].size(), cnt = 0;
        for(int i = 0; i < mat.size(); i++){
            for(int j = 0; j < mat[i].size(); j++){
                if(mat[i][j] == 1)
                    pos.push_back({i,j});
            }
        }
        bool f1, f2;
        for(int i = 0; i < pos.size(); i++){
            f1 = finderRow(pos[i].first, pos[i].second, cols, mat);
            f2 = finderCol(pos[i].second, pos[i].first, rows, mat);
            if(f1 && f2)
                cnt++;
        }
        return cnt;
    }
};
16300