class Solution {
public int numSpecial(int[][] mat) {
Map<Integer, Integer> cntRows = new HashMap<>();
Map<Integer, Integer> cntCols = new HashMap<>();
for (int i = 0; i < mat.length; i++) {
int sum = 0;
for (int num: mat[i]) sum += num;
if (sum > 0) cntRows.put(i, sum);
}
for (int j = 0; j < mat[0].length; j++) {
int sum = 0;
for (int i = 0; i < mat.length; i++) sum += mat[i][j];
if (sum > 0) cntCols.put(j, sum);
}
int ans = 0;
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
if (mat[i][j] == 1 && cntRows.get(i) == 1 && cntCols.get(j) == 1) ans++;
}
}
return ans;
}
}