------
## 最终结论
```python
def min_toggles_to_compress_image(n, m, image):
    # Function to count the number of toggles needed for a k x k block to be uniform
    def count_toggles(block):
        count_0 = sum(1 for row in block for pixel in row if pixel == '0')
        count_1 = sum(1 for row in block for pixel in row if pixel == '1')
        return min(count_0, count_1)
    
    # Padding the image with zeros
    def pad_image(image, n, m, k):
        padded_n = (n + k - 1) // k * k
        padded_m = (m + k - 1) // k * k
        padded_image = [row + '0' * (padded_m - m) for row in image]
        padded_image += ['0' * padded_m] * (padded_n - n)
        return padded_image
    
    min_toggles = float('inf')
    
    # Check all possible values of k
    for k in range(2, min(n, m) + 1):
        padded_image = pad_image(image, n, m, k)
        
        total_toggles = 0
        
        # Iterate over each k x k block
        for i in range(0, len(padded_image), k):
            for j in range(0, len(padded_image[0]), k):
                block = [row[j:j+k] for row in padded_image[i:i+k]]
                total_toggles += count_toggles(block)
        
        min_toggles = min(min_toggles, total_toggles)
    
    return min_toggles

# Input reading
n, m = map(int, input().split())
image = [input().strip() for _ in range(n)]

# Output the result
print(min_toggles_to_compress_image(n, m, image))
```