Matrix Program
you are given an m*n matrix.write a program to compute the perimeter of the matrix and print the result.Perimeter of a matrix is defined as the sum of all elements of the four edges of the matrix.
Input
The first line of input containing the positive integer.
The second line of input containing the positive integer.
The next N lines of input space-separated integers.
Explanation: The input should be
3
4
1 2 3 4
5 6 7 8
9 10 11 12
The output should be 65.
Sample Input1
4
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Sample Output1
102
n, m = int(input()), int(input())
array = [input().split() for i in range(n)]
sum = 0
for i in range(n):
for j in range(m):
if(i == 0 or i == n - 1):
sum += int(array[i][j])
else:
if(j == 0 or j == m - 1):
sum += int(array[i][j])
print(sum)
Comments
Leave a comment