Skip to main content
LeetCode 531, Medium. Topics: Array, Hash Table, Matrix. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:

Problem

Given an m x n picture consisting of black 'B' and white 'W' pixels, return the number of black lonely pixels. A black lonely pixel is a character 'B' located at a specific position where the same row and same column don’t have any other black pixels.

Examples

Example 1

Constraints

  • m == picture.length
  • n == picture[i].length
  • 1 <= m, n <= 500
  • picture[i][j] is 'W' or 'B'.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026