> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Lonely Pixel I Python Solution with Tests

> Tested Python solution for LeetCode 531 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 531, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/lonely-pixel-i/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 531   # by problem number
lcpy gen -s lonely_pixel_i   # by problem name
```

## 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](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0531.Lonely%20Pixel%20I/images/pixel1.jpg)

```
Input: picture = [["W","W","B"],["W","B","W"],["B","W","W"]]
Output: 3
Explanation: All the three 'B's are black lonely pixels.
```

```
Input: picture = [["B","B","B"],["B","B","W"],["B","B","B"]]
Output: 0
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/lonely_pixel_i/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/lonely_pixel_i/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(m + n)
    def find_lonely_pixel(self, picture: list[list[str]]) -> int:
        if not picture:
            return 0
        m, n = len(picture), len(picture[0])
        row_counts = [row.count("B") for row in picture]
        col_counts = [sum(1 for r in range(m) if picture[r][c] == "B") for c in range(n)]
        return sum(
            1
            for r in range(m)
            for c in range(n)
            if picture[r][c] == "B" and row_counts[r] == 1 and col_counts[c] == 1
        )
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(m + n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.