Thanks for sharing.

I have a space-cost optimization.

Actually, we don't need to store a hard-copy of a row's values, we can just store the row's reference pointer.

Here is my code:

class Solution {
public int findBlackPixel(char[][] picture, int N) {
if(picture==null || picture.length==0 || picture[0].length==0)
return 0;
int m=picture.length;
int n=picture[0].length;
int[] cols = new int[n];
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
Map<Integer, char[]> rowMap = new HashMap<Integer, char[]>();
for(int i=0; i<m; i++){
int count=0; //count number of 'B' in current row
for(int j=0; j<n; j++)
if(picture[i][j]=='B'){
cols[j]++;
count++;
}
if(count==N){ //if current row has the desired number of 'B'
int hash = Arrays.hashCode( picture[i] );
countMap.put( hash, countMap.getOrDefault(hash, 0)+1 ); //put the reference of the current row, rather than the hard copy
rowMap.putIfAbsent(hash, picture[i]);
}
}
int result=0;
for(Map.Entry<Integer, Integer> entry : countMap.entrySet())
if( entry.getValue()==N ){ //if there are N rows, all of which are the same
char[] row = rowMap.get( entry.getKey() );
for(int j=0; j<n; j++)
if(row[j]=='B' && cols[j]==N) //current position is 'B', current col also has N 'B'
result += N;
}
return result;
}
}