#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second

vector < int > max_;

bool cmp(const pair < string, int >& a, const pair < string, int >& b)
{
    return max_[a.s] < max_[b.s];
}

int main()
{
    #ifndef LOCAL_DEBUG
    freopen("video.in", "rt", stdin);
    freopen("video.out", "wt", stdout);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector < pair < string, int > > s(n);
    for (int i = 0; i < n; i++)
    {
        cin >> s[i].f;
        s[i].s = i;
    }
    max_.resize(n);
    vector < int > maxi(n);
    int curr = 0, curri;
    for (int i = 0; i < n; i++)
    {
        int j = 0;
        while (j < m)
        {
            curr = 0, curri = j;
            while (j < m && s[i].f[j] == '2')
                j++, curr++;
            if (curr > max_[i])
                max_[i] = curr, maxi[i] = curri;
            while (j < m && s[i].f[j] == '1')
                j++;
        }
    }
    sort(s.begin(), s.end(), cmp);
    for (int i = 0; i < n; i++)
    {
        s[i].f = s[i].f.substr(maxi[s[i].s]) + s[i].f.substr(0, maxi[s[i].s]);
    }
    int64_t res = 0;
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < m - 1; j++)
        {
            if (s[i].f[j] == s[i].f[j + 1] && s[i].f[j] == s[i + 1].f[j + 1] && s[i].f[j] == s[i + 1].f[j])
            {
                res++;
            }
        }
    }
    cout << res << endl;
    return 0;
}
