<p>You are given two images, <code>img1</code> and <code>img2</code>, represented as binary, square matrices of size <code>n x n</code>. A binary matrix has only <code>0</code>s and <code>1</code>s as values.</p>

<p>We <strong>translate</strong> one image however we choose by sliding all the <code>1</code> bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the <strong>overlap</strong> by counting the number of positions that have a <code>1</code> in <strong>both</strong> images.</p>

<p>Note also that a translation does <strong>not</strong> include any kind of rotation. Any <code>1</code> bits that are translated outside of the matrix borders are erased.</p>

<p>Return <em>the largest possible overlap</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/overlap1.jpg" style="width: 450px; height: 231px;" />
<pre>
<strong>Input:</strong> img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We translate img1 to right by 1 unit and down by 1 unit.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/overlap_step1.jpg" style="width: 450px; height: 105px;" />
The number of positions that have a 1 in both images is 3 (shown in red).
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/overlap_step2.jpg" style="width: 450px; height: 231px;" />
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> img1 = [[1]], img2 = [[1]]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> img1 = [[0]], img2 = [[0]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == img1.length == img1[i].length</code></li>
	<li><code>n == img2.length == img2[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 30</code></li>
	<li><code>img1[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
	<li><code>img2[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
public:
    int largestOverlap(vector<vector<int>>& A, vector<vector<int>>& B) {
        // you need to loop through the different offsets. 
        // if anything goes through, you need to loop it back around
        // and set the ones on the other side to be zeros.
        vector<pair<int, int>> offsets;
        int n = A.size();
        int m = A[0].size();
        for(int i = -1 * n; i < n; i ++){
            for(int j = -1 * m; j < m; j ++){
                // cout << "j: " << j << "\n";
                offsets.push_back(make_pair(i, j));
            }
        }
        int res = 0;
        vector<vector<int>> Acopy(A.begin(), A.end());
        for(const auto& offset: offsets){
            for(int i = 0; i < A.size(); i ++){
                for(int j = 0; j < A[0].size(); j ++){
                    int x = i + offset.first;
                    int y = j + offset.second;
                    int x_orig = i+ offset.first;
                    int y_orig = j + offset.second;
                    if(x < 0){
                        x += A.size();
                    }else if(x >= A.size()){
                        x -= A.size();
                    }
                    if (y < 0){
                        y += A[0].size();
                    }else if(y >= A[0].size()){
                        y -= A[0].size();
                    }
                    if(isValid(x_orig, y_orig, A.size(), A[0].size())){
                        // shift it
                        Acopy[x][y] = A[i][j];
                    }else{
                        Acopy[x][y] = 0;
                    }
                   
                }
            }
            res = max(res, countOccurrences(B, Acopy));
        }
        return res;
    }
    bool isValid(int x, int y, int n, int m){
        if(x < 0 || y < 0 || x >= n || y >= m){
            return false;
        }
        return true;
    }
    int countOccurrences(vector<vector<int>>& orig, vector<vector<int>>& created){
        int count = 0;
        for(int i = 0; i < created.size(); i ++){
            for(int j = 0; j < created[0].size(); j ++){
                if(orig[i][j] == created[i][j] && orig[i][j] == 1){
                    ++ count;
                }
            }
        }
        return count;
    }
};
13100