1. Marks in the First Row and Column Optimal
Intuition
Every decision has to come from the ORIGINAL grid: zero a row and a column as the scan finds a 0, and the zeros just written are found later and spread again. So mark first and fill after. The marks need a place, and the grid has one: a 0 at (r, c) inside writes a 0 at the start of its row, (r, 0), and at the top of its column, (0, c). But the first row and the first column hold values of their own, and a mark written there would hide whether they held a 0 to begin with. So two flags are noted before any mark is written: does the first row hold a 0, does the first column. Then the marks, from the inner cells; then every inner cell whose row or column is marked is set to 0; and last, the first row and the first column, from the flags. The order is the point: filled first, a zeroed first row reads as a mark in every column. Mark the rows and columns that hold zeros, and after that, fill them with zeros.
Algorithm
1. Note two flags: whether the first row holds a 0, and whether the first column does.
2. Mark: for every inner cell (r and c from 1) that is 0, set matrix[r][0] and matrix[0][c] to 0.
3. Fill: for every inner cell, set it to 0 when matrix[r][0] or matrix[0][c] is 0.
4. Last, if the first row's flag is set, zero the first row; if the first column's is, zero the first column. Return nothing.
Time & Space
Time O(m x n): the grid is scanned a fixed number of times. Space O(1): two flags.