https://leetcode.com/problems/largest-magic-square/


```python
class Solution:
    def largestMagicSquare(self, grid: List[List[int]]) -> int:
        #8:48
        grid_width = len(grid[0])
        grid_height = len(grid)
        
        def does_it_equal(a_matrix):
            #print(a_matrix)
            value = sum(a_matrix[0])
            for row in a_matrix:
                if value != sum(row):
                    return False
            for index,_ in enumerate(a_matrix):
                if value != sum([row[index] for row in a_matrix]):
                    return False
            return True
        
        def get_a_new_matrix(y, x, side):
            new_grid = []
            y_target = y + side
            x_target = x + side
            for the_y, row in enumerate(grid):
                if the_y >= y_target:
                    continue
                if the_y >= y:
                    new_grid.append(row[x:x_target])
            #print(new_grid)
            return new_grid
        
        self.max_side = 0
        def trying(start_y, start_x, side_length):
            if ((start_y + side_length) > (grid_height)) or ((start_x + side_length) > (grid_width)):
                # end
                return
            new_matrix = get_a_new_matrix(start_y, start_x, side_length)
            ok = does_it_equal(new_matrix)
            #print((start_y, start_x), side_length, ok)
            if (ok):
                if side_length > self.max_side:
                    print((start_y, start_x), side_length, ok, new_matrix)
                    self.max_side = side_length
            trying(start_y, start_x, side_length+1)
                
        for y, row in enumerate(grid):
            for x, value in enumerate(row):
                trying(y,x,1)
                
        return self.max_side
        #9:17
        #debug until 9:28
        #found that the test set has a bug: the following should get output of 2, but the serve expect 1
        #[[1,17,15,17,5,16,8,9],[1,19,11,18,8,18,3,18],[6,6,5,8,3,15,6,11],[19,5,6,11,9,2,14,13],[12,16,16,15,14,18,10,7],[3,11,15,15,7,1,9,8],[15,5,11,17,18,20,14,17],[13,17,7,20,12,2,13,19]]
```

___

https://leetcode.com/problems/largest-magic-square/

Change a place, change a mood.
