#include <bits/stdc++.h>
using namespace std;
const int N = 16384;
int n, m;
int p[N + N];
int findSet(int v) {
  if (p[v] == -1 || p[v] == v) return p[v];
  return p[v] = findSet(p[v]);
}
string t;
void parse(string s, int &cnt) {
  t = "";
  for (auto ch : s) {
    switch (ch) {
      case '0':
        t += "0000";
        break;
      case '1':
        t += "0001";
        break;
      case '2':
        t += "0010";
        break;
      case '3':
        t += "0011";
        break;
      case '4':
        t += "0100";
        break;
      case '5':
        t += "0101";
        break;
      case '6':
        t += "0110";
        break;
      case '7':
        t += "0111";
        break;
      case '8':
        t += "1000";
        break;
      case '9':
        t += "1001";
        break;
      case 'A':
        t += "1010";
        break;
      case 'B':
        t += "1011";
        break;
      case 'C':
        t += "1100";
        break;
      case 'D':
        t += "1101";
        break;
      case 'E':
        t += "1110";
        break;
      case 'F':
        t += "1111";
        break;
    }
  }
  for (int i = 0; i < m; i++) {
    if (t[i] == '1') {
      if (i > 0 && t[i - 1] == '1') {
        p[N + i] = p[N + i - 1];
      } else {
        p[N + i] = N + i;
        cnt++;
      }
    } else {
      p[N + i] = -1;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int cnt = 0;
    parse(s, cnt);
    ans += cnt;
    if (i > 0) {
      for (int j = 0; j < (int)(m); j++) {
        if (p[N + j] != -1 && p[j] != -1) {
          int v = findSet(j);
          int u = findSet(N + j);
          if (v != u) {
            p[v] = u;
            ans--;
          }
        }
      }
    }
    for (int j = 0; j < (int)(m); j++) {
      p[j] = max(findSet(N + j) - N, -1);
    }
  }
  cout << ans << endl;
  return 0;
}
