Code Room
CodingMediumcod-g033
Subject Grid dpLevel Mid–Senior~20 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given an m x n binary matrix (entries are 0 or 1), count how many square submatrices have all entries equal to 1. Squares of every side length count, and squares overlapping each other are counted separately. Return the total number of all-ones square submatrices. Dimensions are at least 1x1 and at most 300x300.

Implement
count_squares(matrix: list[list[int]]) → int
Examples
in[[[0,1,1,1],[1,1,1,1],[0,1,1,1]]]out15
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.