#include <bits/stdc++.h>
using namespace std;
char a[1005][1005];
int w[1005];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", a[i]);
  }
  int ans = 0, k = -1;
  for (k = 0; k < m; ++k) {
    bool bad = false;
    for (int i = 0; i < n - 1; ++i) {
      if (a[i][k] > a[i + 1][k]) {
        bad = true;
      }
    }
    if (!bad) {
      break;
    }
    ++ans;
  }
  if (k < m) {
    for (int i = 0; i < n - 1; ++i) {
      w[i] = int(a[i][k] < a[i + 1][k]);
    }
    for (int j = k + 1; j < m; ++j) {
      bool bad = false;
      for (int i = 0; i < n - 1; ++i) {
        if (w[i] == 0 && a[i][j] > a[i + 1][j]) {
          bad = true;
        }
      }
      if (bad) {
        ++ans;
      } else {
        for (int i = 0; i < n - 1; ++i) {
          if (w[i] == 0) {
            if (a[i][j] < a[i + 1][j]) {
              w[i] = 1;
            }
          }
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
