This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
bool used[1005][15];
int N,M;
int memo[1005][10][1030][2];
int func(int n, int pos, int bitm, bool c){
    if (pos==-1){
        pos = M-1;
        n--;
        if (c) return 999999999;
    }
    if (n<0){
        return bitm==0?0:999999999;
    }
    if (memo[n][pos][bitm][c]!=-1) return memo[n][pos][bitm][c];
    if ((bitm&(1<<pos))!=0 && used[n][pos]){
        return 999999999;
    }
    if (used[n][pos] && c){
        return 999999999;
    }
    if (used[n][pos]){
        return memo[n][pos][bitm][c] = func(n,pos-1,bitm,c);
    }
    if (c && (bitm&(1<<pos))!=0) return 999999999;
    int starthorz = 1+func(n,pos-1,bitm,false);
    int startvert = 1+func(n,pos-1,bitm&~(1<<pos),false);
    int usehorz = func(n,pos-1,bitm,true);
    int usevert = func(n,pos-1,bitm|(1<<pos),false);
    //printf("%d %d, %d %d %d %d\n",n,pos,starthorz,startvert,usehorz,usevert);
    if (c){
        return memo[n][pos][bitm][c] = min(starthorz,usehorz);
    }
    if (bitm&(1<<pos)){
        return memo[n][pos][bitm][c] = min(startvert, usevert);
    }
    return memo[n][pos][bitm][c] = min(min(starthorz, usehorz),min(startvert,usevert));
}
int main(){
    memset(memo,-1,sizeof(memo));
    scanf("%d%d",&N,&M);
    for (int x = 0; x<N; x++){
        for (int y = 0; y<M; y++){
            char c;
            scanf(" %c",&c);
            used[x][y] = c=='.';
        }
    }
    printf("%d",func(N,-1,0,false));
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |     scanf("%d%d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~
Main.cpp:49:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   49 |             scanf(" %c",&c);
      |             ~~~~~^~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |