//Bai 17
#include<bits/stdc++.h>
using namespace std;
#define el "\n"
#define ll long long
#define ull unsigned long long
#define se second
#define fi first
#define be begin()
#define en end()
#define Faster cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
int n,m;
void Run()
{
cin >> n >> m;
ll ans = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
int x; cin >> x;
if(i == 0 || i == n-1 || j == 0 || j == n-1) ans += 1ll*x;
}
}
cout << ans;
}
int main()
{
Faster;
Run();
return 0;
}
Ly9CYWkgMTcKCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZWwgIlxuIgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIHVsbCB1bnNpZ25lZCBsb25nIGxvbmcKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBmaSBmaXJzdAojZGVmaW5lIGJlIGJlZ2luKCkKI2RlZmluZSBlbiBlbmQoKQojZGVmaW5lIEZhc3RlciBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKaW50IG4sbTsKdm9pZCBSdW4oKQp7CiAgICBjaW4gPj4gbiA+PiBtOwogICAgbGwgYW5zID0gMDsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICB7CiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IG07IGorKykKICAgICAgICB7CiAgICAgICAgICAgIGludCB4OyBjaW4gPj4geDsKICAgICAgICAgICAgaWYoaSA9PSAwIHx8IGkgPT0gbi0xIHx8IGogPT0gMCB8fCBqID09IG4tMSkgYW5zICs9IDFsbCp4OwogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgYW5zOwp9CmludCBtYWluKCkKewogICAgRmFzdGVyOwogICAgUnVuKCk7CiAgICByZXR1cm4gMDsKfQoK