显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int n,m;
char a[10010],b[10010],c[10010];
int nx[10010],x[10010];
int main(){
freopen("mgrid.in","r",stdin);
freopen("mgrid.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++){
scanf("%s",a+1);
if(i == 1){
for(int j = 1;j <= m;j++)b[j] = a[j];
}
c[i] = a[1];
}
for(int i = 2,j = 0;i <= m;i++){
while(j > 0 && b[i] != b[j+1])j = nx[j];
if(b[i] == b[j+1])j++;
nx[i] = j;
}
for(int i = 2,j = 0;i <= n;i++){
while(j > 0 && c[i] != c[j+1])j = x[j];
if(c[i] == c[j+1])j++;
x[i] = j;
}
printf("%d\n",(n - x[n]) * (m - nx[m]));
return 0;
}