------
## 最终结论
```python
def largestOverlap(img1, img2):
    n = len(img1)
    max_overlap = 0
    
    # Function to calculate overlap for a given translation
    def calculate_overlap(x_offset, y_offset):
        overlap_count = 0
        for i in range(n):
            for j in range(n):
                if 0 <= i + x_offset < n and 0 <= j + y_offset < n:
                    overlap_count += img1[i][j] * img2[i + x_offset][j + y_offset]
        return overlap_count
    
    # Try all possible translations
    for x in range(-n + 1, n):
        for y in range(-n + 1, n):
            max_overlap = max(max_overlap, calculate_overlap(x, y))
    
    return max_overlap
```