program Video;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const max=1000;
type megabool=0..1;
type matrix=array[1..max,1..max] of megabool;
var
  n,m,i,j,ans,t:integer;
  c:char;
  f,p:matrix;
  r:array[1..4] of megabool;
function plus(x:integer;b:megabool):integer;
begin
  if b=0 then
  begin
    if x=n then result:=1
    else result:=x+1;
  end
  else
  begin
    if x=m then result:=1
    else result:=x+1;
  end;
end;
begin
  assign(input,'video.in');
  assign(output,'video.out');
  reset(input);
  rewrite(output);
  readln(n,m);
  for i:=1 to n do for j:=1 to m do begin p[i,j]:=0; f[i,j]:=0; end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(c);
      if c='1' then p[i,j]:=0
      else p[i,j]:=1;
    end;
    if i<n then readln;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      r[1]:=p[i,j];
      r[2]:=p[i,plus(j,1)];
      r[3]:=p[plus(i,0),j];
      r[4]:=p[plus(i,0),plus(j,1)];
      if (r[1]=r[2]) and (r[2]=r[3]) and (r[3]=r[4]) then f[i,j]:=1;
    end;
  end;
  t:=0;
  ans:=0;
  for i:=1 to n-1 do for j:=1 to m-1 do if f[i,j]=1 then inc(t);
  if t>ans then ans:=t; t:=0;
  for i:=2 to n do for j:=1 to m-1 do if f[i,j]=1 then inc(t);
  if t>ans then ans:=t; t:=0;
  for i:=1 to n-1 do for j:=2 to m do if f[i,j]=1 then inc(t);
  if t>ans then ans:=t; t:=0;
  for i:=2 to n do for j:=2 to m do if f[i,j]=1 then inc(t);
  if t>ans then ans:=t;
  write(ans);
  close(input);
  close(output);
end.
