class Solution:
def minFallingPathSum(self, matrix: List[List[int]]) -> int:
N = len(matrix)
for i in range(1, N):
for j in range(N):
if j == 0:
matrix[i][j] += min(matrix[i-1][j], matrix[i-1][j+1])
elif j == N-1:
matrix[i][j] += min(matrix[i-1][j], matrix[i-1][j-1])
else:
matrix[i][j] += min(matrix[i-1][j], matrix[i-1][j-1], matrix[i-1][j+1])
return min(matrix[N-1])