#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
char mp[MAXN][MAXN];
int n, m;
vector<pair<int, int> > vec[MAXN];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
  }
  int cnt = 0;
  int pre = 0;
  vec[0].push_back(make_pair(1, n));
  for (int j = 1; j <= m; j++) {
    int ok = 1;
    for (int c = 0; c < vec[pre].size(); c++) {
      int l = vec[pre][c].first;
      int r = vec[pre][c].second;
      int fg = 1;
      for (int i = l + 1; i <= r; i++) {
        if (mp[i - 1][j] > mp[i][j]) {
          ok = false;
          break;
        } else if (mp[i - 1][j] == mp[i][j]) {
          if (fg) {
            vec[j].push_back(make_pair(i - 1, i));
            fg = 0;
          } else {
            vec[j].back().second++;
          }
        } else
          fg = 1;
      }
      if (!ok) break;
    }
    if (!ok) {
      ++cnt;
      vec[j].clear();
    } else {
      pre = j;
    }
  }
  printf("%d\n", cnt);
  return 0;
}
