#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int a[4][100010];
int p[N];
int find(int x) {
  if (x != p[x]) {
    p[x] = find(p[x]);
  }
  return p[x];
}
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int ans = 0;
  int i, j;
  for (i = 1; i <= n; i++) {
    int now = i & 1;
    string s;
    cin >> s;
    s = " " + s;
    for (int j = 1; j <= m / 4; j++) {
      int res;
      if (s[j] >= '0' && s[j] <= '9') {
        res = s[j] - '0';
      } else {
        res = s[j] - 'A' + 10;
      }
      a[now][j * 4] = res & 1, ans += (res & 1), res >>= 1;
      a[now][j * 4 - 1] = res & 1, ans += (res & 1), res >>= 1;
      a[now][j * 4 - 2] = res & 1, ans += (res & 1), res >>= 1;
      a[now][j * 4 - 3] = res & 1, ans += (res & 1), res >>= 1;
    }
    int tmp = now * m;
    for (int j = 1; j <= m; j++) {
      p[tmp + j] = tmp + j;
    }
    for (int j = 1; j < m; j++) {
      if (a[now][j] && a[now][j + 1]) {
        int pa = find(tmp + j);
        int pb = find(tmp + j + 1);
        if (pa != pb) {
          p[pb] = pa;
          ans--;
        }
      }
    }
    for (int j = 1; j <= m; j++) {
      if (a[now][j] && a[now ^ 1][j]) {
        int pa = find(tmp + j);
        int pb = find((now ^ 1) * m + j);
        if (pa != pb) {
          p[pb] = pa;
          ans--;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
