C++ solution. O(1) space, O(n * m) time complexity


#1

#define ROWCOL_PROCESSED -3
#define ROW_PROCESSED -1
#define COL_PROCESSED -2
#define NO_PROCESSING 0


void process_row(vector<int>& row) {
    for (auto i = 0; i < row.size(); i++) {
        if (row[i] >= 1) {
            row[i]++;
        } else if (row[i] == NO_PROCESSING) {
            row[i] = ROW_PROCESSED;
        } else if (row[i] == COL_PROCESSED) {
            row[i] = ROWCOL_PROCESSED;
        }
    }
}

void process_column(vector<vector<int>>& matrix, size_t column) {
    for (auto i = 0; i < matrix.size(); i++) {
        if (matrix[i][column] >= 1) {
            matrix[i][column]++;
        } else if (matrix[i][column] == NO_PROCESSING) {
            matrix[i][column] = COL_PROCESSED;
        } else if (matrix[i][column] == ROW_PROCESSED) {
            matrix[i][column] = ROWCOL_PROCESSED;
        } 
    }
}


void Solution::setZeroes(vector<vector<int> > &A) {
    if (A.size() == 0 || A[0].size() == 0) {
        return;
    }
    
    for (auto i = 0; i < A.size(); i++) {
        for (auto j = 0; j < A[0].size(); j++) {
            if (A[i][j] == NO_PROCESSING) {
                process_row(A[i]);
                process_column(A, j);
                A[i][j] = ROWCOL_PROCESSED;
                
            } else if (A[i][j] == ROW_PROCESSED) {
                process_column(A, j);
                A[i][j] = ROWCOL_PROCESSED;
                
            } else if (A[i][j] == COL_PROCESSED) {
                process_row(A[i]);
                A[i][j] = ROWCOL_PROCESSED;
            }
        }
    }
    
    for (auto i = 0; i < A.size(); i++) {
        for (auto j = 0; j < A[0].size(); j++) {
            if (A[i][j] != 1)
                A[i][j] = 0;
        }
    }
}