#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
char s[maxn][maxn];
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s[i]);
  }
  vector<pair<int, int> > intervals;
  intervals.push_back(make_pair(0, n - 1));
  int ans = 0;
  for (int i = 0; i < m; ++i) {
    if ((int)intervals.size() == n) {
      break;
    }
    vector<pair<int, int> > new_intervals;
    bool deleted = false;
    for (size_t j = 0; j < intervals.size(); ++j) {
      char ch = s[intervals[j].first][i];
      int last_r = intervals[j].first;
      bool flag = true;
      for (int r = intervals[j].first + 1; r <= intervals[j].second; ++r) {
        if (s[r][i] < ch) {
          flag = false;
          break;
        }
        if (s[r][i] > ch) {
          new_intervals.push_back(make_pair(last_r, r - 1));
          ch = s[r][i];
          last_r = r;
        }
      }
      if (flag) {
        new_intervals.push_back(make_pair(last_r, intervals[j].second));
      } else {
        deleted = true;
      }
    }
    if (!deleted) {
      intervals = new_intervals;
    } else {
      ans++;
    }
  }
  printf("%d\n", ans);
}
