20250708
//#include<stdio.h>
//int arr[105][105]={},n,cnt=0,a=0,b[1005]={}, i, j,k, max=-1;
//void f(int y, int x)
//{
// a++;
// arr[y][x]=cnt;
// if (arr[y+1][x]==-1)
// {
// f(y+1, x);
// }
// if (arr[y-1][x]==-1)
// {
// f(y-1, x);
// }
// if (arr[y][x+1]==-1)
// {
// f(y, x+1);
// }
// if (arr[y][x-1]==-1)
// {
// f(y, x-1);
// }
//}
//int main()
//{
// scanf("%d", &n);
// for(i=1;i<=n;i++)
// {
// for(j=1;j<=n;j++)
// {
// scanf("%1d", &k);
// if (k)
// {
// arr[i][j]=-1;
// }
// }
// }
// for(i=1;i<=n;i++)
// {
// for(j=1;j<=n;j++)
// {
// if (arr[i][j]==-1)
// {
// cnt++;
// f(i,j);
// if (a>max)
// {
// max = a;
// }
// b[a]++;
// a = 0;
// }
// }
// }
// printf("%d\n", cnt);
// for(i=1;i<=max;i++)
// {
// for(j=1;j<=b[i];j++)
// {
// printf("%d\n", i);
// }
// }
//// printf("\n\n");
//// for(i=1;i<=n;i++)
//// {
//// for(j=1;j<=n;j++)
//// {
//// printf("%d ", arr[i][j]);
//// }
//// printf("\n");
//// }
// return 0;
//}
// /*
//25
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101
//0101010101010101010101010
//1010101010101010101010101 */
#include<stdio.h>
int n, m,q[10005][2]={}, top=-1,b=-1,i,j,arr[1005][1005]={},k=1,cnt=0;
int main()
{
scanf("%d %d", &m, &n);
for(i=0;i<=n+1;i++)
{
for(j=0;j<=m+1;j++)
{
if (i<1 || j<1 || i>n || j>m)
{
arr[i][j]=-1;
}
else
{
scanf("%d", &arr[i][j]);
if (arr[i][j]==1)
{
top++;
q[top][0]=i;
q[top][1]=j;
}
}
}
}
while(top>b)
{
b++;
int y=q[b][0];
int x=q[b][1];
if (arr[y+1][x]==0)
{
arr[y+1][x]=1;
top++;
q[top][0]=y+1;
q[top][1]=x;
}
if (arr[y-1][x]==0)
{
arr[y+1][x]=1;
top++;
q[top][0]=y+1;
q[top][1]=x;
}
if (arr[y][x+1]==0)
{
arr[y][x+1]=1;
top++;
q[top][0]=y;
q[top][1]=x+1;
}
if (arr[y][x-1]==0)
{
arr[y][x-1]=1;
top++;
q[top][0]=y;
q[top][1]=x-1;
}
}
printf("%d", b);
return 0;
}




