program Z5;

{$APPTYPE CONSOLE}

uses
  SysUtils;
   var i,j,n,m,k,mx,l1,l2,k1,y:longint;
       s:string;
       a:array [1..2000,1..2000] of char;
begin
 assign(input,'video.in');
 reset(input);
 assign(output,'video.out');
 rewrite(output);
 readln(n,m);
 for i:=1 to n do
  begin
    readln(s);
     for j:=1 to m do
      begin
        a[i,j]:=s[j];
        a[i+n,j]:=s[j];
        a[i,j+m]:=s[j];
        a[i+n,j+m]:=s[j];
      end;
  end;
  close(input);
  mx:=0;
  k:=0;
   for i:=1 to n-1 do
    for j:=1 to m-1 do
     begin
      if (a[i,j]=a[i+1,j])and(a[i,j]=a[i+1,j+1])and(a[i,j]=a[i,j+1]) then k:=k+1;

     end;
     k1:=k;
     mx:=k;
    for i:=1 to n do

     for j:=1 to m do
      begin
        if (j=1)and(i<>1) then
         begin
          l1:=0;
          l2:=0;
           for y:=1 to m-1 do
             begin
               if (a[i,y]=a[i-1,y])and(a[i,y]=a[i,y+1])and(a[i,y]=a[i-1,y+1]) then l1:=l1+1;
               if (a[i+n-1,y]=a[i-2+n,y])and(a[i+n-1,y]=a[i+n-1,y+1])and(a[i+n-1,y]=a[i-2+n,y+1]) then l2:=l2+1;

             end;
            k:=k1-l1+l2;
               k1:=k;
               if mx<k then mx:=k;
         end;
         if j<>1 then
          begin
           l1:=0;
           l2:=0;
            for y:= i to n+i-2 do
             begin
               if (a[y,j]=a[y+1,j])and(a[y,j]=a[y,j-1])and(a[y,j]=a[y+1,j-1]) then l1:=l1+1;
               if (a[y,j+m-1]=a[y+1,j+m-1])and(a[y,j+m-1]=a[y,j-2+m])and(a[y,j+m-1]=a[y+1,j-2+m]) then l2:=l2+1;


             end;
            k:=k-l1+l2;
                if mx<k then mx:=k;
          end;


      end;
   writeln(mx);
  close(output);
end.
