#include<vector>
#include<iostream>
using namespace std;
int m,n;
void Link(const int Key,int x,int y,const int Set,vector<vector<int>>& Map,int& sum)
{
if(Map[x][y] == Key){
sum++;
Map[x][y] = Set;
if(x - 1 >= 0 && Map[x - 1][y] == Key){
Link(Key,x - 1,y,Set,Map,sum);
}
if(x + 1 < m && Map[x + 1][y] == Key){
Link(Key,x + 1,y,Set,Map,sum);
}
if(y - 1 >= 0 && Map[x][y - 1] == Key){
Link(Key,x,y - 1,Set,Map,sum);
}
if(y + 1 < n && Map[x][y + 1] == Key){
Link(Key,x,y + 1,Set,Map,sum);
}
}
else{
return;
}
}
int main(){
int sum = 0;
cin >> m >> n;
vector<vector<int>> Map(m,vector<int>(n,0));
for(int i = 0;i < m;i ++){
for(int j = 0;j < n;j ++){
int tmp;
scanf("%d",&tmp);
Map[i][j] = tmp;
}
}
for(int i = 0;i < n;i ++){
if(Map[0][i] == 0){
Link(0,0,i,-1,Map,sum);
}
}
for(int i = 0;i < m;i ++){
if(Map[i][0] == 0){
Link(0,i,0,-1,Map,sum);
}
if(Map[i][n - 1] == 0){
Link(0,i,n - 1,-1,Map,sum);
}
}
for(int i = 0;i < m;i ++){
for(int j = 0;j < n;j ++){
if(Map[i][j] == 0){
Map[i][j] = 1;
}
}
}
sum = 0;
Link(1,m - 1,0,2,Map,sum);
printf("%d\n",sum);
return 0;
}