You are given a matrix of dimensions n x m. The task is to perform boundary traversal on the matrix in a clockwise manner. 
Example 1:
Input:
n = 4, m = 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
Explanation:
The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The boundary traversal is:
1 2 3 4 8 12 16 15 14 13 9 5
Example 2:
Input:
n = 3, m = 4
matrrix[][] = {{12, 11, 10, 9},
         {8, 7, 6, 5},
         {4, 3, 2, 1}}
Output: 12 11 10 9 5 1 2 3 4 8
Your Task:
Complete the function boundaryTraversal() that takes matrix, n and m as input parameters and returns the list of integers that form the boundary traversal of the matrix in a clockwise manner.
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)
Constraints:
1 <= n, m<= 100
0 <= matrix_{i} <= 1000

Difficulty: EASY
Tags: Matrices

Data structures
Skill types: Data structures
Expected Time Complexity: O(N + M)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:
    
    #Function to return list of integers that form the boundary 
    #traversal of the matrix in a clockwise manner.
    def BoundaryTraversal(self,matrix, n, m):
        # code here 

Solutions:
class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		ls = []
		if n == 1:
			return matrix[0]
		if m == 1:
			for i in range(n):
				ls.append(matrix[i][0])
			return ls
		for j in range(m - 1):
			ls.append(matrix[0][j])
		for i in range(n):
			ls.append(matrix[i][m - 1])
		for j in range(m - 2, -1, -1):
			ls.append(matrix[n - 1][j])
		for i in range(n - 2, 0, -1):
			ls.append(matrix[i][0])
		return ls


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		res = []
		if n == 1:
			for j in range(m):
				res.append(matrix[0][j])
		elif m == 1:
			for i in range(n):
				res.append(matrix[i][0])
		else:
			for j in range(m):
				res.append(matrix[0][j])
			for i in range(1, n):
				res.append(matrix[i][m - 1])
			for j in range(m - 2, -1, -1):
				res.append(matrix[n - 1][j])
			for i in range(n - 2, 0, -1):
				res.append(matrix[i][0])
		return res


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		i = 0
		j = 0
		res = []
		while j < m - 1:
			res.append(matrix[i][j])
			j += 1
		if n == 1:
			res.append(matrix[0][m - 1])
			return res
		while i < n - 1:
			res.append(matrix[i][j])
			i += 1
		if m == 1:
			res.append(matrix[n - 1][0])
			return res
		while j > 0:
			res.append(matrix[i][j])
			j -= 1
		while i > 0:
			res.append(matrix[i][j])
			i -= 1
		return res


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		left = 0
		ans = []
		right = m - 1
		top = 0
		bottom = n - 1
		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 BoundaryTraversal(self, a, n, m):
		k = []
		if n == 1:
			return a[0]
		elif m == 1:
			for i in range(n):
				k.append(a[i][0])
		else:
			for i in range(m):
				k.append(a[0][i])
			for i in range(1, n):
				k.append(a[i][m - 1])
			for i in range(m - 2, -1, -1):
				k.append(a[n - 1][i])
			for i in range(n - 2, 0, -1):
				k.append(a[i][0])
		return k


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		l = []
		low = 0
		high = m - 1
		high1 = n - 1
		if n == 1:
			return matrix[0]
		if m == 1:
			l = []
			for i in range(n):
				l.append(matrix[i][0])
			return l
		for i in range(low, high + 1):
			l.append(matrix[0][i])
		for i in range(low + 1, high1 + 1):
			l.append(matrix[i][high])
		for i in range(high - 1, low - 1, -1):
			l.append(matrix[high1][i])
		for i in range(high1 - 1, low, -1):
			l.append(matrix[i][low])
		return l


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		lr = []
		if n == 1:
			j = 0
			i = 0
			while j < m:
				lr.append(matrix[i][j])
				j = j + 1
			return lr
		elif m == 1:
			i = 0
			j = 0
			while i < n:
				lr.append(matrix[i][j])
				i += 1
			return lr
		else:
			row = 0
			col = 0
			ls = []
			while col < m:
				r = row
				c = col
				ls.append(matrix[r][c])
				col += 1
			row = 1
			col = m - 1
			while row < n:
				r = row
				c = col
				ls.append(matrix[r][c])
				row += 1
			row = n - 1
			col = col - 1
			while col >= 0:
				r = row
				c = col
				ls.append(matrix[r][c])
				col -= 1
			row = row - 1
			col = 0
			while row > 0:
				r = row
				c = col
				ls.append(matrix[r][c])
				row -= 1
			return ls


