#include <iostream>                                                                                                                      
#include <vector>                                                                                                                        
#define min(x,y) ((x < y) ? x : y)                                                                                                       
                                                                                                                                         
int main()                                                                                                                               
{                                                                                                                                        
    int h, w;                                                                                                                            
    std::cin >> h >> w;                                                                                                                  
                                                                                                                                         
    std::vector< std::vector<int> > cicadas(h, std::vector<int>(w, 0));                                                                  
    for (int i = 0; i < h; ++i)                                                                                                          
    {                                                                                                                                    
        std::string str;                                                                                                                 
        std::cin >> str;                                                                                                                 
        for (int j = 0; j < w; ++j)                                                                                                      
            cicadas[i][j] = (int)(str.at(j) - '0');                                                                                      
    }                                                                                                                                    
                                                                                                                                         
    std::vector< std::vector<int> > sum(cicadas);                                                                                        
    for (int i = 0; i < h; ++i)                                                                                                          
    for (int j = 0; j < w; ++j)                                                                                                          
    {                                                                                                                                    
        if (i == 0 && j == 0)                                                                                                            
            continue;                                                                                                                    
        else if (i == 0)                                                                                                                 
            sum[i][j] += sum[i][j-1];                                                                                                    
        else if (j == 0)                                                                                                                 
            sum[i][j] += sum[i-1][j];                                                                                                    
        else                                                                                                                             
            sum[i][j] += min(sum[i][j-1], sum[i-1][j]);                                                                                  
    }                                                                                                                                    
                                                                                                                                         
    std::cout << sum[h-1][w-1] << std::endl;                                                                                             
                                                                                                                                         
    return 0;                                                                                                                            
}                                                                                                                                        
                                                                                                                                         
        

Measured memory_bytes: 1256