program zadB;
var n, k, m, i, j, f, br: Int32;
    a: array [1..1000000, 1..2000] of Int32;
begin
    f := 0;
    br := 0;
    ReadLn (n, k);
    for i := 1 to n do
        begin
        for j := 1 to n do
            begin
            Read (m);
            a [m, j] := a [m, j] + 1;
            end;
        ReadLn ();
        end;
    for i := 1 to k do
        begin
        for j := 1 to n do
            begin
            if a [i, j] < a [i, j + 1] then f := f + 1;
            if a [i, j] > a [i, j + 1] then
                if a [i, j + 1] <> 0 then f := f + 1;
            end;
        if f = 1 then br := br + 1;
        ReadLn ();
        end;
    WriteLn (br);
end.