#include <bits/stdc++.h>
#define double long double
#define AC return 0
#define pii pair<int,int>
#define fi first
#define se second
#define gc getchar
#define el cout<<"\n"
#define ls (p<<1)
#define rs (p<<1|1)
#define lowbit(x) ((x)&(-x))
using namespace std;
const int N = 200010;
int n, m, f[110][1 << 10][1 << 10], h[1 << 10], a[110], l, ans;
char c;
int getsum(int x) {
int ret = 0;
while (x) {
x -= lowbit(x);
ret++;
}
return ret;
}
string gete(int x) {
return bitset<4>(x).to_string();
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 0; j < m; j++) {
cin >> c;
a[i] <<= 1;
if (c == 'H')a[i]++;
}
for (int i = 0; i < (1 << m); i++)
if (!(((i << 2) | (i << 1) | (i >> 1) | (i >> 2))&i))
h[++l] = i;
for (int i = 1; i <= n + 1; i++)
for (int j = 1; j <= l; j++)
if (!(a[i]&h[j]))
for (int k = 1; k <= l; k++)
if ((!(a[i - 1]&h[k])) && (!(h[j]&h[k])))
for (int p = 1; p <= l; p++)
if ((!(a[i - 2]&h[j])) && (!(h[k]&h[p])) && (!(h[j]&h[p])))
f[i][h[j]][h[k]] = max(f[i][h[j]][h[k]], f[i - 1][h[k]][h[p]] + getsum(h[j]));
for (int i = 1; i <= l; i++)
ans = max(ans, f[n + 1][0][h[i]]);
cout << ans;
AC;
}