#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  char input[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> input[i][j];
  vector<vector<int> > check(n, vector<int>(m));
  int ans = 0;
  for (int j = 0; j < m; j++)
    for (int i = 1; i < n; i++) {
      if (j == ans || (j > ans && check[i][j - 1] == 1)) {
        if (input[i][j] == input[i - 1][j])
          check[i][j] = 1;
        else if (input[i][j] > input[i - 1][j])
          check[i][j] = 0;
        else {
          if (j != ans)
            for (int k = 1; k < n; k++) check[k][j] = check[k][j - 1];
          ans++;
          break;
        }
      }
    }
  cout << ans;
  return 0;
}
