Here is my code, cannot be compared to yours.

public class Solution { public int findBlackPixel(char[][] picture, int N) { int count = 0, R = picture.length, C = picture[0].length; int[] row = new int[R]; int[] col = new int[C]; HashMap<Integer,String> map = new HashMap<>(); for(int i = 0;i<R;i++){ StringBuilder sb = new StringBuilder(); for(int j = 0;j<C;j++){ sb.append(picture[i][j]); if(picture[i][j] == 'B'){ row[i]++; col[j]++; } } map.put(i,sb.toString()); } for(int i = 0;i<R;i++){ for(int j = 0;j<C;j++){ if(picture[i][j] == 'B'){ if(row[i] == N && col[j] == N){ int n = 0; for(int k = 0;k<R && n < N;k++){ if(picture[k][j] == 'B'){ if(!map.get(i).equals(map.get(k))) break; n++; } } if(n == N) count++; } } } } return count; } }Lonely Pixel II