Add some conditions to avoid unnecessary check.

class Solution { public int findLonelyPixel(char[][] picture) { int m = picture.length, n = picture[0].length; int[] row = new int[m]; int[] col = new int[n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (picture[i][j] == 'B') { row[i]++; col[j]++; } } } int res = 0; for (int i = 0; i < m; i++) { // Only if the current row has only 1 black, we will step into it. if (row[i] != 1) { continue; } for (int j = 0; j < n; j++) { // Since this row only has 1 black, after we met it, we can break and search next row. if (picture[i][j] == 'B') { if (col[j] == 1) { res++; } break; } } } return res; } }Lonely Pixel I