#include <bits/stdc++.h>
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <class T>
using vec = std::vector<T>;
template <class T>
using vec2d = std::vector<std::vector<T>>;
bool __hack = std::ios::sync_with_stdio(false);
auto __hack1 = cin.tie(nullptr);
struct Input {
  Input(istream &in) : in(&in) {}
  template <class T>
  T next() const {
    T x;
    *in >> x;
    return x;
  }
  int ni() const { return next<int>(); }
  string ns() const { return next<string>(); }
  istream *in;
};
Input in(cin);
class Output {
 private:
  ostream *out;
  template <typename T>
  void printSingle(const T &value) {
    *out << value;
  }

 public:
  Output(ostream &out) : out(&out) {}
  inline void print() {}
  template <typename T, typename... Ts>
  inline void print(const T &f, const Ts &...args) {
    printSingle(f);
    if (sizeof...(args) != 0) {
      *out << ' ';
      print(args...);
    }
  }
  template <typename... Ts>
  inline void println(const Ts &...args) {
    print(args...);
    (*out) << '\n';
  }
  template <typename... Ts>
  inline void operator()(const Ts &...args) {
    println(args...);
  }
};
Output out(cout);
namespace template_util {
constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; }
template <int N>
struct bytetype {};
template <uint64_t N>
struct minimal_uint : bytetype<bytecount(N)> {};
}  // namespace template_util
namespace dsu {
struct DSU {
  vector<int> parent;
  vector<int> rank;
  vector<int> size;
  DSU(int n) {
    parent = vector<int>(n, 0);
    rank = vector<int>(n, 0);
    size = vector<int>(n, 1);
    for (int i = 0; i < (n); ++i) {
      parent[i] = i;
    }
  }
  void resetOne(int i) {
    parent[i] = i;
    rank[i] = 0;
    size[i] = 1;
  }
  bool unite(int a, int b) {
    a = findSet(a);
    b = findSet(b);
    if (a == b) {
      return false;
    }
    int size_a = size[a];
    int size_b = size[b];
    if (rank[a] >= rank[b]) {
      ++rank[b];
      parent[b] = a;
    } else {
      parent[a] = b;
    }
    size[findSet(a)] = size_a + size_b;
    return true;
  }
  int findSet(int v) {
    return (v == parent[v]) ? v : (parent[v] = findSet(parent[v]));
  }
};
}  // namespace dsu
vec2d<int> V = {
    {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1},
    {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1},
    {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
    {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1},
};
vec<bool> readOne() {
  auto s = in.ns();
  vec<bool> v;
  v.reserve(s.length() * 4);
  for (int i = 0; i < (s.length()); ++i) {
    int x = (isdigit(s[i]) ? s[i] - '0' : s[i] + 10 - 'A');
    for (int j = 0; j < (4); ++j) {
      v.push_back(bool(V[x][j]));
    }
  }
  return v;
}
void solve(istream &inStream, ostream &outStream) {
  in = Input(inStream);
  out = Output(outStream);
  auto n = in.ni();
  auto m = in.ni();
  vec<bool> prev = readOne();
  vec<int> colors(m, -1);
  int curColor = 0;
  for (int i = 0; i < (m); ++i) {
    if (prev[i]) {
      int j = i;
      for (; j < m && prev[j]; ++j) {
        colors[j] = curColor;
      }
      i = j - 1;
      ++curColor;
    }
  }
  vec<int> toRemap(2 * m);
  vec<int> remapVersion(2 * m);
  int curVersion = 1;
  auto remapColors = [&]() {
    curColor = 0;
    ++curVersion;
    for (int i = 0; i < (m); ++i) {
      if (colors[i] != -1) {
        if (remapVersion[colors[i]] != curVersion) {
          remapVersion[colors[i]] = curVersion;
          toRemap[colors[i]] = curColor++;
        }
        colors[i] = toRemap[colors[i]];
      }
    }
  };
  if (n == 1) {
    out(curColor);
    return;
  }
  int res = 0;
  vec<int> nextColor(m);
  vec<bool> cur;
  dsu::DSU ds(2 * m);
  vec<int> countColor(2 * m);
  vec<int> version(2 * m);
  for (int i = (1); i < (n); ++i) {
    cur = readOne();
    for (int j = 0; j < (m); ++j) {
      if (cur[j]) {
        int k = j;
        for (; k < m && cur[k]; ++k) {
          nextColor[k] = curColor;
        }
        j = k - 1;
        ++curColor;
      } else {
        nextColor[j] = -1;
      }
    }
    for (int j = 0; j < (curColor); ++j) {
      ds.resetOne(j);
    }
    for (int j = 0; j < (m); ++j) {
      if (nextColor[j] != -1 && colors[j] != -1) {
        ds.unite(nextColor[j], colors[j]);
      }
    }
    for (int j = 0; j < (m); ++j) {
      if (colors[j] != -1) {
        colors[j] = ds.findSet(colors[j]);
      }
      if (nextColor[j] != -1) {
        nextColor[j] = ds.findSet(nextColor[j]);
      }
    }
    int cnt = 0;
    for (int j = 0; j < (m); ++j) {
      if (colors[j] != -1) {
        if (version[colors[j]] != i) {
          ++cnt;
          version[colors[j]] = i;
          countColor[colors[j]] = true;
        }
      }
    }
    for (int j = 0; j < (m); ++j) {
      if (nextColor[j] != -1) {
        if (version[nextColor[j]] == i) {
          if (countColor[nextColor[j]]) {
            --cnt;
            countColor[nextColor[j]] = 0;
          }
        }
      }
    }
    res += cnt;
    prev = cur;
    colors = nextColor;
    remapColors();
  }
  int cnt = 0;
  for (int j = 0; j < (m); ++j) {
    if (colors[j] != -1) {
      if (version[colors[j]] != n + 1) {
        ++cnt;
        version[colors[j]] = n + 1;
        countColor[colors[j]] = true;
      }
    }
  }
  res += cnt;
  out(res);
}
int main() {
  solve(cin, cout);
  return 0;
}
