#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

char c[1500][1500];
int num1[1500], num2[1500];

int main(){
    freopen("video.in", "r", stdin);
    freopen("video.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    gets(c[0]);
    int a = 0, b = 0 , bb = 0;
    for (int i = 0; i < n; i++){
        gets(c[i]);
    }
    int i1, j1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++){
            i1 = (n + i - 1) % n;
            j1 = (m + j - 1) % m;
            if (c[i][j] == c[i1][j] && c[i1][j] == c[i][j1] && c[i][j] == c[i1][j1]){
                num1[i]++;
                num2[j]++;
            }
    }
    int pos1, pos2, best1 = 1e9, best2 =    1e9, ans = 0;
    for (int i = 0; i < n; i++)
        if (num1[i] < best1){
            best1 = num1[i];
            pos1 = i;
        }
    for (int i = 0; i < m; i++)
        if (num2[i] < best2){
            best2 = num2[i];
            pos2 = i;
        }
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++){
            i1 = (n + i - 1) % n;
            j1 = (m + j - 1) % m;
            if (i != pos1 && j != pos2)
            if (c[i][j] == c[i1][j] && c[i1][j] == c[i][j1] && c[i][j] == c[i1][j1]){
                ans++;
            }
    }
    cout << ans;
    return 0;
}
