import java.util.*;
import java.io.*;

import static java.lang.Math.*;

public class tictactoe_as {
    int[][] f;

    int[] dx = {-1, -1, -1, 0};
    int[] dy = {-1, 0, 1, 1};

    boolean isWin1(int x, int y, int who) {
        if (f[x][y] != 0) {
            return false;
        }
        f[x][y] = who;
        boolean win = false;
        for (int dir = 0; dir < 4; dir++) {
            int sx = x;
            int sy = y;
            int cnt = 0;
            while (0 <= sx && sx < f.length && 0 <= sy && sy < f[sx].length && f[sx][sy] == who && cnt < 5) {
                sx += dx[dir];
                sy += dy[dir];
                cnt++;
            }
            sx = x - dx[dir];
            sy = y - dy[dir];
            while (0 <= sx && sx < f.length && 0 <= sy && sy < f[sx].length && f[sx][sy] == who && cnt < 5) {
                sx -= dx[dir];
                sy -= dy[dir];
                cnt++;
            }
            if (cnt >= 5) {
                win = true;
            }
        }
        f[x][y] = 0;
        return win;
    }

    public void run() throws IOException {
        in = new FastScanner(new FileInputStream("tictactoe.in"));
        out = new PrintWriter(new File("tictactoe.out"));

        int ans = solve();
        out.println(ans);

        out.close();
    }

    private int solve() {
        int h = in.nextInt();
        int w = in.nextInt();

        f = new int[h + 4][w + 4];
        for (int i = 0; i < h; i++) {
            String s = in.next();
            for (int j = 0; j < w; j++) {
                if (s.charAt(j) == 'X') {
                    f[i + 2][j + 2] = 1;
                }
                if (s.charAt(j) == '0') {
                    f[i + 2][j + 2] = 2;
                }
            }
        }

        h += 4;
        w += 4;
        int cntWin1 = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (isWin1(i, j, 1)) {
                    cntWin1++;
                }
            }
        }

        if (cntWin1 > 0) {
            return cntWin1;
        }

        int cntLose1 = 0;
        int losex = 0, losey = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (isWin1(i, j, 2)) {
                    cntLose1++;
                    losex = i;
                    losey = j;
                }
            }
        }
        if (cntLose1 > 1) {
            return 0;
        }

        if (cntLose1 == 1) {
            f[losex][losey] = 1;
            int cntWin2 = 0;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (isWin1(i, j, 1)) {
                        cntWin2++;
                    }
                }
            }
            if (cntWin2 > 1) {
                return 1;
            } else {
                return 0;
            }
        }

        int ans = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (f[i][j] == 0) {
                    f[i][j] = 1;
                    int cntWin2 = 0;
                    for (int x = i - 5; x <= i + 5; x++) {
                        for (int y = j - 5; y <= j + 5; y++) {
                            if (0 <= x && x < h && 0 <= y && y < w && isWin1(x, y, 1)) {
                                cntWin2++;
                            }
                        }
                    }
                    if (cntWin2 > 1) {
                        ans++;
                    }
                    f[i][j] = 0;
                }
            }
        }
        return ans;
    }

    FastScanner in;
    PrintWriter out;

    public static void main(String[] args) throws IOException {
        new tictactoe_as().run();
    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(InputStream in) throws IOException {
            br = new BufferedReader(new InputStreamReader(in));
        }

        String next() {
            try {
                while (st == null || !st.hasMoreTokens()) {
                    st = new StringTokenizer(br.readLine());
                }
            } catch (IOException e) {
                System.err.println(e);
                System.exit(1);
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}