Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
           {5, 6, 7, 8},
           {9, 10, 11, 12},
           {13, 14, 15,16}}
Output: 
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4  
matrix[][] = {{1, 2, 3, 4},
           {5, 6, 7, 8},
           {9, 10, 11, 12}}
Output: 
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above, 
output for the 2nd testcase will be 
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix. 
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Matrices

Data structures

Starter code: #User function Template for python3

class Solution:
    
    #Function to return a list of integers denoting spiral traversal of matrix.
    def spirallyTraverse(self,matrix, r, c): 
        # code here 

Solutions:
class Solution:

	def spirallyTraverse(self, matrix, r, c):
		res = []
		left = 0
		right = len(matrix[0])
		top = 0
		bottom = len(matrix)
		while left < right and top < bottom:
			for i in range(left, right):
				res.append(matrix[top][i])
			top += 1
			for i in range(top, bottom):
				res.append(matrix[i][right - 1])
			right -= 1
			if not (left < right and top < bottom):
				break
			for i in range(right - 1, left - 1, -1):
				res.append(matrix[bottom - 1][i])
			bottom -= 1
			for i in range(bottom - 1, top - 1, -1):
				res.append(matrix[i][left])
			left += 1
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(x1, y1) = (0, 0)
		(x2, y2) = (r - 1, c - 1)
		ans = []
		(i, j) = (0, 0)
		p = 0
		while p < r * c:
			if i == x1 and j != y2:
				ans.append(matrix[i][j])
				j += 1
			elif j == y2 and i != x2:
				ans.append(matrix[i][j])
				i += 1
			elif i == x2 and j != y1:
				ans.append(matrix[i][j])
				j -= 1
			elif j == y1 and i != x1:
				ans.append(matrix[i][j])
				i -= 1
				if i == x1:
					x1 += 1
					y1 += 1
					x2 -= 1
					y2 -= 1
					(i, j) = (x1, y1)
					if x1 == x2 and y1 == y2:
						ans.append(matrix[i][j])
			p += 1
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		if not matrix:
			return []
		(rows, cols) = (len(matrix), len(matrix[0]))
		(top_row, bottom_row) = (0, rows - 1)
		(left_col, right_col) = (0, cols - 1)
		result = []
		while top_row <= bottom_row and left_col <= right_col:
			for col in range(left_col, right_col + 1):
				result.append(matrix[top_row][col])
			top_row += 1
			for row in range(top_row, bottom_row + 1):
				result.append(matrix[row][right_col])
			right_col -= 1
			if top_row <= bottom_row:
				for col in range(right_col, left_col - 1, -1):
					result.append(matrix[bottom_row][col])
				bottom_row -= 1
			if left_col <= right_col:
				for row in range(bottom_row, top_row - 1, -1):
					result.append(matrix[row][left_col])
				left_col += 1
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		left = 0
		right = c - 1
		top = 0
		bottom = r - 1
		ans = []
		while left <= right and top <= bottom:
			for i in range(left, right + 1):
				ans.append(matrix[top][i])
			top += 1
			for i in range(top, bottom + 1):
				ans.append(matrix[i][right])
			right -= 1
			if top <= bottom:
				for i in range(right, left - 1, -1):
					ans.append(matrix[bottom][i])
				bottom -= 1
			if left <= right:
				for i in range(bottom, top - 1, -1):
					ans.append(matrix[i][left])
				left += 1
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):

		def spirallyTraverseUtil(matrix, out, startr, endr, startc, endc):
			if startr > endr or startc > endc:
				return
			if endr == startr:
				for i in range(startc, endc + 1):
					out.append(matrix[endr][i])
				return
			if endc == startc:
				for i in range(startr, endr + 1):
					out.append(matrix[i][endc])
				return
			if endr - startr == 1:
				for i in range(startc, endc + 1):
					out.append(matrix[startr][i])
				for i in range(endc, startc - 1, -1):
					out.append(matrix[endr][i])
				return
			for i in range(startc, endc + 1):
				out.append(matrix[startr][i])
			for i in range(startr + 1, endr + 1):
				out.append(matrix[i][endc])
			for i in range(endc - 1, startc - 1, -1):
				out.append(matrix[endr][i])
			for i in range(endr - 1, startr, -1):
				out.append(matrix[i][startc])
			spirallyTraverseUtil(matrix, out, startr + 1, endr - 1, startc + 1, endc - 1)
		out = []
		try:
			spirallyTraverseUtil(matrix, out, 0, r - 1, 0, c - 1)
		except Exception as e:
			print(e)
		return out


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		n = len(matrix)
		m = len(matrix[0])
		top = 0
		bottom = n - 1
		left = 0
		right = m - 1
		result = []
		while left <= right and top <= bottom:
			for i in range(left, right + 1):
				result.append(matrix[top][i])
			top += 1
			for i in range(top, bottom + 1):
				result.append(matrix[i][right])
			right -= 1
			if top <= bottom:
				for i in range(right, left - 1, -1):
					result.append(matrix[bottom][i])
				bottom -= 1
			if left <= right:
				for i in range(bottom, top - 1, -1):
					result.append(matrix[i][left])
				left += 1
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		if matrix is None:
			return []
		(t, b, l, rt) = (0, r - 1, 0, c - 1)
		res = []
		while t <= b and l <= rt:
			for i in range(l, rt + 1):
				res.append(matrix[t][i])
			t += 1
			for j in range(t, b + 1):
				res.append(matrix[j][rt])
			rt -= 1
			if t <= b:
				for i in range(rt, l - 1, -1):
					res.append(matrix[b][i])
				b -= 1
			if l <= rt:
				for j in range(b, t - 1, -1):
					res.append(matrix[j][l])
				l += 1
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		ans = []
		if len(matrix) == 0:
			return ans
		m = len(matrix)
		n = len(matrix[0])
		seen = [[0 for i in range(n)] for j in range(m)]
		dr = [0, 1, 0, -1]
		dc = [1, 0, -1, 0]
		x = 0
		y = 0
		di = 0
		for i in range(m * n):
			ans.append(matrix[x][y])
			seen[x][y] = True
			cr = x + dr[di]
			cc = y + dc[di]
			if 0 <= cr and cr < m and (0 <= cc) and (cc < n) and (not seen[cr][cc]):
				x = cr
				y = cc
			else:
				di = (di + 1) % 4
				x += dr[di]
				y += dc[di]
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		res = []
		while matrix:
			res += matrix.pop(0)
			matrix = list(zip(*matrix))[::-1]
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		result = []
		top = 0
		bottom = r - 1
		left = 0
		right = c - 1
		while True:
			for i in range(left, right + 1):
				result.append(matrix[top][i])
			top += 1
			if top > bottom or left > right:
				break
			for i in range(top, bottom + 1):
				result.append(matrix[i][right])
			right -= 1
			if top > bottom or left > right:
				break
			for i in range(right, left - 1, -1):
				result.append(matrix[bottom][i])
			bottom -= 1
			if top > bottom or left > right:
				break
			for i in range(bottom, top - 1, -1):
				result.append(matrix[i][left])
			left += 1
			if top > bottom or left > right:
				break
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		k = 0
		l = 0
		ans = []
		while k < r and l < c:
			for i in range(l, c):
				ans.append(matrix[k][i])
			k += 1
			for i in range(k, r):
				ans.append(matrix[i][c - 1])
			c -= 1
			if k < r:
				for i in range(c - 1, l - 1, -1):
					ans.append(matrix[r - 1][i])
				r -= 1
			if l < c:
				for i in range(r - 1, k - 1, -1):
					ans.append(matrix[i][l])
				l += 1
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		n = r * c
		d = 1
		p = 0 + 0j
		(x0, x1) = (0, c - 1)
		(y0, y1) = (0, r - 1)
		ans = []
		while n > 0:
			(y, x) = (int(p.imag), int(p.real))
			ans.append(matrix[y][x])
			if d == 1 + 0j and x == x1:
				y0 += 1
				d *= 1j
			if d == 1j and y == y1:
				x1 -= 1
				d *= 1j
			if d == -1 + 0j and x == x0:
				y1 -= 1
				d *= 1j
			if d == -1j and y == y0:
				x0 += 1
				d *= 1j
			p += d
			n -= 1
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(top, bottom, left, right) = (0, r - 1, 0, c - 1)
		direction = 0
		result = []
		while top <= bottom and left <= right:
			if direction == 0:
				for i in range(left, right + 1):
					result.append(matrix[top][i])
				top += 1
			elif direction == 1:
				for i in range(top, bottom + 1):
					result.append(matrix[i][right])
				right -= 1
			elif direction == 2:
				for i in range(right, left - 1, -1):
					result.append(matrix[bottom][i])
				bottom -= 1
			elif direction == 3:
				for i in range(bottom, top - 1, -1):
					result.append(matrix[i][left])
				left += 1
			direction = (direction + 1) % 4
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		current_row = 0
		current_column = -1
		lst = []
		while r > 0 and c > 0:
			count = 0
			while count < c:
				current_column += 1
				count += 1
				lst.append(matrix[current_row][current_column])
			count = 1
			row_changed = False
			while count < r:
				current_row += 1
				count += 1
				lst.append(matrix[current_row][current_column])
				row_changed = True
			count = 1
			column_changed = False
			while row_changed and count < c:
				current_column -= 1
				count += 1
				lst.append(matrix[current_row][current_column])
				column_changed = True
			count = 2
			while column_changed and count < r:
				current_row -= 1
				count += 1
				lst.append(matrix[current_row][current_column])
			r = r - 2
			c = c - 2
		return lst


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		d = []
		t = 0
		l = 0
		ri = c - 1
		b = r - 1
		while l <= ri and t <= b:
			for i in range(l, ri + 1):
				d.append(matrix[t][i])
			t += 1
			for i in range(t, b + 1):
				d.append(matrix[i][ri])
			ri -= 1
			if t <= b:
				for i in range(ri, l - 1, -1):
					d.append(matrix[b][i])
			b -= 1
			if l <= ri:
				for i in range(b, t - 1, -1):
					d.append(matrix[i][l])
			l += 1
		return d


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(rows, cols) = (len(matrix), len(matrix[0]))
		(startRow, endRow) = (0, rows - 1)
		(startCol, endCol) = (0, cols - 1)
		result = []
		while startRow <= endRow and startCol <= endCol:
			for i in range(startCol, endCol + 1):
				result.append(matrix[startRow][i])
			for i in range(startRow + 1, endRow + 1):
				result.append(matrix[i][endCol])
			if startRow != endRow:
				for i in range(endCol - 1, startCol - 1, -1):
					result.append(matrix[endRow][i])
			if startCol != endCol:
				for i in range(endRow - 1, startRow, -1):
					result.append(matrix[i][startCol])
			startRow += 1
			endRow -= 1
			startCol += 1
			endCol -= 1
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		res = []
		if len(matrix) == 0:
			return []
		m = len(matrix)
		di = 0
		row = [0, 1, 0, -1]
		col = [1, 0, -1, 0]
		x = 0
		y = 0
		seen = [[0 for i in range(c)] for j in range(m)]
		for i in range(m * c):
			res.append(matrix[x][y])
			seen[x][y] = True
			cr = x + row[di]
			cc = y + col[di]
			if 0 <= cr and cr < m and (0 <= cc) and (cc < c) and (not seen[cr][cc]):
				x = cr
				y = cc
			else:
				di = (di + 1) % 4
				x = x + row[di]
				y = y + col[di]
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		l = []
		startc = 0
		startr = 0
		endc = c - 1
		endr = r - 1
		while startr <= endr and startc <= endc:
			for i in range(startc, endc):
				l.append(matrix[startr][i])
			for i in range(startr, endr):
				l.append(matrix[i][endc])
			for i in range(endc, startc, -1):
				l.append(matrix[endr][i])
			for i in range(endr, startr, -1):
				l.append(matrix[i][startc])
			if startr == endr and startc == endc and (startc == startr):
				l.append(matrix[startr][endr])
			startc += 1
			startr += 1
			endc -= 1
			endr -= 1
		return l[0:r * c]


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(m, n) = (len(matrix), len(matrix[0]))
		(row, col) = (0, 0)
		result = []
		while row < m and col < n:
			for i in range(col, n):
				result.append(matrix[row][i])
			row += 1
			for i in range(row, m):
				result.append(matrix[i][n - 1])
			n -= 1
			if row < m:
				for i in range(n - 1, col - 1, -1):
					result.append(matrix[m - 1][i])
				m -= 1
			if col < n:
				for i in range(m - 1, row - 1, -1):
					result.append(matrix[i][col])
				col += 1
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		ans = []
		(rowStart, rowEnd) = (0, r - 1)
		(columnStart, columnEnd) = (0, c - 1)
		while rowStart <= rowEnd and columnStart <= columnEnd:
			for i in range(columnStart, columnEnd + 1):
				ans.append(matrix[rowStart][i])
			for j in range(rowStart + 1, rowEnd + 1):
				ans.append(matrix[j][columnEnd])
			if rowStart < rowEnd and columnStart < columnEnd:
				for i in range(columnEnd - 1, columnStart, -1):
					ans.append(matrix[rowEnd][i])
				for j in range(rowEnd, rowStart, -1):
					ans.append(matrix[j][columnStart])
			rowStart += 1
			rowEnd -= 1
			columnStart += 1
			columnEnd -= 1
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		top = 0
		down = r
		left = 0
		right = c
		d = 0
		l = []
		while top < down and left < right:
			if d == 0:
				for i in range(left, right):
					l.append(matrix[top][i])
				top += 1
			if d == 1:
				for i in range(top, down):
					l.append(matrix[i][right - 1])
				right -= 1
			if d == 2:
				for i in range(right - 1, left - 1, -1):
					l.append(matrix[down - 1][i])
				down -= 1
			if d == 3:
				for i in range(down - 1, top - 1, -1):
					l.append(matrix[i][left])
				left += 1
			d = (d + 1) % 4
		return l


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		row = 0
		col = 0
		output = []
		while row < r and col < c:
			for i in range(col, c):
				output.append(matrix[row][i])
			row += 1
			for i in range(row, r):
				output.append(matrix[i][c - 1])
			c -= 1
			if row < r:
				for i in range(c - 1, col - 1, -1):
					output.append(matrix[r - 1][i])
				r -= 1
			if col < c:
				for i in range(r - 1, row - 1, -1):
					output.append(matrix[i][col])
				col += 1
		return output


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		start_row = 0
		start_col = 0
		end_row = r - 1
		end_col = c - 1
		ans = []
		while len(ans) < r * c and start_row <= end_row and (start_col <= end_col):
			for i in range(start_col, end_col + 1):
				ans.append(matrix[start_row][i])
			start_row += 1
			for i in range(start_row, end_row + 1):
				ans.append(matrix[i][end_col])
			end_col -= 1
			if len(ans) < r * c and start_col <= end_col:
				for i in range(end_col, start_col - 1, -1):
					ans.append(matrix[end_row][i])
				end_row -= 1
			if len(ans) < r * c and start_row <= end_row:
				for i in range(end_row, start_row - 1, -1):
					ans.append(matrix[i][start_col])
				start_col += 1
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(sr, sc, dr, dc) = (0, 0, r - 1, c - 1)
		tne = r * c
		count = 0
		ans = []
		while count < tne:
			i = sr
			j = sc
			while count < tne and j <= dc:
				ans.append(matrix[i][j])
				count += 1
				j += 1
			sr += 1
			i = sr
			j = dc
			while count < tne and i <= dr:
				ans.append(matrix[i][j])
				count += 1
				i += 1
			dc -= 1
			i = dr
			j = dc
			while count < tne and j >= sc:
				ans.append(matrix[i][j])
				count += 1
				j -= 1
			dr -= 1
			i = dr
			j = sc
			while count < tne and i >= sr:
				ans.append(matrix[i][j])
				count += 1
				i -= 1
			sc += 1
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		result = []
		while matrix:
			result.extend(matrix.pop(0))
			matrix = list(zip(*matrix))[::-1]
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		li = matrix.pop(0)
		while len(matrix):
			si = []
			for i in zip(*matrix):
				si.append(i)
			matrix = si[::-1]
			li += matrix.pop(0)
		return li