class Solution:

	def BoundaryTraversal(self, matrix, r, c):
		res = []
		if r == 1:
			for i in range(c):
				res.append(matrix[0][i])
		elif c == 1:
			for i in range(r):
				res.append(matrix[i][0])
		else:
			for i in range(c):
				res.append(matrix[0][i])
			for i in range(1, r - 1):
				res.append(matrix[i][-1])
			for i in range(c - 1, -1, -1):
				res.append(matrix[-1][i])
			for i in range(r - 2, 0, -1):
				res.append(matrix[i][0])
		return res


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		arr = []
		if n == 1:
			for i in range(m):
				arr.append(matrix[0][i])
		elif m == 1:
			for i in range(n):
				arr.append(matrix[i][0])
		else:
			for i in range(m):
				arr.append(matrix[0][i])
			for i in range(1, n - 1):
				arr.append(matrix[i][m - 1])
			for i in range(m - 1, 0, -1):
				arr.append(matrix[n - 1][i])
			for i in range(n - 1, 0, -1):
				arr.append(matrix[i][0])
		return arr


class Solution:

	def BoundaryTraversal(self, mat, n, m):
		res = []
		for i in range(m):
			res.append(mat[0][i])
		for i in range(1, n):
			res.append(mat[i][m - 1])
		if n > 1:
			for i in range(m - 2, -1, -1):
				res.append(mat[n - 1][i])
		if m > 1:
			for i in range(n - 2, 0, -1):
				res.append(mat[i][0])
		return res


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		ans = []
		if n == 1 or m == 1:
			for i in range(n):
				for j in range(m):
					ans.append(matrix[i][j])
		else:
			for i in range(m):
				ans.append(matrix[0][i])
			for i in range(1, n):
				ans.append(matrix[i][m - 1])
			for i in range(m - 2, -1, -1):
				ans.append(matrix[n - 1][i])
			for i in range(n - 2, 0, -1):
				ans.append(matrix[i][0])
		return ans


