Код:
for(int col= 0; col< M;col++)
{
     sum += arr[0,col];
}

for(int row= 0; row< N;row++)
{
     sum += arr[row,0];
}

for(int col= 0; col< M;col++)
{
     sum += arr[N,col];
}

for(int row= 0; row< N;row++)
{
     sum += arr[row,M];
}