class Solution:

	def spirallyTraverse(self, a, m, n):
		res = []
		k = 0
		l = 0
		while k < m and l < n:
			for i in range(l, n):
				res.append(a[k][i])
			k += 1
			for i in range(k, m):
				res.append(a[i][n - 1])
			n -= 1
			if k < m:
				for i in range(n - 1, l - 1, -1):
					res.append(a[m - 1][i])
				m -= 1
			if l < n:
				for i in range(m - 1, k - 1, -1):
					res.append(a[i][l])
				l += 1
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		ans = []
		top = -1
		right = c
		left = -1
		bottom = r
		i = 0
		j = 0
		while top < bottom and left < right:
			while j < right:
				ans.append(matrix[i][j])
				j += 1
			j -= 1
			top += 1
			i += 1
			if left + 1 == right or top + 1 == bottom:
				break
			while i < bottom:
				ans.append(matrix[i][j])
				i += 1
			i -= 1
			right -= 1
			j -= 1
			if left + 1 == right or top + 1 == bottom:
				break
			while j > left:
				ans.append(matrix[i][j])
				j -= 1
			j += 1
			bottom -= 1
			i -= 1
			if left + 1 == right or top + 1 == bottom:
				break
			while i > top:
				ans.append(matrix[i][j])
				i -= 1
			i += 1
			left += 1
			j += 1
			if left + 1 == right or top + 1 == bottom:
				break
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		ROWS = list(range(r))
		COLS = list(range(c))
		res = []
		while ROWS and COLS:
			rowStart = ROWS.pop(0)
			for col in COLS:
				res.append(matrix[rowStart][col])
			colEnd = COLS.pop()
			for row in ROWS:
				res.append(matrix[row][colEnd])
			if ROWS:
				rowEnd = ROWS.pop()
				for col in COLS[::-1]:
					res.append(matrix[rowEnd][col])
			if COLS:
				colStart = COLS.pop(0)
				for row in ROWS[::-1]:
					res.append(matrix[row][colStart])
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		l = []
		ri = ci = 0
		while ri < r and ci < c:
			for i in range(ci, c):
				l.append(matrix[ri][i])
			ri += 1
			for i in range(ri, r):
				l.append(matrix[i][c - 1])
			c -= 1
			if ri < r:
				for i in range(c - 1, ci - 1, -1):
					l.append(matrix[r - 1][i])
				r -= 1
			if ci < c:
				for i in range(r - 1, ri - 1, -1):
					l.append(matrix[i][ci])
				ci += 1
		return l


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		u = 0
		d = r - 1
		l = 0
		ri = c - 1
		count = 0
		i = 0
		j = 0
		k = 0
		res = []
		p = r * c - 1
		while count < p:
			while count < r * c and j < ri:
				res.append(matrix[i][j])
				count += 1
				j += 1
			u += 1
			while count < p and i < d:
				res.append(matrix[i][j])
				count += 1
				i += 1
			ri -= 1
			while count < p and j > l:
				res.append(matrix[i][j])
				j -= 1
				count += 1
			d -= 1
			while count < p and i > u:
				res.append(matrix[i][j])
				i -= 1
				count += 1
			l += 1
		res.append(matrix[i][j])
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		row = 0
		column = 0
		res = []
		while row < r and column < c:
			for i in range(column, c):
				res.append(matrix[row][i])
			row += 1
			for i in range(row, r):
				res.append(matrix[i][c - 1])
			c = c - 1
			if row < r:
				for i in range(c - 1, column - 1, -1):
					res.append(matrix[r - 1][i])
				r = r - 1
			if column < c:
				for i in range(r - 1, row - 1, -1):
					res.append(matrix[i][column])
				column = column + 1
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		dir = 0
		(top, down, left, right) = (0, r - 1, 0, c - 1)
		res = []
		while top <= down and left <= right:
			if dir == 0:
				for i in range(left, right + 1):
					res.append(matrix[top][i])
				top += 1
			if dir == 1:
				for i in range(top, down + 1):
					res.append(matrix[i][right])
				right -= 1
			if dir == 2:
				for i in range(right, left - 1, -1):
					res.append(matrix[down][i])
				down -= 1
			elif dir == 3:
				for i in range(down, top - 1, -1):
					res.append(matrix[i][left])
				left += 1
			dir = (dir + 1) % 4
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		flattened_matrix = []
		while matrix:
			try:
				r1 = matrix.pop(0)
				for i in r1:
					flattened_matrix.append(i)
			except:
				pass
			try:
				for j in matrix:
					val = j.pop()
					flattened_matrix.append(val)
			except:
				pass
			try:
				r_last = matrix.pop()[::-1]
				for k in r_last:
					flattened_matrix.append(k)
			except:
				pass
			try:
				left_side = []
				for l in matrix:
					val = l.pop(0)
					left_side.append(val)
				flattened_matrix += left_side[::-1]
			except:
				pass
		return flattened_matrix


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(top, down) = (0, r - 1)
		(left, right) = (0, c - 1)
		dir = 0
		result = []
		while top <= down and left <= right:
			if dir == 0:
				for r in range(left, right + 1):
					result.append(matrix[top][r])
				top += 1
			elif dir == 1:
				for d in range(top, down + 1):
					result.append(matrix[d][right])
				right -= 1
			elif dir == 2:
				for l in range(right, left - 1, -1):
					result.append(matrix[down][l])
				down -= 1
			else:
				for u in range(down, top - 1, -1):
					result.append(matrix[u][left])
				left += 1
			dir = (dir + 1) % 4
		return result


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		l = []
		rs = 0
		re = r - 1
		cs = 0
		ce = c - 1
		while rs <= re and cs <= ce:
			for i in range(cs, ce + 1):
				l.append(matrix[rs][i])
			rs += 1
			for i in range(rs, re + 1):
				l.append(matrix[i][ce])
			ce -= 1
			if rs <= re:
				for i in range(ce, cs - 1, -1):
					l.append(matrix[re][i])
				re -= 1
			if cs <= ce:
				for i in range(re, rs - 1, -1):
					l.append(matrix[i][cs])
				cs += 1
		return l


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(top, down, left, right) = (0, len(matrix) - 1, 0, len(matrix[0]) - 1)
		(direc, arr) = (0, [])
		while top <= down and left <= right:
			if direc == 0:
				for i in range(left, right + 1):
					arr.append(matrix[top][i])
				top += 1
			elif direc == 1:
				for i in range(top, down + 1):
					arr.append(matrix[i][right])
				right -= 1
			elif direc == 2:
				for i in range(right, left - 1, -1):
					arr.append(matrix[down][i])
				down -= 1
			else:
				for i in range(down, top - 1, -1):
					arr.append(matrix[i][left])
				left += 1
			direc = (direc + 1) % 4
		return arr


