program ProjectA;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var n, m : longint;
    tab : array[1..3000, 1..3000] of longint;
    i, j, p, q : longint;
    s : string;
    kol, mx : longint;

function check(x, y : longint) : boolean;
begin
  if (tab[x][y] = tab[x+1][y]) and (tab[x][y] = tab[x][y+1]) and (tab[x][y] = tab[x+1][y+1]) then
    check := True
  else
    check := False;
end;

begin
  assign(input, 'video.in');  assign(output, 'video.out');
//  assign(input, 'input.txt');  assign(output, 'output.txt');

  readln(n, m);

  mx := 0;

  for i := 1 to n do
  begin
    readln(s);
    for j := 1 to m do
    begin
      tab[i][j] := ord(s[j]) - ord('0');
      tab[i + n][j] := tab[i][j];
      tab[i][j + m] := tab[i][j];
      tab[i + n][j + m] := tab[i][j];
    end;
  end;

  for i := 1 to n do
    for j := 1 to m do
    begin
      kol := 0;
      for p := i to i+n-2 do
        for q := j to j+m-2 do
          if check(p, q) then
            inc(kol);
      if kol > mx then
        mx := kol;
    end;

  writeln(mx);
end.
