It's my first time sharing a solution here. Quite nervous...

```
int minTotalDistance(vector<vector<int>>& grid) {
if(grid.empty()) return 0;
int NumOfRow = grid.size();
int NumOfCol = grid[0].size();
vector<int> rows;
vector<int> cols;
for(int indexOfRow = 0; indexOfRow < NumOfRow; indexOfRow++){
for(int indexOfCol = 0; indexOfCol < NumOfCol; indexOfCol++){
if(grid[indexOfRow][indexOfCol]){
rows.push_back(indexOfRow);
cols.push_back(indexOfCol);
}
}
}
// sort(rows.begin(), rows.end()); as this's already sorted
sort(cols.begin(), cols.end());
int idealRow = rows[rows.size() / 2];
int idealCol = cols[cols.size() / 2];
int result = 0;
for(auto row: rows)
result += abs(row - idealRow);
for(auto col: cols)
result += abs(col - idealCol);
return result;
}
```