class Solution:

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


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		c = []
		for i in range(m):
			c.append(matrix[0][i])
		if n > 2:
			for j in range(1, n - 1):
				c.append(matrix[j][m - 1])
		if n > 1:
			for i in range(m - 1, -1, -1):
				c.append(matrix[n - 1][i])
		if n > 2 and m > 1:
			for k in range(n - 2, 0, -1):
				c.append(matrix[k][0])
		return c


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		if n == 1:
			return matrix[0]
		lp = []
		if m == 1:
			for u in range(n):
				lp.append(matrix[u][0])
			return lp
		ls = []
		ls += matrix[0]
		lp = []
		for u in range(1, n):
			lp.append(matrix[u][m - 1])
		ls += lp
		lss = matrix[n - 1]
		ls += reversed(lss[:-1])
		lp = []
		for u in range(n - 2, 0, -1):
			lp.append(matrix[u][0])
		ls += lp
		return ls


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		res = []
		if n == 1:
			return matrix[0]
		if m == 1:
			for i in range(n):
				res.append(matrix[i][0])
			return res
		for i in range(m):
			res.append(matrix[0][i])
		for j in range(1, n):
			res.append(matrix[j][m - 1])
		for k in range(m - 2, -1, -1):
			res.append(matrix[n - 1][k])
		for o in range(n - 2, 0, -1):
			res.append(matrix[o][0])
		return res


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		l = []
		if m == 1 and n == 1:
			l.append(matrix[0][0])
		if n == 1 and m != 1:
			for i in range(m):
				l.append(matrix[0][i])
		if m == 1 and n != 1:
			for i in range(n):
				l.append(matrix[i][0])
		elif n > 1 and m > 1:
			for i in range(m):
				l.append(matrix[0][i])
			for j in range(1, n):
				l.append(matrix[j][m - 1])
			for i in range(m - 2, -1, -1):
				l.append(matrix[n - 1][i])
			for j in range(n - 2, 0, -1):
				l.append(matrix[j][0])
		return l


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		(top, bottom) = (0, n - 1)
		(left, right) = (0, m - 1)
		res = []
		for i in range(left, right + 1):
			res.append(matrix[top][i])
		top += 1
		for i in range(top, bottom + 1):
			res.append(matrix[i][right])
		right -= 1
		if not (top <= bottom and left <= right):
			return res
		for i in range(right, left - 1, -1):
			res.append(matrix[bottom][i])
		bottom -= 1
		for i in range(bottom, top - 1, -1):
			res.append(matrix[i][left])
		left += 1
		return res


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		if n == 1:
			return matrix[0]
		if m == 1:
			return [item[0] for item in matrix]
		output = matrix[0].copy()
		for i in range(1, n - 1):
			output.append(matrix[i][-1])
		output += matrix[-1][::-1]
		for i in range(n - 2, 0, -1):
			output.append(matrix[i][0])
		return output


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		if n == 1:
			return matrix[0]
		if m == 1:
			return [matrix[i][0] for i in range(n)]
		if n == 1 and m == 1:
			return [matrix[0][0]]
		row = 0
		col = 0
		boundary = []
		while col < m:
			boundary.append(matrix[row][col])
			col += 1
		row += 1
		col -= 1
		while row < n:
			boundary.append(matrix[row][col])
			row += 1
		col -= 1
		row -= 1
		while col >= 0:
			boundary.append(matrix[row][col])
			col -= 1
		row -= 1
		col += 1
		while row >= 1:
			boundary.append(matrix[row][col])
			row -= 1
		return boundary


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		c = 0
		l = []
		if n == 1:
			return matrix[0]
		if m == 1:
			for i in matrix:
				l.extend(i)
			return l
		for i in range(m - 1):
			l.append(matrix[c][i])
		for i in range(n - 1):
			l.append(matrix[i][m - 1])
		for i in range(m - 1, c, -1):
			l.append(matrix[n - 1][i])
		for i in range(n - 1, c, -1):
			l.append(matrix[i][c])
		return l


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		d = 0
		left = 0
		top = 0
		right = m - 1
		down = n - 1
		a = []
		while top <= down and left <= right and (d != 4):
			if d == 0:
				for i in range(left, right + 1):
					a.append(matrix[top][i])
				top = top + 1
			elif d == 1:
				for i in range(top, down + 1):
					a.append(matrix[i][right])
				right = right - 1
			elif d == 2:
				for i in range(right, left - 1, -1):
					a.append(matrix[down][i])
				down = down - 1
			elif d == 3:
				for i in range(down, top - 1, -1):
					a.append(matrix[i][left])
				left = left + 1
			d = d + 1
		return a


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		l = []
		if len(matrix) == 1:
			l.extend(matrix[0])
			return l
		elif len(matrix[0]) == 1:
			for i in range(len(matrix)):
				l.append(matrix[i][0])
			return l
		for i in range(n):
			if i == 0:
				l.extend(matrix[0])
			if i > 0 and i < n - 1:
				l.append(matrix[i][m - 1])
			elif i == n - 1:
				l.extend(reversed(matrix[n - 1]))
		i = n - 2
		while i > 0:
			l.append(matrix[i][0])
			i = i - 1
		return l


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		visited = set()
		opt = []
		i = 0
		for j in range(m):
			visited.add((i, j))
			opt.append(matrix[i][j])
		j = m - 1
		for i in range(1, n):
			if (i, j) not in visited:
				opt.append(matrix[i][j])
				visited.add((i, j))
		i = n - 1
		for j in range(m - 2, -1, -1):
			if (i, j) not in visited:
				opt.append(matrix[i][j])
				visited.add((i, j))
		j = 0
		for i in range(n - 2, 0, -1):
			if (i, j) not in visited:
				opt.append(matrix[i][j])
				visited.add((i, j))
		return opt


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		l = []
		for i in range(m):
			print(matrix[0][i], end=' ')
		for i in range(1, n):
			print(matrix[i][m - 1], end=' ')
		if n > 1:
			for i in range(m - 2, -1, -1):
				print(matrix[n - 1][i], end=' ')
		if m > 1:
			for i in range(n - 2, 0, -1):
				print(matrix[i][0], end=' ')
		return l


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		ans = []
		if n == 1:
			for item in matrix:
				for c in item:
					ans += [c]
		elif m == 1:
			for item in matrix:
				for c in item:
					ans += [c]
		else:
			for j in range(m):
				i = 0
				ans += [matrix[i][j]]
			for i in range(1, n):
				j = m - 1
				ans += [matrix[i][j]]
			for j in range(1, m):
				col = m - j - 1
				j = m - 1
				i = n - 1
				ans += [matrix[i][col]]
			for i in range(1, n):
				row = n - i - 1
				j = 0
				if row != 0:
					ans += [matrix[row][j]]
		return ans


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		(i, j, rows, cols) = (0, 0, n - 1, m - 1)
		output = []
		for k in range(j, cols + 1):
			output.append(matrix[i][k])
		i += 1
		for k in range(i, rows + 1):
			output.append(matrix[k][cols])
		cols -= 1
		if i <= rows:
			for k in range(cols, j - 1, -1):
				output.append(matrix[rows][k])
			rows -= 1
		if j <= cols:
			for k in range(rows, i - 1, -1):
				output.append(matrix[k][j])
			j += 1
		return output


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		sol = []
		if n == 1 or n == 0:
			for i in range(m):
				sol.append(matrix[0][i])
		elif m == 1 or m == 0:
			for i in range(n):
				sol.append(matrix[i][0])
		else:
			for i in range(m):
				sol.append(matrix[0][i])
			for i in range(1, n):
				sol.append(matrix[i][m - 1])
			for i in range(m - 2, -1, -1):
				sol.append(matrix[n - 1][i])
			for i in range(n - 2, 0, -1):
				sol.append(matrix[i][0])
		return sol


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		cnt = 1
		ans = list()
		row = 0
		col = 0
		if n == 1 and m == 1:
			ans.append(matrix[row][col])
		else:
			while True:
				if cnt == 1:
					ans.append(matrix[row][col])
					col += 1
					if col == m:
						cnt += 1
						row += 1
						col -= 1
						if row == n:
							break
				elif cnt == 2:
					ans.append(matrix[row][col])
					row += 1
					if row == n:
						cnt += 1
						row -= 1
						col -= 1
						if col == -1:
							break
				elif cnt == 3:
					ans.append(matrix[row][col])
					col -= 1
					if col == -1:
						cnt += 1
						row -= 1
						col += 1
					if row == 0:
						break
				elif cnt == 4:
					ans.append(matrix[row][col])
					row -= 1
					if row == 0:
						break
		return ans


