#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m, i, j, k, x, z;
  cin >> n >> m;
  string s[n];
  for (i = (0); i < (n); ++i) cin >> s[i];
  if (n == 1) {
    cout << 0 << "\n";
    return 0;
  }
  long long ans = 0;
  int ok[n][m + 1];
  memset(ok, 0, sizeof(ok));
  for (j = (0); j < (m); ++j) {
    bool h = 1;
    int ok2[n];
    for (i = (1); i < (n); ++i) ok2[i] = ok[i][j];
    for (i = (1); i < (n); ++i) {
      if (s[i][j] < s[i - 1][j]) {
        if (ok[i][j]) {
          ok[i][j + 1] = 1;
          continue;
        }
        h = 0;
        break;
      } else {
        if (s[i][j] > s[i - 1][j])
          ok[i][j + 1] = 1;
        else
          ok[i][j + 1] = ok[i][j];
      }
    }
    if (!h) {
      ans++;
      for (i = (1); i < (n); ++i) ok[i][j + 1] = ok2[i];
    }
  }
  cout << ans << "\n";
  return 0;
}
