public class Solution {
public int findLonelyPixel(char[][] picture) {
if (picture == null || picture.length == 0) {
return 0;
}
int m = picture.length, n = picture[0].length;
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int lonelyPixel = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (picture[i][j] == 'B') {
int count = 0;
for (int[] direction : directions) {
if (!findLonelyPixelByDFS(i, j, picture, direction)) {
break;
}
else {
++count;
}
}
if (count == 4) {
++lonelyPixel;
}
}
}
}
return lonelyPixel;
}
public boolean findLonelyPixelByDFS(int row, int col, char[][] picture, int[] direction) {
row += direction[0];
col += direction[1];
if (row < 0 || row >= picture.length || col < 0 || col >= picture[0].length) {
return true;
}
if (picture[row][col] == 'B') {
return false;
}
return findLonelyPixelByDFS(row, col, picture, direction);
}
}