Given an m x n binary matrix mat, return the number of special positions in mat.

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

&nbsp;
Example 1:

Input: mat = [[1,0,0],[0,0,1],[1,0,0]]
Output: 1
Explanation: (1, 2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.


Example 2:

Input: mat = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Explanation: (0, 0), (1, 1) and (2, 2) are special positions.


&nbsp;
Constraints:


	m == mat.length
	n == mat[i].length
	1 &lt;= m, n &lt;= 100
	mat[i][j] is either 0 or 1.


class Solution(object):
    def numSpecial(self, mat):
        """
        :type mat: List[List[int]]
        :rtype: int
        """
        r=len(mat)
        c=len(mat[0])
        
        r_c={}
        l_c={}
        
        for i in range(r):
            flag=0
            for j in range(c):
                if(mat[i][j]==1):
                    flag+=1
            r_c[i]=flag
        for i in range(c):
            flag=0
            for j in range(r):
                if(mat[j][i]==1):
                    flag+=1
            l_c[i]=flag
        ret=0
        for i in range(r):
            for j in range(c):
                if(mat[i][j]==1 and l_c[j]==1 and r_c[i]==1):
                    ret+=1
        return ret
class Solution {
    public int numSpecial(int[][] mat) {
      int count=0;
        for(int i=0;i<mat.length;i++){
            for(int j=0;j<mat[0].length;j++){
                if(mat[i][j]==1){
                    int flag=0;
                    for(int k=0;k<mat.length;k++){
                        if(mat[k][j]!=0 && k!=i){
                            flag=1;break;
                        }
                    }
                    if(flag==1) continue;
                    for(int k=0;k<mat[0].length;k++){
                        if(mat[i][k]!=0 && k!=j){
                            flag=1;
                            break;
                        }
                    }
                    if(flag==0) count++;
                }
            }
        }
        return count;
    }
}
class Solution {
public:
    int numSpecial(vector<vector<int>>& mat) {
        vector<vector<int>>v;
        map<int,vector<int>>m;

        for(int i=0;i<mat.size();i++){
            vector<int>temp = mat[i];

            for(int j=0;j<temp.size();j++){
                m[j].push_back(temp[j]);
            }
        }
        for(auto i:m){
            v.push_back(i.second);
        }
        int counter = 0;
        for(int i=0;i<mat.size();i++){
            int onecount = 0;
            int column = 0;
            for(int j=0;j<mat[i].size();j++){
                if(mat[i][j]==1){
                    column = j;
                    onecount++;
                }
            }
            if(onecount==1){
                int countone = 0;
                vector<int>temp = v[column];
                for(auto i:temp){
                    if(i==1){
                        countone++;
                    }
                }
                if(countone==1){
                    counter++;
                }
            }
        }
        return counter;
    }
};
/**
 * @param {number[][]} mat
 * @return {number}
 */
var numSpecial = function(mat) {
    let specialPostions = [];
    for(let i in mat){
        for(let j in mat[i]){
            if(mat[i][j] == 1 ){
                let horizontalOnes = 0;
                let verticalOnes = 0;
                    
                for(let k in mat[i]){
                    if(k != j &&  mat[i][k] == 1){
                        horizontalOnes++;
                    }
                }
                    
                for(let k = 0 ; k < mat.length ; k++ ){
                    if(k != i && mat[k][j] == 1){
                        verticalOnes++;
                    }
                }
                    
                if(horizontalOnes == 0 && verticalOnes == 0){
                    specialPostions.push([i,j]);
                }
                
            }
        }
    }
    
    return specialPostions.length;
    
};
Special Positions in a Binary Matrix