#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
//英语 看博友分析 抄博友程序 四叉树 巧妙 没掌握
struct state{
int s1,s2,s3,s4;
bool check(int n1,int n2,int n3,int n4)
{
return n1==s1&&n2==s2&&n3==s3&& n4==s4;
}
}p[10010];//巧妙
int tot;
int c1,c2;
char s[140][140];
int a[140][140];
int dfs(int x1,int y1,int x2,int y2)
{
int t=c1;
c1++,c2++;
if(x1==x2&& y1==y2) return a[x1][y1];
int x3=(x1+x2)/2;
int y3=(y1+y2)/2;
int a1=dfs(x1,y1,x3,y3);
int a2=dfs(x3+1,y1,x2,y3);
int a3=dfs(x1,y3+1,x3,y2);
int a4=dfs(x3+1,y3+1,x2,y2);
if(a1==0&& a2==0 && a3==0 && a4==0)
{
c1=c1-4;
c2=c2-4;
return 0;
}
if(a1==1&& a2==1 && a3==1 && a4==1)
{
c1=c1-4;
c2=c2-4;
return 1;
}
for(int i=2;i<tot;i++)
{
if(p[i].check(a1,a2,a3,a4))
{
c1=t;
return i;
}
}
p[tot++]=(state){a1,a2,a3,a4};
return tot-1;
}
int main()
{
while(1)
{
int n,m;
cin>>n>>m;
if(n==0 && m==0)
{
break;
}
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
scanf("%s",s[i]);
for(int j=0;j<m;j++)
{
a[i][j]=s[i][j]-'0';
}
}
int t=max(n,m);
int k=1;
while(k<t)
{
k=k<<1;//抄博友程序
}
tot=2;
c1=c2=0;
dfs(0,0,k-1,k-1);
cout<<c2<<" "<<c1<<endl;
}
return 0;
}