#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
int n, m;
string lines[1010];
int labels1[1010];
int labels2[1010];
int main() {
  cin >> n >> m;
  for (int i = 0; i < (n); ++i) {
    cin >> lines[i];
  }
  int ans = 0;
  bool bad = false;
  for (int i = 0; i < (m); ++i) {
    labels2[0] = 0;
    for (int j = 1; j < n; ++j) {
      if (labels1[j] > labels1[j - 1]) {
        labels2[j] = labels2[j - 1] + 1;
        continue;
      }
      if (lines[j][i] < lines[j - 1][i]) {
        ++ans;
        bad = true;
        break;
      }
      if (lines[j][i] == lines[j - 1][i]) {
        labels2[j] = labels2[j - 1];
        continue;
      } else if (lines[j][i] > lines[j - 1][i]) {
        labels2[j] = labels2[j - 1] + 1;
      }
    }
    if (labels2[n - 1] == n - 1) {
      cout << ans << endl;
      return 0;
    }
    if (!bad) {
      swap(labels1, labels2);
    }
    bad = false;
  }
  cout << ans << endl;
}
