In Java the ArrayList can expand its size on demand, so I use two ArrayList to remember the row and column indices with 0. The space depends on how many 0s are in the matrix but should be less than m+n.

Is it considered as a constant space?

```
public class Solution {
public void setZeroes(int[][] matrix) {
ArrayList<Integer> rows = new ArrayList<>(); // row indices with 0
ArrayList<Integer> columns = new ArrayList<>(); // column indices with 0
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
if (!rows.contains(i)) rows.add(i); // do not add duplicate
if (!columns.contains(j)) columns.add(j);
}
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (rows.contains(i) || columns.contains(j)) {
matrix[i][j] = 0;
}
}
}
}
}
```