#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e4 + 5;
int par[maxn], a[2][maxn], n, m, ans = 0;
char s[maxn];
int findfa(int x) {
  if (par[x] == x) return x;
  par[x] = findfa(par[x]);
  return par[x];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int k = i & 1;
    scanf("%s", s + 1);
    for (int j = 1; j <= m / 4; j++) {
      int res;
      if (isdigit(s[j]))
        res = s[j] - '0';
      else
        res = s[j] - 'A' + 10;
      a[k][j * 4] = res & 1, ans += (res & 1), res >>= 1;
      a[k][j * 4 - 1] = res & 1, ans += (res & 1), res >>= 1;
      a[k][j * 4 - 2] = res & 1, ans += (res & 1), res >>= 1;
      a[k][j * 4 - 3] = res & 1, ans += (res & 1), res >>= 1;
    }
    int resl = k * m;
    for (int j = 1; j <= m; j++) {
      par[resl + j] = resl + j;
    }
    for (int j = 1; j < m; j++) {
      if (a[k][j] && a[k][j + 1]) {
        int x = resl + j, y = x + 1;
        int fx = findfa(x), fy = findfa(y);
        if (fx != fy) par[fy] = fx, --ans;
      }
    }
    for (int j = 1; j <= m; j++) {
      if (a[k][j] && a[k ^ 1][j]) {
        int x = resl + j, y = (k ^ 1) * m + j;
        int fx = findfa(x), fy = findfa(y);
        if (fx != fy) par[fy] = fx, --ans;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
