#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; const int maxn = 2050; int n,m,ans; char s[maxn][maxn]; bool ok; bool vis[maxn]; int main(){ freopen("evensgn.in","r",stdin); freopen("evensgn.out","w",stdout); cin>>n>>m; for(int i = 1;i <= n;i++){ scanf("%s",s[i]+1); } memset(vis,true,sizeof(vis)); for(int i = 1;i <= m;i++){ ok = true; for(int j = 2;j <= n;j++){ if(vis[j]&& s[j][i] < s[j-1][i]){ ans++; ok = false; break; } } if(ok){ for(int j = 2;j <= n;j++){ if(s[j][i] != s[j-1][i]) vis[j] = false; } } } cout<<ans; return 0; } #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<stack> #include<cstdlib> #include<string> #include<bitset> #define INF 100000000 #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int,int> pii; char s[1005][1005]; int rk[1005],trk[1005]; int main() { int n,m,i,j,ans=0,flag; freopen("evensgn.in","r",stdin); freopen("evensgn.out","w",stdout); cin>>n>>m; for(i=1;i<=n;i++) scanf("%s",s[i]+1); for(j=1;j<=m;j++) { flag=0; trk[1]=0; for(i=1;i<n;i++) { if(rk[i]<rk[i+1]) { trk[i+1]=trk[i]+1; continue; } else { if(s[i][j]>s[i+1][j]) { ans++; //printf("j=%d i=%d ",j,i); flag=1; break; } else if(s[i][j]<s[i+1][j]) trk[i+1]=trk[i]+1; else trk[i+1]=trk[i]; } } if(!flag) for(i=1;i<=n;i++) rk[i]=trk[i]; } cout<<ans<<endl; return 0; } // davidlee1999WTK 2014/ // srO myk Orz //ios::sync_with_stdio(false);