class Solution:

	def BoundaryTraversal(self, a, n, m):
		if n == 1 or m == 1:
			l = []
			for i in a:
				for j in i:
					l.append(j)
			return l
		count = 0
		total = 2 * n + 2 * m - 4
		l = []
		sr = 0
		er = n - 1
		sc = 0
		ec = m - 1
		while count < total:
			i = sc
			while i <= ec and count < total:
				l.append(a[sr][i])
				i += 1
				count += 1
			sr += 1
			i = sr
			while i <= er and count < total:
				l.append(a[i][ec])
				i += 1
				count += 1
			ec -= 1
			i = ec
			while i >= sc and count < total:
				l.append(a[er][i])
				i -= 1
				count += 1
			er -= 1
			i = er
			while i >= sr and count < total:
				l.append(a[i][sc])
				i -= 1
				count += 1
			sc += 1
		return l


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		ans = []
		ans.extend(matrix[0])
		for row in range(1, n - 1):
			ans.append(matrix[row][m - 1])
		if n > 1:
			ans.extend(reversed(matrix[n - 1]))
		if m > 1:
			for row in range(n - 2, 0, -1):
				ans.append(matrix[row][0])
		return ans


class Solution:

	def BoundaryTraversal(self, a, n, m):
		output = []
		if n == 1:
			i = 0
			while i < m:
				output.append(matrix[0][i])
				i += 1
		elif m == 1:
			i = 0
			while i < n:
				output.append(matrix[i][0])
				i += 1
		else:
			row_ind = 0
			col_ind = 0
			while col_ind < m:
				output.append(matrix[row_ind][col_ind])
				col_ind += 1
			col_ind = m - 1
			row_ind += 1
			while row_ind < n:
				output.append(matrix[row_ind][col_ind])
				row_ind += 1
			row_ind = n - 1
			col_ind -= 1
			while col_ind > -1:
				output.append(matrix[row_ind][col_ind])
				col_ind -= 1
			col_ind = 0
			row_ind -= 1
			while row_ind > 0:
				output.append(matrix[row_ind][col_ind])
				row_ind -= 1
		return output


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		n = len(matrix)
		m = len(matrix[0])
		sol = []
		if n == 1:
			for _ in range(m):
				sol.append(matrix[0][_])
			return sol
		if m == 1:
			for _ in range(n):
				sol.append(matrix[_][0])
			return sol
		for i in range(m):
			sol.append(matrix[0][i])
		for j in range(1, n):
			sol.append(matrix[j][m - 1])
		for k in range(m - 2, -1, -1):
			sol.append(matrix[n - 1][k])
		for l in range(n - 2, 0, -1):
			sol.append(matrix[l][0])
		return sol


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		b = []
		for i in range(n):
			if i == 0:
				for j in range(m):
					b.append(matrix[i][j])
			elif i == n - 1:
				for k in range(m - 1, -1, -1):
					b.append(matrix[i][k])
			else:
				b.append(matrix[i][m - 1])
		if n >= 3 and m > 1:
			for i in range(n - 2, 0, -1):
				b.append(matrix[i][0])
		return b


