#include <bits/stdc++.h>
using namespace std;
const int N = 10010, M = 110;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> v(n, vector<int>(m, 0));
map<string, int> cnt;
map<string, int> rank;
for (int i = 0; i < n; i++) {
string s;
for (int j = 0; j < m; j++) {
cin >> v[i][j];
s += to_string(v[i][j]);
if (j != m - 1) s += " ";
}
cnt[s]++;
}
sort(v.begin(), v.end());
int inc = 1;
for (int i = n - 1; i >= 0; i--) {
string s;
for (int j = 0; j < m; j++) {
s += to_string(v[i][j]);
if (j != m - 1) s += " ";
}
if (!rank.count(s)) rank[s] = inc++;
}
vector<tuple<int, string, int>> res;
for (auto &itr : cnt) {
res.push_back({itr.second, itr.first, rank[itr.first]});
}
sort(res.begin(), res.end(), [&] (tuple<int, string, int> A, tuple<int, string, int> B) {
if (get<0>(A) != get<0>(B)) return get<0>(A) > get<0>(B);
return get<2>(A) > get<2>(B);
});
cout << cnt.size() << "\n";
for (int i = 0; i < res.size(); ++i) {
cout << get<0>(res[i]) << " " << get<1>(res[i]);
if (i != res.size() - 1) cout << "\n";
}
return 0;
}