#include <bits/stdc++.h>
using namespace std;
char s[1005][1005];
int del[1005];
int main() {
  int n, m, minv = 1000000000;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  for (int j = 0; j < m; j++) {
    int ok = 1;
    for (int i = 1; i < n; i++)
      if (s[i][j] < s[i - 1][j]) {
        ok = 0;
        break;
      }
    if (ok) {
      int ans = 0;
      for (int i = 0; i < j; i++) del[i] = 1;
      while (ok) {
        ok = 0;
        for (int i = 1; i < n; i++) {
          if (s[i][j] > s[i - 1][j])
            continue;
          else {
            for (int k = j + 1; k < m; k++) {
              if (del[k]) continue;
              if (s[i][k] < s[i - 1][k])
                del[k] = 1, ok = 1;
              else if (s[i][k] > s[i - 1][k])
                break;
            }
          }
        }
      }
      for (int i = 0; i < m; i++)
        if (del[i]) ans++, del[i] = 0;
      minv = min(minv, ans);
    }
  }
  printf("%d\n", min(minv, m));
  return 0;
}
