Answer to Question #314578 in Python for Manikanta

Question #314578

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



1
Expert's answer
2022-03-21T02:03:36-0400
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS