```
class Solution(object):
def minOneDimension(self, points):
minimum, index, n, left, total = (
float('inf'), None, len(points), 0, sum(points))
for i, p in enumerate(points, 1):
current = (p * (i - 1) - left) + (total - left - p * (n - i + 1))
minimum, index = (
current, p) if current < minimum else (minimum, index)
left += p
return index
def minTotalDistance(self, grid):
points = [
(i, j) for i, row in enumerate(grid)
for j, num in enumerate(row) if num]
meetingPoint = map(
self.minOneDimension,
[sorted(map(operator.itemgetter(i), points)) for i in xrange(2)])
return sum([
abs(meetingPoint[0] - p[0]) + abs(meetingPoint[1] - p[1])
for p in points])
```