var t:text;
    q:array[1..2000,1..2000]of integer;
    e,n,m,i,j,a,s,d:integer;
    c:char;
function qw(x,y:integer):integer;
var i,j,z:integer;
begin
z:=0;
for i:=x to x+n-2 do
for j:=y to y+m-2 do
if (q[i,j]=q[i,j+1])and(q[i,j]=q[i+1,j+1])and(q[i,j]=q[i+1,j]) then inc(z);
qw:=z;
{writeln(z);
for i:=x to x+n-1 do
begin
for j:=y to y+m-1 do
write(q[i,j]:2);
writeln;
end;
writeln;}
end;

begin
assign(t,'video.in');
reset(t);
readln(t,n,m);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(t,c);
q[i,j]:=strtoint(c);
q[i+n,j]:=strtoint(c);
q[i,j+m]:=strtoint(c);
q[i+n,j+m]:=strtoint(c);
end;
readln(t);
end;
a:=0;
for i:= 1 to n do
for j:= 1 to m do
begin
s:=qw(i,j);
if s>a then a:=s;
end;

close(t);
assign(t,'video.out');
rewrite(t);

writeln(t,a);
close(t);
end.