package pack;
import java.util.*;
class Filltool
{
Scanner s = new Scanner(System.in);
String str;
int r = s.nextInt();
int q = s.nextInt();
char ch[] = new char[q];
int arr[][] = new int[r][q];
void insert()
{
for(int i = 0; i < r; i++)
{
str = s.next();
ch = str.toCharArray();
for (int j = 0; j < q; j++)
{
if (ch[j] == '0')
{
arr[i][j] = 0;
}
else if (ch[j] == '1')
{
arr[i][j] = 1;
}
else
{
System.out.println("invalid output");
return;
}
}
}
}
int c=1,count=0,t,ccount=0,count1=0;
int[] c1= new int[100];
int[][] arr1=new int[r][q];
//int[][] arr={{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0}};
public int tool()
{
c1[0]=-1;
for(int x=0;x<r;x++)
{
for(int j=0;j<q;j++)
{
if(arr[x][j]==0)
{
fill(x,j,c);
count1=filling(c);
arr[x][j]=2;
tool();
}
}
}
return count1;
}
public int filling(int c)
{
count=count+1;
c1[c]=c;
for(c=c+1;c1[c-1]==c-1;c++)
{
for(int a=0;a<r;a++)
{
for(int b=0;b<q;b++)
{
if(arr1[a][b]==(c-1))
{
fill(a,b,c);
c1[c]=c;
//System.out.println(c1[c]);
}
}
}
}
c=c-1;
return count;
}
void fill(int x,int j,int c)
{
for (int i=j+1;i<q;i++)
{
if(arr[x][i]==0)
{
arr[x][i]=2;
arr1[x][i]=c;
}
else if(arr[x][i]==1)
break;
}
for (int i=j-1;((i>=0)&&(i<q));i--)
{
if(arr[x][i]==0)
{
arr[x][i]=2;
arr1[x][i]=c;
}
else if(arr[x][i]==1)
break;
}
for (int l=x+1;l<r;l++)
{
if(arr[l][j]==0)
{
arr[l][j]=2;
arr1[l][j]=c;
}
else if(arr[l][j]==1)
break;
}
for (int l=x-1;((l>=0)&&(l<r));l--)
{
if(arr[l][j]==0)
{
arr[l][j]=2;
arr1[l][j]=c;
}
else if(arr[l][j]==1)
break;
}
for(int i=x+1,l=j+1;(((i>=0)&&(i<r))&&((l<q)&&(l>=0)));i++,l++)
{
if(arr[i][l]==0)
{
arr[i][l]=2;
arr1[i][l]=c;
}
else if(arr[i][l]==1)
break;
}
for(int i=x+1,l=j-1;(((i>=0)&&(i<r))&&((l<q)&&(l>=0)));i++,l--)
{
if(arr[i][l]==0)
{
arr[i][l]=2;
arr1[i][l]=c;
}
else if(arr[i][l]==1)
break;
}
for(int i=x-1,l=j+1;(((i>=0)&&(i<r))&&((l<q)&&(l>=0)));i--,l++)
{
if(arr[i][l]==0)
{
arr[i][l]=2;
arr1[i][l]=c;
}
else if(arr[i][l]==1)
break;
}
for(int i=x-1,l=j-1;(((i>=0)&&(i<r))&&((l<q)&&(l>=0)));i--,l--)
{
if(arr[i][l]==0)
{
arr[i][l]=2;
arr1[i][l]=c;
}
else if(arr[i][l]==1)
break;
}
}
public static void main(String[] args)
{
// write here your solution for filltool using Java
Filltool obj=new Filltool();
obj.insert();
int ans=obj.tool();
System.out.print(ans);
}
}
there is an error ... can u please find it and solve it... with 12 hours.. please very urgent