#include <bits/stdc++.h>
using namespace std;

int n, m;
long long a[1505][1505];
long long sum;
long long sum2;

void aaa(int x, int y, int totsum)
{
	if (x == n && y == m)
	{
		if (totsum > sum2)
		{
			totsum = sum2;
		}
	}
    if (a[x+1][y] == 1)
	{
		aaa(x+1, y, totsum+1);
	}
	if (a[x-1][y] == 1)
	{
		aaa(x-1, y, totsum+1);
	}
	if (a[x][y+1] == 1)
	{
		aaa(x, y+1, totsum+1);
	}
	if (a[x][y-1] == 1)
	{
		aaa(x, y-1, totsum+1);
	}
}

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("%lld", &a[i][j]);
			sum += a[i][j];
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (a[i][j] == 1)
			{
				aaa(i, j, 0);
				printf("%d\n", sum-sum2);
				return 0;
			}
		}
	}
	return 0;
}