class Solution:

	def BoundaryTraversal(self, mat, n, m):
		l = 0
		k = 0
		ans = []
		for i in range(l, m):
			ans.append(mat[k][i])
		k += 1
		for j in range(k, n):
			ans.append(mat[j][m - 1])
		m -= 1
		if k < n:
			for z in range(m - 1, -1, -1):
				ans.append(mat[n - 1][z])
		n -= 1
		if l < m:
			for x in range(n - 1, k - 1, -1):
				ans.append(mat[x][l])
		return ans


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		col_st = 0
		row_end = n - 1
		col_end = m - 1
		row_st = 0
		ls = []
		count = 0
		total = 2 * n + 2 * m - 4
		if n == 1:
			for i in range(m):
				ls.append(matrix[0][i])
			return ls
		if m == 1:
			for i in range(n):
				ls.append(matrix[i][0])
			return ls
		if count < total:
			for i in range(col_end + 1):
				ls.append(matrix[row_st][i])
				count += 1
			row_st += 1
		if count < total:
			for i in range(row_st, row_end + 1):
				ls.append(matrix[i][col_end])
				count += 1
			col_end -= 1
		if count < total:
			for i in range(col_end, col_st - 1, -1):
				ls.append(matrix[row_end][i])
				count += 1
			row_end -= 1
		if count < total:
			for i in range(row_end, row_st - 1, -1):
				ls.append(matrix[i][col_st])
				count += 1
		return ls


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		outter = []
		(left, right) = (0, len(matrix[0]) - 1)
		(top, bottom) = (0, len(matrix) - 1)
		for idx in range(left, right):
			outter.append(matrix[top][idx])
		for idx in range(top, bottom + 1):
			lastRowIdx = len(matrix[idx]) - 1
			outter.append(matrix[idx][lastRowIdx])
		right = len(matrix[-1]) - 1
		for idx in range(right - 1, left - 1, -1):
			if top == bottom:
				break
			outter.append(matrix[bottom][idx])
		for idx in range(bottom - 1, top, -1):
			if left == right:
				break
			outter.append(matrix[idx][left])
		return outter


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		top = 0
		bottom = n - 1
		left = 0
		right = m - 1
		result = []
		i = left
		while i <= right:
			result.append(matrix[top][i])
			i += 1
		top += 1
		i = top
		while i <= bottom:
			result.append(matrix[i][right])
			i += 1
		right -= 1
		i = right
		if top > bottom or left > right:
			return result
		while i >= left:
			result.append(matrix[bottom][i])
			i -= 1
		bottom -= 1
		i = bottom
		while i >= top:
			result.append(matrix[i][left])
			i -= 1
		left += 1
		return result


