var a: array[1..1000,1..1000] of byte;
i: array[1..1000,1..1000] of byte;
sx,sy: array[1..1000] of integer;
sum,mx,tx,ty: integer;
x,y,n,m,z,t: integer;
f: text;
c:char;

begin
assign(f,'video.in');
reset(f);
readln(f,n,m);
for y:=1 to n do
begin
for x:=1 to m do
begin
read(f,c);
a[x,y]:=strtoint(c);
end;
readln(f);
end;

close(f);

sum:=0;

for y:=1 to n do
for x:=1 to m do
begin
z:=a[x,y];
if x=m then tx:=1 else tx:=x+1;
if a[tx,y] = z then
begin
if y=n then ty:=1 else ty:=y+1;
if a[x,ty] = z then
if a[tx,ty] = z then
begin
i[x,y]:=1;
sx[x]:=sx[x]+1;
sy[y]:=sy[y]+1;
sum:=sum+1;
end;
end;
end;

mx:=i[1,1]-sx[1]-sy[1];

for y:=1 to n do
for x:=1 to m do
begin
t:=i[x,y]-sx[x]-sy[y];
if mx<t then mx:=t;
end;

mx:=mx+sum;

assign(f,'video.out');
rewrite(f);
write(f,mx);
close(f);

end.