def bestSquares(m, k):
    curMax = 0
    curNums = set()
    for x in range(0, len(m) - k):
        for y in range(0, len(m[0]) - k):
            curSum = sumSquare(m, x, y, k)
            if curSum >= curMax:
                if curSum == curMax:
                    curMax = curSum
                    curNums = set()
                addUniqNum(curNums, m, x, y, k)
    return sum(curNums)


 
def sumSquare(m, i, j, k):
    total = 0
    for x in range(i, i+k):
        for y in range(j, j+k):
            total += m[x][y]
    return total

def addUniqNum(curNums, m, i, j, k):
    for x in range(i, i+k):
         for y in range(j, j+k):
             curNums.add(m[x][y])
    return
    