2017年11月01日普及组 I Got a Matrix!

Description

给定一个 n∗m 的矩阵 A,询问位于矩阵边缘的元素之和。所谓矩阵边缘的元素,就是第一行和最后一行的元素以及第一列和最后一列的元素。

Input

第一行包含两个整数 n 和 m。
之后 n 行每行包含 m 个整数 A[i,j]。

Output

共一行包含一个整数 ans,表示位于矩阵边缘的元素之和。

Sample Input

3 3
3 4 1
3 7 1
2 0 1
Sample Output

15
Hint
对于 100% 的数据:n,m ≤ 100

程序:

var
  n,m,i,j,k:longint;
  ans:int64;
begin
  assign(input,'matrix.in');
  assign(output,'matrix.out');
  reset(input);
  rewrite(output);
  readln(n,m);
  for i:=1 to n do
    for j:=1 to m do
    begin
      read(k);
      if (i=1)or(i=n)or(j=1)or(j=m) then inc(ans,k);
    end;
  writeln(ans);
  close(input);
  close(output);
end.
posted @ 2017-11-04 15:16  银叶草  阅读(122)  评论(0编辑  收藏  举报
Live2D