in Rust this is just
pub fn count_squares(matrix: Vec>) -> i32 {
let mut table = vec![0; matrix[0].len() + 1];
let (mut prev, mut res) = (0, 0);

for i in 0..matrix.len() {
for j in 0..matrix[0].len() {
table[j + 1] = (matrix[i][j] == 1)
.then(|| replace(&mut prev, table[j + 1]).min(table[j].min(table[j + 1])) + 1)
.unwrap_or(0);

res += table[j + 1];
}
}

res
}