All the Squares — Solution & Editorial
Size-i squares anchor at (N−i+1)(M−i+1) positions; sum over sizes (a closed form exists via square-sum identities, but the O(min) loop is instant).
Complexity: O(min(N, M))
Size-i squares anchor at (N−i+1)(M−i+1) positions; sum over sizes (a closed form exists via square-sum identities, but the O(min) loop is instant).
Complexity: O(min(N, M))