program casino;
Uses sysutils;
{$H+}
const lung=1000000;
var  N,M,C,w,v,coppie,index:Int64;
     S,S_ruotate:array[0..lung] of AnsiString;
     funz_errore : array[0..2000000] of Int64;
    
function LexicalMinRotation(var x: AnsiString):Int64;
var 
len,K,i,j:Int64;

begin
   x:=x+x;
   len:=length(x); 
   for i:=0 to len  do funz_errore[i]:=-1;
   K:=1; 
   j:=2;
      while j<=len do begin         
             i:= funz_errore[j-k-1];
             while (i <> -1 ) and (x[j] <> x[(k + i+1 )]) and (j<=len ) do
                            begin
                              
                              if x[j] < x[(k + i+1 )] then k:= j - i - 1;
                              i:=funz_errore[i];
                            end;                           
            if (i = -1) and (x[j] <> x[(k + i+1 )]) then
                                                       begin
                                                          if x[j] < x[(k + i+1 )] then k:= j;
                                                          funz_errore[j - k]:= -1;
                                                       end           
                                                    else   funz_errore[j - k]:= i + 1;
               
        
         j:=j+1; end;
 LexicalMinRotation:=k;
     
end;

begin
   (*assign(input, 'input.txt'); reset(input);
   assign(output, 'output.txt'); rewrite(output);*)
   readln (N,M);
   for w:=0 to N-1 do begin readln(S[w]);  S[w]:=Trim(S[w]); end;
   coppie:=0; 
   index:=LexicalMinRotation(S[0]);
   S_ruotate[0]:=copy(S[0],index,M);
   for w:=1 to N-1 do
          begin
            index:=LexicalMinRotation(S[w]);
            S_ruotate[w]:=copy(S[w],index,M);
            if S_ruotate[w]=S_ruotate[0] then  coppie:=coppie+1;
          end;
   
    writeln(coppie);
end.
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        