(*
 * II Всероссийская Командная Олимпиада Школьников по Программированию
 *
 * Задача:      Блокада
 * Автор:       Георгий Корнеев
 * Решение:     Андрей Станкевич
 *)


program blockade;

var
    a: array [1..200, 1..200] of char;
    can: array [1..100, 1..100] of integer;
    m, n, k, i, j, p, main, answer, best: longint;
    u: array [char] of boolean;
    border, neighbor, isolated: array [0..100] of boolean;
    ind: array [char] of longint;
    c: char;
    no: boolean;

begin
{    assign(input, 'input.txt');
    reset(input);
    assign(output, 'output.txt');
    rewrite(output);}
    
    readln(m, n);
    for i := 1 to m do
    begin
        for j := 1 to n do
        begin
            read(a[i][j]);
            u[a[i][j]] := true;
        end;            
        readln;
    end;
    
    k := 0;
    for c := #33 to #255 do
        if u[c] then
        begin
            inc(k);
            ind[c] := k;
        end;
    
    if k > 100 then
    begin
        writeln('fatal! k = ', k);
        runerror(255);
    end;
    
    
    fillchar(can, sizeof(can), $ff);
    for i := 1 to m do
        for j := 1 to n do
        begin
            if (i < m) then
            begin
                can[ind[a[i][j]]][ind[a[i+1][j]]] := 1;
                can[ind[a[i+1][j]]][ind[a[i][j]]] := 1;
            end;
            if (j < n) then
            begin
                can[ind[a[i][j]]][ind[a[i][j+1]]] := 1;
                can[ind[a[i][j+1]]][ind[a[i][j]]] := 1;
            end;
        end;
        
    for i := 1 to m - 1 do
        for j := 1 to n - 1 do
        begin
            if  (a[i][j] <> a[i+1][j]) and
                (a[i][j] <> a[i][j+1]) and
                (a[i][j] <> a[i+1][j+1]) and
                (a[i+1][j] <> a[i][j+1]) and
                (a[i+1][j] <> a[i+1][j+1]) and
                (a[i][j+1] <> a[i+1][j+1]) then
            begin
                writeln('fatal! 2x2 at (', i, ',', j, ') contains 4 provinces!');
            end;
        end;
        
    for i := 1 to k do
        can[i][i] := 0;
        
    for i := 1 to m do
    begin
        border[ind[a[i][1]]] := true;
        border[ind[a[i][n]]] := true;
    end;
    for j := 1 to n do
    begin
        border[ind[a[1][j]]] := true;
        border[ind[a[m][j]]] := true;
    end;
        
    main := ind['A'];
    
    answer := 0;
    for i := 1 to k do
        if can[i][main] <> -1 then
        begin
            neighbor[i] := true;
            inc(answer);
        end;
     
    for p := 1 to k do
        if p <> main then
            for i := 1 to k do
                for j := 1 to k do
                    if (can[i][p] <> -1) and (can[p][j] <> -1) and
                    ((can[i][p] + can[p][j] < can[i][j]) or (can[i][j] = -1)) then
                        can[i][j] := can[i][p] + can[p][j];

    for i := 1 to k do
        isolated[i] := true;
    for i := 1 to k do
        for j := 1 to k do
            if border[i] and (can[i][j] <> -1) then
                isolated[j] := false;

{    writeln('border:');
    for c := #33 to #255 do
        if border[ind[c]] then  
            write(c);
    writeln;        
        
    writeln('neighbor:');
    for c := #33 to #255 do
        if neighbor[ind[c]] then  
            write(c);
    writeln;        
        
    writeln('isolated:');
    for c := #33 to #255 do
        if isolated[ind[c]] then  
            write(c);
    writeln;        }
        
    no := false;         
    for i := 1 to k do
        if isolated[i] then
        begin
            writeln(-1);
            no := true;
            break;
        end;                         
        
    if not no then
    begin
        best := maxlongint;
        for i := 1 to k do
            for j := 1 to k do
                if border[i] and neighbor[j] and (can[i][j] < best) then
                    best := can[i][j];
{        writeln(best, ' ', answer);}
        writeln(best + answer - 1);
    end;
    
                        
    close(input);
    close(output);
end.
