class Solution { fun product( xs: Sequence, ys: Sequence, ): Sequence> = xs.flatMap { x -> ys.map { y -> x to y } } fun product( xs: Iterable, ys: Iterable, ): Sequence> = product(xs.asSequence(), ys.asSequence()) fun countSquares(matrix: Array): Int { val (rows, columns) = matrix.size to matrix[0].size val dp = Array(rows + 1) { IntArray(columns + 1) } var answer = 0 for ((y, x) in product(0.. matrix[y][x] == 1 }) { dp[y + 1][x + 1] = 1 + listOf(dp[y][x + 1], dp[y + 1][x], dp[y][x]).min() answer += dp[y + 1][x + 1] } return answer } }