class Solution:

	def BoundaryTraversal(self, matrix, R, C):
		(top, bottom) = (0, R - 1)
		(left, right) = (0, C - 1)
		result = []
		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 BoundaryTraversal(self, matrix, n, m):
		rows = len(matrix)
		columns = len(matrix[0])
		result = []
		for i in range(columns):
			result.append(matrix[0][i])
		for i in range(1, rows):
			result.append(matrix[i][columns - 1])
		if rows > 1:
			for i in range(columns - 2, -1, -1):
				result.append(matrix[rows - 1][i])
		if columns > 1:
			for i in range(rows - 2, 0, -1):
				result.append(matrix[i][0])
		return result


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		if n == 1:
			return matrix[0]
		if m == 1:
			ans = []
			for i in range(n):
				ans.append(matrix[i][0])
			return ans
		ans = []
		for i in range(m):
			ans.append(matrix[0][i])
		ans.pop()
		for i in range(n):
			ans.append(matrix[i][-1])
		ans.pop()
		for i in range(m - 1, -1, -1):
			ans.append(matrix[-1][i])
		ans.pop()
		for i in range(n - 1, -1, -1):
			ans.append(matrix[i][0])
		ans.pop()
		return ans


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		arr = []
		r = 0
		c = 0
		if n == 1:
			return matrix[0]
		if m == 1:
			for i in range(n):
				arr.append(matrix[i][0])
			return arr
		while c < m - 1:
			arr.append(matrix[r][c])
			c += 1
		while r < n - 1:
			arr.append(matrix[r][c])
			r += 1
		while c > 0:
			arr.append(matrix[r][c])
			c -= 1
		while r > 0:
			arr.append(matrix[r][c])
			r -= 1
		return arr


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		(ROWS, COLS) = (len(matrix), len(matrix[0]))
		res = []
		if n == 1:
			for col in range(COLS):
				res.append(matrix[0][col])
		elif m == 1:
			for row in range(ROWS):
				res.append(matrix[row][0])
		else:
			for col in range(COLS - 1):
				res.append(matrix[0][col])
			for row in range(ROWS - 1):
				res.append(matrix[row][-1])
			for col in range(COLS - 1, 0, -1):
				res.append(matrix[-1][col])
			for row in range(ROWS - 1, 0, -1):
				res.append(matrix[row][0])
		return res


class Solution:

	def BoundaryTraversal(self, matrix, n, m):
		r = []
		if n == 1:
			for i in range(m):
				r.append(matrix[0][i])
		elif m == 1:
			for j in range(n):
				r.append(matrix[j][0])
		else:
			for i in range(m):
				r.append(matrix[0][i])
			for j in range(1, n):
				r.append(matrix[j][m - 1])
			for k in range(m - 2, -1, -1):
				r.append(matrix[n - 1][k])
			for l in range(n - 2, 0, -1):
				r.append(matrix[l][0])
		return r


class Solution:

	def print_j_up(self, arr, const, var, ans):
		while var > 0:
			ans.append(arr[var][const])
			var -= 1

	def print_i_backward(self, arr, const, var, ans):
		while var >= 0:
			ans.append(arr[const][var])
			var -= 1

	def print_j_down(self, arr, const, var, n, ans):
		while var < n:
			ans.append(arr[var][const])
			var += 1

	def print_i_forward(self, arr, const, var, m, ans):
		while var < m:
			ans.append(arr[const][var])
			var += 1

	def BoundaryTraversal(self, arr, n, m):
		if n == 1 or m == 1:
			ans = []
			for i in range(len(arr)):
				for j in range(len(arr[i])):
					ans.append(arr[i][j])
			return ans
		ans = []
		i = 0
		while i < n:
			if i == 0:
				self.print_i_forward(arr, i, 0, m, ans)
				self.print_j_down(arr, m - 1, 1, n, ans)
			if i == n - 1:
				self.print_i_backward(arr, n - 1, m - 2, ans)
				self.print_j_up(arr, 0, n - 2, ans)
			i += 1
		return ans