class Solution:

	def spirallyTraverse(self, m, r, c):
		a = []
		count = 0
		up = 0
		left = 0
		right = c - 1
		down = r - 1
		while count < r * c:
			for i in range(left, right + 1):
				if count >= r * c:
					break
				a.append(m[up][i])
				count += 1
			up += 1
			for i in range(up, down + 1):
				if count >= r * c:
					break
				a.append(m[i][right])
				count += 1
			right -= 1
			for i in range(right, left - 1, -1):
				if count >= r * c:
					break
				a.append(m[down][i])
				count += 1
			down -= 1
			for i in range(down, up - 1, -1):
				if count >= r * c:
					break
				a.append(m[i][left])
				count += 1
			left += 1
		return a


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		m = []
		rmin = 0
		rmax = r - 1
		cmin = 0
		cmax = c - 1
		while rmin <= rmax and cmin <= cmax:
			for i in range(cmin, cmax + 1):
				m.append(matrix[rmin][i])
			for i in range(rmin + 1, rmax + 1):
				m.append(matrix[i][cmax])
			if rmin < rmax:
				for i in range(cmax - 1, cmin - 1, -1):
					m.append(matrix[rmax][i])
			if cmin < cmax:
				for i in range(rmax - 1, rmin, -1):
					m.append(matrix[i][cmin])
			cmin += 1
			cmax -= 1
			rmin += 1
			rmax -= 1
		return m


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		start_row_ind = 0
		start_col_ind = 0
		end_row_ind = r - 1
		end_col_ind = c - 1
		count = 0
		total_elements = r * c
		res = []
		while count < total_elements:
			i = start_col_ind
			while i <= end_col_ind and count < total_elements:
				res.append(matrix[start_row_ind][i])
				i += 1
				count += 1
			start_row_ind += 1
			i = start_row_ind
			while i <= end_row_ind and count < total_elements:
				res.append(matrix[i][end_col_ind])
				i += 1
				count += 1
			end_col_ind -= 1
			i = end_col_ind
			while i >= start_col_ind and count < total_elements:
				res.append(matrix[end_row_ind][i])
				i -= 1
				count += 1
			end_row_ind -= 1
			i = end_row_ind
			while i >= start_row_ind and count < total_elements:
				res.append(matrix[i][start_col_ind])
				i -= 1
				count += 1
			start_col_ind += 1
		return res


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		i = r
		j = c
		r = 0
		c = 0
		ans = []
		direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]
		reverse = [[1, -1], [-1, -1], [-1, 1], [1, 1]]
		order = 0

		def outOfBound(r, c):
			return r < 0 or c < 0 or r == i or (c == j)
		while True:
			if outOfBound(r, c) or matrix[r][c] == -9999:
				r += reverse[order][0]
				c += reverse[order][1]
				order = (order + 1) % 4
			if len(ans) == i * j:
				break
			ans.append(matrix[r][c])
			matrix[r][c] = -9999
			r += direction[order][0]
			c += direction[order][1]
			len(ans)
		return ans


class Solution:

	def spirallyTraverse(self, matrix, r, c):
		(sr, sc) = (0, 0)
		(er, ec) = (len(matrix) - 1, len(matrix[0]) - 1)
		res = []
		while sr <= er and sc <= ec:
			for i in range(sc, ec + 1):
				res.append(matrix[sc][i])
			for i in range(sr + 1, er + 1):
				res.append(matrix[i][ec])
			for i in range(ec - 1, sc - 1, -1):
				if sr == er:
					break
				res.append(matrix[er][i])
			for i in range(er - 1, sr, -1):
				if ec == sc:
					break
				res.append(matrix[i][sc])
			sr += 1
			sc += 1
			er -= 1
			ec -= 1
		return res