classSolution: deffindDiagonalOrder(self, mat: List[List[int]]) -> List[int]: ifnot mat ornot mat[0]: return [] m, n = len(mat), len(mat[0]) ans = [] row, col = 0, 0 direction = 1 whilelen(ans) < m * n: ans.append(mat[row][col]) if direction == 1: new_row = row - 1 new_col = col + 1 else: new_row = row + 1 new_col = col - 1 is_out_of_bounds = new_row < 0or new_row >= m or new_col < 0or new_col >= n ifnot is_out_of_bounds: row, col = new_row, new_col else: if direction == 1: if col + 1 < n: col += 1 else: row += 1 else: if row + 1 < m: row += 1 else: col += 1 direction *= -1 return ans